diff --git a/core/command/ContentTypeCreateCmd.php b/core/command/ContentTypeCreateCmd.php new file mode 100644 index 0000000..24029f4 --- /dev/null +++ b/core/command/ContentTypeCreateCmd.php @@ -0,0 +1,266 @@ +moduleList = $application->boot()->getModulesList(); + $this->routeList = $application->boot()->getRoutesList(); + $this->stringConverter = new StringConverter(); + + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + protected function configure() { + $this + ->setName('ct:create') + ->setDescription('commands.controller.create.description') + ->addOption( + 'type', + '', + InputOption::VALUE_REQUIRED, + 'commands.create.controller.options.type' + ) + ->addOption( + 'name', + '', + InputOption::VALUE_OPTIONAL, + 'commands.create.controller.options.name' + ) + ->addOption( + 'description', + '', + InputOption::VALUE_REQUIRED, + 'commands.create.controller.options.description' + ); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) { + $type = $input->getOption('type'); + $name = $input->getOption('name'); + $description = $input->getOption('description'); + + $modulecommand = $this->getApplication()->find('module:create'); + + $modulearguments = array( + 'command' => 'module:create', + '--module' => $type, + '--machine-name' => $this->stringConverter->createMachineName($type), + '--module-path' => '/module', + '--description' => $description, + '--core' => '1.x', + '--package' => 'Custom', + '--module-file' => 'yes', + 'isContentType' => TRUE, + ); + + $moduletypeInput = new ArrayInput($modulearguments); + $returnCode = $modulecommand->run($moduletypeInput, $output); + + $ctlcommand = $this->getApplication()->find('ctl:create'); + + $ctlearguments = array( + 'command' => 'ctl:create', + '--module' => $type, + '--class' => ucfirst($type), + '--routes' => array( + array( + 'title' => $type.' list', + 'name' => $type.'.'.$type.'_list', + 'method' => $type.'_list', + 'path' => '/admin/'.$type.'/list', + 'args' => array(), + ), + array( + 'title' => $type.' add', + 'name' => $type.'.'.$type.'_add', + 'method' => $type.'_add', + 'path' => '/admin/'.$type.'/add', + 'args' => array(), + ), + array( + 'title' => $type.' edit', + 'name' => $type.'.'.$type.'_edit', + 'method' => $type.'_edit', + 'path' => '/admin/'.$type.'/edit/{'.substr($type, 0, 1 ).'id}', + 'args' => array('$'.substr($type, 0, 1 ).'id'), + ), + array( + 'title' => $type.' update', + 'name' => $type.'.'.$type.'_update', + 'method' => $type.'_update', + 'path' => '/admin/'.$type.'/update', + 'args' => array(), + ), + array( + 'title' => $type.' del', + 'name' => $type.'.'.$type.'_del', + 'method' => $type.'_del', + 'path' => '/admin/'.$type.'/del/{'.substr($type, 0, 1 ).'id}', + 'args' => array('$'.substr($type, 0, 1 ).'id'), + ) + ), + 'isContentType' => TRUE, + ); + + $ctltypeInput = new ArrayInput($ctlearguments); + $returnCode = $ctlcommand->run($ctltypeInput, $output); + + $writed = $this->renderFile('ct-list.html', HUNTER_ROOT .'/theme/admin/'.$type.'-list.html', array('type' => $type, 'name' => $name)); + $writed = $this->renderFile('ct-add.html', HUNTER_ROOT .'/theme/admin/'.$type.'-add.html', array('type' => $type, 'name' => $name)); + $writed = $this->renderFile('ct-edit.html', HUNTER_ROOT .'/theme/admin/'.$type.'-edit.html', array('type' => $type, 'name' => $name)); + $writed = $this->renderFile('ct-install.html', HUNTER_ROOT .'/module/'.$type.'/'.$type.'.install', array('type' => $type, 'name' => $name)); + + $module_install_command = $this->getApplication()->find('module:install'); + + $module_install_arguments = array( + 'command' => 'module:install', + '--module' => $type, + ); + + $module_install_typeInput = new ArrayInput($module_install_arguments); + $returnCode = $module_install_command->run($module_install_typeInput, $output); + + if($writed){ + $output->writeln('['.date("Y-m-d H:i:s").'] '.$type.' content type create successful!'); + }else{ + $output->writeln('['.date("Y-m-d H:i:s").'] '.$type.' content type create failed!'); + } + } + + /** + * {@inheritdoc} + */ + protected function interact(InputInterface $input, OutputInterface $output) { + $helper = $this->getHelper('question'); + + // --type option + $type = $input->getOption('type'); + if (!$type) { + $question = new Question('Enter the new type name:', ''); + $type = $helper->ask($input, $output, $question); + $input->setOption('type', $type); + } + + // --name option + $name = $input->getOption('name'); + if (!$name) { + $question = new Question('Enter the name:', ''); + $name = $helper->ask($input, $output, $question); + $input->setOption('name', $name); + } + + // --description option + $description = $input->getOption('description'); + if (!$description) { + $question = new Question('Enter type description [My custom content type]:', 'My custom content type'); + $description = $helper->ask($input, $output, $question); + $input->setOption('description', $description); + } + } + + /** + * @param string $template + * @param string $target + * @param array $parameters + * @param null $flag + * + * @return bool + */ + protected function renderFile($template, $target, $parameters, $flag = null) { + if (!is_dir(dirname($target))) { + mkdir(dirname($target), 0777, true); + } + + if (file_put_contents($target, theme('command')->render($template, $parameters), $flag)) { + $this->files[] = str_replace(HUNTER_ROOT.'/', '', $target); + + return true; + } + + return false; + } + + /** + * @return array + */ + private function inlineValueAsArray($inputValue) + { + $inputArrayValue = []; + foreach ($inputValue as $key => $value) { + if (!is_array($value)) { + $inputValueItems = []; + foreach (explode(" ", $value) as $inputKeyValueItem) { + list($inputKeyItem, $inputValueItem) = explode(":", $inputKeyValueItem); + $inputValueItems[$inputKeyItem] = $inputValueItem; + } + $inputArrayValue[$key] = $inputValueItems; + } + } + + return $inputArrayValue?$inputArrayValue:$inputValue; + } + + /** + * @return array + */ + public function getArgumentsFromRoute($path) + { + $returnValues = ''; + preg_match_all('/{(.*?)}/', $path, $returnValues); + + $returnValues = array_map( + function ($value) { + return sprintf('$%s', $value); + }, $returnValues[1] + ); + + return $returnValues; + } + +} diff --git a/core/command/ControllerCreateCmd.php b/core/command/ControllerCreateCmd.php index 06f5be4..77bcb65 100644 --- a/core/command/ControllerCreateCmd.php +++ b/core/command/ControllerCreateCmd.php @@ -73,6 +73,11 @@ protected function configure() { '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'commands.create.controller.options.routes' + ) + ->addArgument( + 'isContentType', + InputArgument::OPTIONAL, + 'Is this a new content type?' ); } @@ -83,9 +88,12 @@ protected function execute(InputInterface $input, OutputInterface $output) { $module = $input->getOption('module'); $class = $input->getOption('class'); $routes = $input->getOption('routes'); + $isContentType = $input->getArgument('isContentType'); if(isset($this->moduleList[$module])){ $module_path = HUNTER_ROOT .'/'. dirname($this->moduleList[$module]['pathname']); + }else{ + $module_path = HUNTER_ROOT .'/module/'.strtolower($module); } $routes = $this->inlineValueAsArray($routes); @@ -98,9 +106,15 @@ protected function execute(InputInterface $input, OutputInterface $output) { 'append' => $this->append ]; + if($isContentType){ + $ctltemplate = 'ctcontroller.php.html'; + }else { + $ctltemplate = 'controller.php.html'; + } + $writed = $this->renderFile( - 'controller.php.html', - $module_path.'/src/Controller/'.$class.'.php', + $ctltemplate, + $module_path.'/src/Controller/'.$class.'Controller.php', $parameters ); @@ -112,9 +126,9 @@ protected function execute(InputInterface $input, OutputInterface $output) { ); if($writed){ - $output->writeln('['.date("Y-m-d H:i:s").'] '.$class.' create successful!'); + $output->writeln('['.date("Y-m-d H:i:s").'] '.$class.' Controller create successful!'); }else{ - $output->writeln('['.date("Y-m-d H:i:s").'] '.$class.' create failed!'); + $output->writeln('['.date("Y-m-d H:i:s").'] '.$class.' Controller create failed!'); } } @@ -156,7 +170,7 @@ protected function interact(InputInterface $input, OutputInterface $output) { if(isset($this->routeList[$module]) && !empty($this->routeList[$module])){ $this->append = true; } - + while (true) { $title_question = new Question('Enter the Controller method title (leave empty and press enter when done) []:', ''); $title = $helper->ask($input, $output, $title_question); @@ -182,7 +196,7 @@ protected function interact(InputInterface $input, OutputInterface $output) { 'args' => $this->getArgumentsFromRoute($path) ]; } - + $input->setOption('routes', $routes); } } diff --git a/core/command/ModuleCreateCmd.php b/core/command/ModuleCreateCmd.php index 3f13a85..3c6cbb1 100644 --- a/core/command/ModuleCreateCmd.php +++ b/core/command/ModuleCreateCmd.php @@ -84,6 +84,11 @@ protected function configure() { '', InputOption::VALUE_NONE, 'commands.create.module.options.module-file' + ) + ->addArgument( + 'isContentType', + InputArgument::OPTIONAL, + 'Is this a new content type?' ); } @@ -98,6 +103,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $core = $input->getOption('core'); $package = $input->getOption('package'); $moduleFile = $input->getOption('module-file'); + $isContentType = $input->getArgument('isContentType'); $dir .= '/'.$machineName; if (file_exists($dir)) { @@ -135,6 +141,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { 'core' => $core, 'description' => $description, 'package' => $package, + 'isContentType' => $isContentType, ); $writed = $this->renderFile( diff --git a/core/command/ModuleInstallCmd.php b/core/command/ModuleInstallCmd.php index db5cdec..265f60c 100644 --- a/core/command/ModuleInstallCmd.php +++ b/core/command/ModuleInstallCmd.php @@ -45,22 +45,24 @@ protected function execute(InputInterface $input, OutputInterface $output) { $installed = false; if(isset($this->moduleList[$input->getOption('module')])){ $install_file = str_replace('info.yml', 'install', $this->moduleList[$input->getOption('module')]['pathname']); + }else { + $install_file = 'module/'.$input->getOption('module').'/'.$input->getOption('module').'.install'; + } - if(file_exists($install_file)){ - require_once $install_file; - } + if(file_exists($install_file)){ + require_once $install_file; - $schema_fun = $input->getOption('module').'_schema'; - $install_fun = $input->getOption('module').'_install'; - if (function_exists($schema_fun)) { - $schemas = $schema_fun(); - } + $schema_fun = $input->getOption('module').'_schema'; + $install_fun = $input->getOption('module').'_install'; + if (function_exists($schema_fun)) { + $schemas = $schema_fun(); + } - $installed = db_schema()->installSchema($schemas); + $installed = db_schema()->installSchema($schemas); - if (function_exists($install_fun)) { - $install_fun(); - } + if (function_exists($install_fun)) { + $install_fun(); + } } if($installed){ diff --git a/core/command/templates/controller.php.html b/core/command/templates/controller.php.html index eeff38c..909a969 100644 --- a/core/command/templates/controller.php.html +++ b/core/command/templates/controller.php.html @@ -8,7 +8,7 @@ * * @package Hunter\\Controller */ -class { +class Controller { /** * . diff --git a/core/command/templates/ct-add.html b/core/command/templates/ct-add.html new file mode 100644 index 0000000..fcd2531 --- /dev/null +++ b/core/command/templates/ct-add.html @@ -0,0 +1,97 @@ + + +
+ +