Skip to content

Commit

Permalink
finish crud template
Browse files Browse the repository at this point in the history
  • Loading branch information
aviggngyv committed Feb 27, 2017
1 parent a091224 commit a4ac747
Show file tree
Hide file tree
Showing 13 changed files with 910 additions and 19 deletions.
266 changes: 266 additions & 0 deletions core/command/ContentTypeCreateCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
<?php

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Command\Command as BaseCommand;
use Hunter\Core\App\Application;
use Hunter\Core\Utility\StringConverter;

/**
* 创建content-type命令
* php hunter ct:create
*/
class ContentTypeCreateCmd extends BaseCommand {

/**
* @var moduleList
*/
protected $moduleList;

/**
* @var routeList
*/
protected $routeList;

/**
* @var StringConverter
*/
protected $stringConverter;

/**
* @var StringConverter
*/
protected $append = false;

/**
* InstallCommand constructor.
* @param Site $site
*/
public function __construct() {
$application = new Application();
$this->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;
}

}
26 changes: 20 additions & 6 deletions core/command/ControllerCreateCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?'
);
}

Expand All @@ -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);
Expand All @@ -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
);

Expand All @@ -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!');
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -182,7 +196,7 @@ protected function interact(InputInterface $input, OutputInterface $output) {
'args' => $this->getArgumentsFromRoute($path)
];
}

$input->setOption('routes', $routes);
}
}
Expand Down
7 changes: 7 additions & 0 deletions core/command/ModuleCreateCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?'
);
}

Expand All @@ -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)) {
Expand Down Expand Up @@ -135,6 +141,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
'core' => $core,
'description' => $description,
'package' => $package,
'isContentType' => $isContentType,
);

$writed = $this->renderFile(
Expand Down
Loading

0 comments on commit a4ac747

Please sign in to comment.