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 @@ + + + + + Add <?php print $name; ?> + + + + + + + + + + + + + + + + diff --git a/core/command/templates/ct-edit.html b/core/command/templates/ct-edit.html new file mode 100644 index 0000000..bc2215c --- /dev/null +++ b/core/command/templates/ct-edit.html @@ -0,0 +1,98 @@ + + + + + Edit <?php print $name; ?> + + + + + + + + + +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ + +
+
+ +
+
+ +
+
+
+
+ + + + + + diff --git a/core/command/templates/ct-install.html b/core/command/templates/ct-install.html new file mode 100644 index 0000000..ddb9429 --- /dev/null +++ b/core/command/templates/ct-install.html @@ -0,0 +1,82 @@ + + +/** + * @file + * database info. + */ + +function _schema() { + $schema[''] = array( + 'description' => 'Stores data.', + 'fields' => array( + 'id' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'description' => 'ID of .', + ), + 'title' => array( + 'type' => 'varchar', + 'length' => 60, + 'not null' => TRUE, + 'default' => '', + 'description' => ' title.', + ), + 'content' => array( + 'type' => 'text', + 'size' => 'normal', + 'description' => " content.", + ), + 'images' => array( + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + 'description' => ' images.', + ), + 'attach_files' => array( + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + 'description' => ' attach files.', + ), + 'uid' => array( + 'type' => 'varchar', + 'length' => 60, + 'not null' => TRUE, + 'default' => '', + 'description' => " uid.", + ), + 'status' => array( + 'type' => 'varchar', + 'length' => 9, + 'not null' => TRUE, + 'default' => '', + 'description' => ' status.', + ), + 'created' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + 'description' => 'The Unix timestamp when the user was created.', + ), + 'updated' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + 'description' => 'The Unix timestamp when the user was updated.', + ), + ), + 'primary key' => array('id'), + ); + + return $schema; +} + +/** + * Insert init . + */ +function _install() { + +} diff --git a/core/command/templates/ct-list.html b/core/command/templates/ct-list.html new file mode 100644 index 0000000..3b46e59 --- /dev/null +++ b/core/command/templates/ct-list.html @@ -0,0 +1,165 @@ + + + + + + <?php print $name; ?> List + + + + + + + + + +
+
+ +
+ + + + + + + + + + + + + '; ?> + + + + + + + + + ';?> + +
IDNameauthorStatusCreatedOperations
+ '.substr($type, 0, 1 ); ?>id; ?> + + title; ?>'; ?> + + uid); ?>'; ?> + + status ? "published" : "not published"; ?>'; ?> + + created); ?>'; ?> + + edit + delete +
+ +
+ + + + + + diff --git a/core/command/templates/ctcontroller.php.html b/core/command/templates/ctcontroller.php.html new file mode 100644 index 0000000..61a7474 --- /dev/null +++ b/core/command/templates/ctcontroller.php.html @@ -0,0 +1,127 @@ + + + +namespace Hunter\\Controller; + +use Zend\Diactoros\ServerRequest; + +/** + * Class . + * + * @package Hunter\\Controller + */ +class Controller { + /** + * _list. + * + * @return string + * Return _list string. + */ + public function _list() { + $_list = $this->get__list(); + + return view('/admin/-list.html', array('s' => $_list)); + } + + /** + * _add. + * + * @return string + * Return _add string. + */ + public function _add(ServerRequest $request) { + if ($parms = $request->getParsedBody()) { + $title = $parms['title']; + $content = $parms['content']; + $status = $parms['status']; + $user = session()->get('admin'); + + $id = db_insert('') + ->fields(array( + 'title' => $title, + 'content' => $content, + 'status' => $status, + 'uid' => $user->uid, + 'created' => time(), + 'updated' => time(), + )) + ->execute(); + + return $id; + } + + return view('/admin/-add.html'); + } + + /** + * _edit. + * + * @return string + * Return _edit string. + */ + public function _edit($id) { + $ = get__byid($id); + + return view('/admin/-edit.html', array('' => $, 'id' => $id)); + } + + /** + * _update. + * + * @return string + * Return _update string. + */ + public function _update(ServerRequest $request) { + if ($parms = $request->getParsedBody()) { + $id = $parms['id']; + $title = $parms['title']; + $content = $parms['content']; + $status = $parms['status']; + $user = session()->get('admin'); + + db_update('') + ->fields(array( + 'title' => $title, + 'content' => $content, + 'status' => $status, + 'uid' => $user->uid, + 'updated' => time(), + )) + ->condition('id', $id) + ->execute(); + + return true; + } + return false; + } + + /** + * _del. + * + * @return string + * Return _del string. + */ + public function _del($id) { + $result = db_delete('') + ->condition('id', $id) + ->execute(); + + if ($result) { + return true; + } + + return false; + } + + /** + * get list + */ + public function get__list() { + $_list = db_select('', '') + ->fields('') + ->execute() + ->fetchAllAssoc('id'); + + return $_list; + } +} diff --git a/core/command/templates/module.html b/core/command/templates/module.html index e78750d..fdca9b9 100644 --- a/core/command/templates/module.html +++ b/core/command/templates/module.html @@ -1,2 +1,29 @@ + + /** + * get by id + */ + function get__byid($id) { + $ = db_select('', '') + ->fields('') + ->condition('.id', $id) + ->execute() + ->fetchObject(); + + return $; + } + + /** + * get all + */ + function get_all_() { + $s = db_select('', '') + ->fields('') + ->execute() + ->fetchAll(); + + return $s; + } + + diff --git a/core/command/templates/routing-controller.yml.html b/core/command/templates/routing-controller.yml.html index 554d0a7..c8d8784 100644 --- a/core/command/templates/routing-controller.yml.html +++ b/core/command/templates/routing-controller.yml.html @@ -7,4 +7,5 @@ defaults: _controller: '\Hunter\\Controller\Controller::' _title: '' + \ No newline at end of file diff --git a/core/includes/common.inc b/core/includes/common.inc index 273a3a8..de365a6 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -154,6 +154,11 @@ function base_path() { return $GLOBALS['base_path']; } +//Returns the base root path. +function base_root() { + return $GLOBALS['base_root']; +} + //Get current path. function request_uri() { if (isset($_SERVER['REQUEST_URI'])) {