diff --git a/bin/kathamo b/bin/kathamo index 9867cc5..58aa70e 100755 --- a/bin/kathamo +++ b/bin/kathamo @@ -3,9 +3,5 @@ require_once __DIR__ . '../../vendor/autoload.php'; use Kathamo\App\Commands\KathamoCLI; -use Symfony\Component\Console\Application; -$app = new Application(); -$app->add(new KathamoCLI); - -$app->run(); +new KathamoCLI(); diff --git a/src/CommandManager/Manager.php b/src/CommandManager/Manager.php index 2efbe9a..1ff4e62 100644 --- a/src/CommandManager/Manager.php +++ b/src/CommandManager/Manager.php @@ -2,7 +2,7 @@ namespace Kathamo\App\CommandManager; -class Manager +class Helper { public function input($label) { diff --git a/src/CommandManager/Template.php b/src/CommandManager/Template.php deleted file mode 100644 index 88753d3..0000000 --- a/src/CommandManager/Template.php +++ /dev/null @@ -1,14 +0,0 @@ -kathamo_path); - } -} diff --git a/src/Commands/CommandHelper.php b/src/Commands/CommandHelper.php new file mode 100644 index 0000000..01ecd7b --- /dev/null +++ b/src/Commands/CommandHelper.php @@ -0,0 +1,57 @@ +root_path . "/configs/config.php")) { + throw new \Exception("Config file not found."); + } + return require $this->root_path . "/configs/config.php"; + } + + protected function generateFile($template) + { + $data = array_merge($this->input, $this->getConfig()); + $data['namespace_suffix'] = ""; + if (! empty($data['input_path'])) { + $data['namespace_suffix'] = "\\" . str_replace("/", "\\", $data['input_path']); + } + + $controller_template = dirname(__DIR__) . "/templates/partials/$template"; + $mustache = new \Mustache_Engine(array('entity_flags' => ENT_QUOTES)); + $file_content = $mustache->render(file_get_contents($controller_template), $data); + + $this->create($data, $file_content); + } + + protected function create($data, $file_content) + { + $controller_dir = $this->target_path . $data['input_path']; + if (! empty($data['input_path']) && ! is_dir($controller_dir)) { + mkdir($controller_dir); + } + if (is_dir($controller_dir)) { + $target_path = $controller_dir . "/"; + } + + $file = fopen( + $target_path . $data['input_class_name'] . ".php", "w" + ); + fwrite($file, $file_content); + fclose($file); + } +} diff --git a/src/CommandManager/Scaffold.php b/src/Commands/Creation/Scaffold.php similarity index 59% rename from src/CommandManager/Scaffold.php rename to src/Commands/Creation/Scaffold.php index 676c8a3..b593a94 100644 --- a/src/CommandManager/Scaffold.php +++ b/src/Commands/Creation/Scaffold.php @@ -1,21 +1,33 @@ getInputs(); + protected function configure() + { + $this->setName('create create:plugin') + ->setDescription('Cli for Kathamo framework.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->getInputs(); echo Notifier::notify(" Creating Kathamo framework scaffold..."); new CreateSkeletonFiles($this->input); echo Notifier::notify("\n Done."); + + return Command::SUCCESS; } private function getInputs() diff --git a/src/Commands/Creation/Template.php b/src/Commands/Creation/Template.php new file mode 100644 index 0000000..b723c56 --- /dev/null +++ b/src/Commands/Creation/Template.php @@ -0,0 +1,28 @@ +setName('create create:template') + ->setDescription('Cli for Kathamo framework.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + new CreateTemplateFiles($this->kathamo_path); + + return Command::SUCCESS; + } +} diff --git a/src/Commands/KathamoCLI.php b/src/Commands/KathamoCLI.php index 4013f82..dcd1cf6 100644 --- a/src/Commands/KathamoCLI.php +++ b/src/Commands/KathamoCLI.php @@ -1,32 +1,26 @@ setName('create') - ->setDescription('Cli for Kathamo framework.') - ->addOption('new') - ->addOption('template'); - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - if ($input->getOption('new')) { - new Scaffold(); - } + $app = new Application(); + $app->add(new Scaffold()); + $app->add(new Template()); - if ($input->getOption('template')) { - new Template(); - } + $app->add(new MakeMigration()); + $app->add(new MakeController()); + $app->add(new MakeService()); + $app->add(new MakeMigration()); - return Command::SUCCESS; + $app->run(); } } diff --git a/src/Commands/Make/MakeController.php b/src/Commands/Make/MakeController.php new file mode 100644 index 0000000..fe657a4 --- /dev/null +++ b/src/Commands/Make/MakeController.php @@ -0,0 +1,37 @@ +setName('make make:controller') + ->setDescription('New Controller for plugin') + ->setAliases(['make:c']); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->getInput(); + + return Command::SUCCESS; + } + + public function getInput() + { + $this->root_path = getcwd(); + $this->target_path = $this->root_path . "/app/Controllers/"; + $this->input['input_class_name'] = $this->input("Controller Name:"); + $this->input['input_path'] = $this->input("Path [app/Controllers/]:"); + + $this->generateFile('Controller_php.mustache'); + } +} diff --git a/src/Commands/Make/MakeMiddleware.php b/src/Commands/Make/MakeMiddleware.php new file mode 100644 index 0000000..412b0a6 --- /dev/null +++ b/src/Commands/Make/MakeMiddleware.php @@ -0,0 +1,68 @@ +setName('make make:middleware') + ->setDescription('New Middleware for plugin') + ->setAliases(['make:mw']); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->getInput(); + $this->generateMiddleware(); + + return Command::SUCCESS; + } + + public function getInput() + { + $this->root_path = getcwd(); + $this->target_path = $this->root_path . "/app/Controllers/Middleware/"; + $this->input['input_class_name'] = $this->input("Middleware Class Name:"); + $this->input['middleware_key'] = $this->input("Middleware key:"); + $this->input['input_path'] = $this->input("Path [app/Controllers/Middleware]:"); + } + + private function generateMiddleware() + { + $data = $this->input; + $config_data = $this->getConfig($this->root_path); + $middware_list = $config_data['middlewares']; + $middleware_name = $config_data['namaspace_root'] . "\App\Controllers\Middleware\\" . $data['input_class_name']; + $middware_list[$data['middleware_key']] = $middleware_name; + + $this->writeConfig($middware_list); + $this->generateFile('Middleware_php.mustache'); + } + + private function writeConfig($data) + { + $schema = ''; + foreach ($data as $key => $i) { + $schema .= "'$key' => $i::class,\n\t\t"; + } + + $controller_template = dirname(__DIR__) . '/templates/partials/config_php.mustache'; + $mustache = new \Mustache_Engine(); + $file_content = $mustache->render( + file_get_contents($controller_template), + ['middleware_list' => $schema] + ); + + $filesystem = new Filesystem; + $filesystem->dumpFile($this->root_path . '/Configs/config.php', $file_content); + } +} diff --git a/src/Commands/Make/MakeMigration.php b/src/Commands/Make/MakeMigration.php new file mode 100644 index 0000000..e00672d --- /dev/null +++ b/src/Commands/Make/MakeMigration.php @@ -0,0 +1,35 @@ +setName('make make:migration') + ->setDescription('New Migration class for plugin') + ->setAliases(['make:m']); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->getInput(); + $this->generateFile('Migration_php.mustache'); + + return Command::SUCCESS; + } + + public function getInput() + { + $this->root_path = getcwd(); + $this->target_path = $this->root_path . "/Database/Migrations/"; + $this->input['input_class_name'] = $this->input("Migration Name:"); + } +} diff --git a/src/Commands/Make/MakeService.php b/src/Commands/Make/MakeService.php new file mode 100644 index 0000000..5a04f95 --- /dev/null +++ b/src/Commands/Make/MakeService.php @@ -0,0 +1,36 @@ +setName('make make:service') + ->setDescription('New Service for plugin') + ->setAliases(['make:s']); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->getInput(); + $this->generateFile('Service_php.mustache'); + + return Command::SUCCESS; + } + + public function getInput() + { + $this->root_path = getcwd(); + $this->target_path = getcwd() . "/app/Services/"; + $this->input['input_class_name'] = $this->input("Service Name:"); + $this->input['input_path'] = $this->input("Path [app/Services/]:"); + } +} diff --git a/src/Services/CreateSkeletonFiles.php b/src/Services/CreateSkeletonFiles.php index 46c255f..f448415 100644 --- a/src/Services/CreateSkeletonFiles.php +++ b/src/Services/CreateSkeletonFiles.php @@ -8,10 +8,12 @@ class CreateSkeletonFiles { private $input_data = []; + private $root_path; public function __construct($input) { $this->input_data = $input; + $this->createRootDir(); $finder = new Finder(); $finder @@ -25,12 +27,23 @@ public function __construct($input) } } + private function createRootDir() + { + $this->root_path = getcwd() . '/' . $this->input_data['text_domain']; + if (is_dir($this->root_path)) { + throw new \Exception("`{$this->input_data['text_domain']}` Directory already exists."); + } + + $filesystem = new Filesystem(); + $filesystem->mkdir($this->root_path); + } + private function createFileDir($file) { if (! is_dir($file->getRealPath())) { return; } - $new_file_path = getcwd() . '/' . $file->getRelativePathname(); + $new_file_path = $this->root_path . '/' . $file->getRelativePathname(); mkdir($new_file_path); return; } @@ -42,7 +55,7 @@ private function createFile($file) } $filesystem = new Filesystem(); - $new_file_path = getcwd() . '/' . $file->getRelativePathname(); + $new_file_path = $this->root_path . '/' . $file->getRelativePathname(); copy( $file->getRealPath(), diff --git a/src/templates/partials/Controller_php.mustache b/src/templates/partials/Controller_php.mustache new file mode 100644 index 0000000..a2f9d33 --- /dev/null +++ b/src/templates/partials/Controller_php.mustache @@ -0,0 +1,13 @@ +column('ID')->bigInt()->unsigned()->autoIncrement()->primary()->required() + // ->column('key')->string(255)->required() + // ->column('data')->string(255)->default('NULL') + // ->index(['ID']) + // ->execute(); + } +} diff --git a/src/templates/partials/Service_php.mustache b/src/templates/partials/Service_php.mustache new file mode 100644 index 0000000..314336b --- /dev/null +++ b/src/templates/partials/Service_php.mustache @@ -0,0 +1,12 @@ + 'kathamo', + 'plugin_slug' => 'kathamo', + 'namaspace_root' => 'Kathamo', + 'plugin_version' => '1.0.0', + 'plugin_name' => 'Kathamo', + 'dev_mode' => false, + 'root_dir' => dirname(__DIR__), + 'middlewares' => [ + {{{middleware_list}}} + ] +);