diff --git a/_demo/quick-start/phar-project/demo-001/.gitignore b/_demo/quick-start/phar-project/demo-001/.gitignore new file mode 100644 index 0000000..7835776 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-001/.gitignore @@ -0,0 +1,4 @@ +composer.lock +lib/vendor/* +tmp/* +demo.phar diff --git a/_demo/quick-start/phar-project/demo-001/README.md b/_demo/quick-start/phar-project/demo-001/README.md new file mode 100644 index 0000000..163f292 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-001/README.md @@ -0,0 +1,98 @@ + + +# Skel Project + + + + +## init + +``` sh +composer install +``` + + + + +## update + +``` sh +composer update +``` + + + + +## dump-autoload + +``` sh +composer dump-autoload +``` + + + + +## run app + +run + +``` sh +php app.php +``` + +or run + +``` sh +./app.php +``` + + + + +## build app + +run + +``` sh +php build.php +``` + +or run + +``` sh +./build.php +``` + + + + +## Link + +* [PHP Composer](https://getcomposer.org/) + + + +## Symfony + +* [symfony/console](https://symfony.com/doc/current/components/console.html) +* [symfony/filesystem](https://symfony.com/doc/current/components/filesystem.html) + + + + +## Phar Create Case + +* [Pointless/scripts/build.php](https://github.com/scarwu/Pointless/blob/master/scripts/build.php) +* [composer/src/Composer/Compiler.php](https://github.com/composer/composer/blob/main/src/Composer/Compiler.php) + + +## Notice + +edit `/etc/php.ini` + +``` ini +[Phar] +; https://php.net/phar.readonly +;phar.readonly = On +phar.readonly = Off +``` diff --git a/_demo/quick-start/phar-project/demo-001/app.php b/_demo/quick-start/phar-project/demo-001/app.php new file mode 100755 index 0000000..7f651d0 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-001/app.php @@ -0,0 +1,7 @@ +#!/usr/bin/env php +run(); + + + } + + + + +} diff --git a/_demo/quick-start/phar-project/demo-001/boot/loader/main.php b/_demo/quick-start/phar-project/demo-001/boot/loader/main.php new file mode 100644 index 0000000..0c91333 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-001/boot/loader/main.php @@ -0,0 +1,3 @@ +setProjectRootDirPath(THE_PRJ_ROOT_DIR_PATH) + ->setTargetPharFileName('demo.phar') + ->run(); diff --git a/_demo/quick-start/phar-project/demo-001/boot/start/main.php b/_demo/quick-start/phar-project/demo-001/boot/start/main.php new file mode 100644 index 0000000..b089cfb --- /dev/null +++ b/_demo/quick-start/phar-project/demo-001/boot/start/main.php @@ -0,0 +1,6 @@ +run(); diff --git a/_demo/quick-start/phar-project/demo-001/build.php b/_demo/quick-start/phar-project/demo-001/build.php new file mode 100755 index 0000000..cd4f540 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-001/build.php @@ -0,0 +1,7 @@ +#!/usr/bin/env php +mkdir( + $path, + ); + } catch (IOExceptionInterface $exception) { + echo "An error occurred while creating your directory at ".$exception->getPath(); + } + + + } + + + + +} diff --git a/_demo/quick-start/phar-project/demo-001/sys/src/Maintain/Compiler.php b/_demo/quick-start/phar-project/demo-001/sys/src/Maintain/Compiler.php new file mode 100644 index 0000000..c4e2121 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-001/sys/src/Maintain/Compiler.php @@ -0,0 +1,160 @@ +_ProjectRootDirPath = dirname(dirname(dirname(__DIR__))); + + $this->_ProjectRootDirPath = dirname(__DIR__, 3); + + + } + + + + + public function run () + { + + //ini_set('phar.readonly', 0); + + //var_dump(__FILE__); + + $project_root_dir_path = $this->_ProjectRootDirPath; + + $filesystem = new Filesystem(); + + + $target_phar_file_name = $this->_TargetPharFileName; + + $target_phar_file_path = "{$project_root_dir_path}/{$target_phar_file_name}"; + + + $project_tmp_dir_path = "{$project_root_dir_path}/tmp"; + + $target_root_dir_path = "{$project_tmp_dir_path}/prj"; + + + + + if (!file_exists("{$project_root_dir_path}/lib/vendor")) { + chdir($project_root_dir_path); + system('composer install'); + } + + if (file_exists("{$project_tmp_dir_path}")) { + echo "\n## Remove [tmp]({$project_tmp_dir_path})\n"; + $filesystem->remove("{$project_tmp_dir_path}"); + } + + + + + foreach ([ + 'app', + 'boot', + 'lib', + 'sys' + ] as $sub_path) { + + $filesystem->mirror( + "{$project_root_dir_path}/{$sub_path}", + "{$target_root_dir_path}/{$sub_path}" + ); + + } + + + if (file_exists("{$target_phar_file_path}")) { + $filesystem->remove("{$target_phar_file_path}"); + } + + +$stub = sprintf(<< +EOF, $target_phar_file_name, $target_phar_file_name, time()); + + //echo $stub; + + + + $phar = new \Phar("{$target_phar_file_path}"); + $phar->setAlias($target_phar_file_name); + $phar->setStub($stub); + $phar->buildFromDirectory("{$target_root_dir_path}"); + $phar->compressFiles(\Phar::GZ); + $phar->stopBuffering(); + + + echo "\n## Create [{$target_phar_file_name}]({$target_phar_file_path})\n"; + + // Setting Phar is Executable + chmod("{$target_phar_file_path}", 0755); + + return 0; + + } + + + + + protected string $_ProjectRootDirPath = ''; + + public function setProjectRootDirPath (string $val) + { + + $this->_ProjectRootDirPath = $val; + + return $this; + } + + + + + protected string $_TargetPharFileName = 'demo.phar'; + + public function setTargetPharFileName (string $val) + { + + $this->_TargetPharFileName = $val; + + return $this; + } + + + + +} diff --git a/_demo/quick-start/phar-project/demo-002/.gitignore b/_demo/quick-start/phar-project/demo-002/.gitignore new file mode 100644 index 0000000..7835776 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-002/.gitignore @@ -0,0 +1,4 @@ +composer.lock +lib/vendor/* +tmp/* +demo.phar diff --git a/_demo/quick-start/phar-project/demo-002/README.md b/_demo/quick-start/phar-project/demo-002/README.md new file mode 100644 index 0000000..a46bfb9 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-002/README.md @@ -0,0 +1,136 @@ + + +# Skel Project + + + + +## init + +``` sh +composer install +``` + + + + +## update + +``` sh +composer update +``` + + + + +## dump-autoload + +``` sh +composer dump-autoload +``` + + + + +## run app + +run + +``` sh +php app.php +``` + +or run + +``` sh +./app.php +``` + + + + +## build phar + +run + +``` sh +php build.php +``` + +or run + +``` sh +./build.php +``` + + +## phar usage + +run + +``` sh +./demo.phar help app:create-user +``` + + +run + +``` sh +./demo.phar app:create-user test +``` + + +run + +``` sh +./demo.phar app:create-user --user-role=Admin adm +``` + + +## Link + +* [PHP Composer](https://getcomposer.org/) + + + + +## Symfony / Components + +* [symfony/console](https://symfony.com/doc/current/components/console.html) +* [symfony/filesystem](https://symfony.com/doc/current/components/filesystem.html) + + +## Symfony / Docs + +* Console Commands / [Creating a Command](https://symfony.com/doc/current/console.html#creating-a-command) +* [Console Input (Arguments & Options)](https://symfony.com/doc/current/console/input.html) + + + + +## Phar Create Case + +* [Pointless/scripts/build.php](https://github.com/scarwu/Pointless/blob/master/scripts/build.php) +* [composer/src/Composer/Compiler.php](https://github.com/composer/composer/blob/main/src/Composer/Compiler.php) + + +## Symfony Console App Case + +* [composer](https://github.com/composer/composer) +* [leaf-cli](https://github.com/leafsphp/cli) + + + + +## Notice + +### Building Phar Requirement + +edit `/etc/php.ini` + +``` ini +[Phar] +; https://php.net/phar.readonly +;phar.readonly = On +phar.readonly = Off +``` diff --git a/_demo/quick-start/phar-project/demo-002/app.php b/_demo/quick-start/phar-project/demo-002/app.php new file mode 100755 index 0000000..7f651d0 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-002/app.php @@ -0,0 +1,7 @@ +#!/usr/bin/env php +add(new Command\CreateUserCommand()); + + + + + + $application->run(); + + + } + +} diff --git a/_demo/quick-start/phar-project/demo-002/app/src/App/Command/CreateUserCommand.php b/_demo/quick-start/phar-project/demo-002/app/src/App/Command/CreateUserCommand.php new file mode 100644 index 0000000..f65d418 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-002/app/src/App/Command/CreateUserCommand.php @@ -0,0 +1,118 @@ +setUserName($input->getArgument('username')) + ->setUserTitle($input->getOption('user-title')) + ->setUserRole($input->getOption('user-role')) + ->run(); + + + $output->writeln(''); + + $output->writeln([ + 'User Creator', + '============', + '', + ]); + + // retrieve the argument value using getArgument() + $output->writeln('Username: '.$input->getArgument('username')); + + + + // this method must return an integer number with the "exit status code" + // of the command. You can also use these constants to make code more readable + + // return this if there was no problem running the command + // (it's equivalent to returning int(0)) + return Command::SUCCESS; + + // or return this if some error happened during the execution + // (it's equivalent to returning int(1)) + // return Command::FAILURE; + + // or return this to indicate incorrect command usage; e.g. invalid options + // or missing arguments (it's equivalent to returning int(2)) + // return Command::INVALID + } + + + + + protected function configure (): void + { + + // run `demo.phar help app:create-user` + $this + // the command description shown when running "php bin/console list" + ->setDescription('Creates a new user.') + // the command help shown when running the command with the "--help" option + ->setHelp('This command allows you to create a user...') + ; + + + // ## https://symfony.com/doc/current/console/input.html#using-command-arguments + $this->addArgument( + 'username', + InputArgument::REQUIRED, + 'The user name of the user.' + ); + + + // ## https://symfony.com/doc/current/console/input.html#using-command-options + $this->addOption( + 'user-title', + null, + InputOption::VALUE_OPTIONAL, + 'The user title of the user.', + '' + ); + + + $this->addOption( + 'user-role', + 'r', + InputOption::VALUE_OPTIONAL, + 'The user role of the user.', + 'Normal' + ); + + + + } + + + + +} diff --git a/_demo/quick-start/phar-project/demo-002/boot/loader/main.php b/_demo/quick-start/phar-project/demo-002/boot/loader/main.php new file mode 100644 index 0000000..0c91333 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-002/boot/loader/main.php @@ -0,0 +1,3 @@ +setProjectRootDirPath(THE_PRJ_ROOT_DIR_PATH) + ->setTargetPharFileName('demo.phar') + ->run(); diff --git a/_demo/quick-start/phar-project/demo-002/boot/start/main.php b/_demo/quick-start/phar-project/demo-002/boot/start/main.php new file mode 100644 index 0000000..b089cfb --- /dev/null +++ b/_demo/quick-start/phar-project/demo-002/boot/start/main.php @@ -0,0 +1,6 @@ +run(); diff --git a/_demo/quick-start/phar-project/demo-002/build.php b/_demo/quick-start/phar-project/demo-002/build.php new file mode 100755 index 0000000..cd4f540 --- /dev/null +++ b/_demo/quick-start/phar-project/demo-002/build.php @@ -0,0 +1,7 @@ +#!/usr/bin/env php +_UserName; + echo("\n## User Name Input: {$user_name} \n"); + + $user_title = $this->_UserTitle; + echo("\n## User Title Input: {$user_title} \n"); + + $user_role = $this->_UserRole; + echo("\n## User Role Input: {$user_role} \n"); + + + + $filesystem = new Filesystem(); + $user_db_dir_path = Path::normalize(sys_get_temp_dir() . '/symfony/demo-project/db/user'); + $filesystem->mkdir($user_db_dir_path,); + echo("\n## Create Dir: {$user_db_dir_path} \n"); + + + //https://www.php.net/manual/en/function.file-put-contents.php + + $user_db_file_path = $user_db_dir_path . '/' . $user_name; + $user_db_content = ''; + $user_db_content .= "UserName: {$user_name}\n"; + $user_db_content .= "UserTitle: {$user_title}\n"; + $user_db_content .= "UserRole: {$user_role}\n"; + file_put_contents($user_db_file_path, $user_db_content); + echo("\n## Create User : [{$user_name}]({$user_db_file_path}) \n"); + echo("\n> $ `cat {$user_db_file_path}` \n"); + + + } + + + + + protected string $_UserName = ''; + + public function setUserName (string $val) + { + + $this->_UserName = $val; + + return $this; + } + + + + + protected string $_UserTitle = ''; + + public function setUserTitle (string $val) + { + + $this->_UserTitle = $val; + + return $this; + } + + + + + protected string $_UserRole = 'Normal'; + + public function setUserRole (string $val) + { + + $this->_UserRole = $val; + + return $this; + } + + + + +} diff --git a/_demo/quick-start/phar-project/demo-002/sys/src/Maintain/Compiler.php b/_demo/quick-start/phar-project/demo-002/sys/src/Maintain/Compiler.php new file mode 100644 index 0000000..1e5c9cf --- /dev/null +++ b/_demo/quick-start/phar-project/demo-002/sys/src/Maintain/Compiler.php @@ -0,0 +1,161 @@ +_ProjectRootDirPath = dirname(dirname(dirname(__DIR__))); + + $this->_ProjectRootDirPath = dirname(__DIR__, 3); + + + } + + + + + public function run () + { + + //ini_set('phar.readonly', 0); + + //var_dump(__FILE__); + + $project_root_dir_path = $this->_ProjectRootDirPath; + + $filesystem = new Filesystem(); + + + $target_phar_file_name = $this->_TargetPharFileName; + + $target_phar_file_path = "{$project_root_dir_path}/{$target_phar_file_name}"; + + + $project_tmp_dir_path = "{$project_root_dir_path}/tmp"; + + $target_root_dir_path = "{$project_tmp_dir_path}/prj"; + + + + + if (!file_exists("{$project_root_dir_path}/lib/vendor")) { + chdir($project_root_dir_path); + system('composer install'); + } + + if (file_exists("{$project_tmp_dir_path}")) { + echo "\n## Remove [tmp]({$project_tmp_dir_path})\n"; + $filesystem->remove("{$project_tmp_dir_path}"); + } + + + + + foreach ([ + 'app', + 'boot', + 'lib', + 'sys' + ] as $sub_path) { + + $filesystem->mirror( + "{$project_root_dir_path}/{$sub_path}", + "{$target_root_dir_path}/{$sub_path}" + ); + + } + + + if (file_exists("{$target_phar_file_path}")) { + $filesystem->remove("{$target_phar_file_path}"); + } + + +$stub = sprintf(<< +EOF, $target_phar_file_name, $target_phar_file_name, time()); + + //echo $stub; + + + + $phar = new \Phar("{$target_phar_file_path}"); + $phar->setAlias($target_phar_file_name); + $phar->setStub($stub); + $phar->buildFromDirectory("{$target_root_dir_path}"); + $phar->compressFiles(\Phar::GZ); + $phar->stopBuffering(); + + + echo "\n## Create [{$target_phar_file_name}]({$target_phar_file_path})\n"; + + // Setting Phar is Executable + chmod("{$target_phar_file_path}", 0755); + + return 0; + + } + + + + + protected string $_ProjectRootDirPath = ''; + + public function setProjectRootDirPath (string $val) + { + + $this->_ProjectRootDirPath = $val; + + return $this; + } + + + + + + protected string $_TargetPharFileName = 'demo.phar'; + + public function setTargetPharFileName (string $val) + { + + $this->_TargetPharFileName = $val; + + return $this; + } + + + + +}