Skip to content

Commit

Permalink
Fixed console command bug when integrate to Tastphp
Browse files Browse the repository at this point in the history
  • Loading branch information
xujiajun committed Oct 12, 2017
1 parent 4ea8271 commit f8e6619
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 43 deletions.
5 changes: 5 additions & 0 deletions src/Framework/Console/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ protected function changeDir($dir)
chdir(__BASEDIR__ . "/{$dir}");
}

protected function getTemplateDir()
{
return __IMPORT_DIR__ . "/Console/Command/Template";
}

private function processName($name)
{
list($name, $names) = $this->handleName($name);
Expand Down
41 changes: 21 additions & 20 deletions src/Framework/Console/Command/GenerateBundleCommand.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace TastPHP\Framework\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Filesystem\Filesystem;

class GenerateBundleCommand extends Command
class GenerateBundleCommand extends BaseCommand
{
protected function configure()
{
Expand All @@ -24,7 +24,6 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->changeDir('src/');
$name = $input->getArgument('name');

if ($name) {
Expand All @@ -33,7 +32,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$helper = $this->getHelper('question');
$question = new Question('Please enter the name of the bundle:', 'DemoBundle');
$question = new Question('Please enter the name of the bundle (default:DemoBundle):', 'DemoBundle');
$name = $helper->ask($input, $output, $question);
$this->generateBundleService($output, $name);
}
Expand All @@ -46,35 +45,37 @@ private function generateBundleService($output, $name)
);
}
$name = ucfirst($name);
$this->generateBundleDir($name);
$this->updateRoutesConfig($name);
$this->updateConfig($name);
$templateDir = $this->getTemplateDir();
$bundleDir = __EXPORT_DIR__ . "/src/" . $name;
$this->generateBundleDir($bundleDir);
$this->updateRoutesConfig($name, $templateDir);
$this->updateConfig($name, $templateDir);
$output->writeln("<fg=black;bg=green>You have success generate {$name}Bundle</>");
}

private function generateBundleDir($name)
private function generateBundleDir($bundleDir)
{
$filesystem = new Filesystem();
$filesystem->mkdir("{$name}/Config");
$filesystem->dumpFile("{$name}/Config/routes.yml", '');
$filesystem->mkdir("{$name}/Controller");
$filesystem->dumpFile("{$name}/Controller/.gitkeep", '');
$filesystem->mkdir("{$name}/Listener");
$filesystem->dumpFile("{$name}/Listener/.gitkeep", '');
$filesystem->mkdir("{$bundleDir}/Config");
$filesystem->dumpFile("{$bundleDir}/Config/routes.yml", '');
$filesystem->mkdir("{$bundleDir}/Controller");
$filesystem->dumpFile("{$bundleDir}/Controller/.gitkeep", '');
$filesystem->mkdir("{$bundleDir}/Listener");
$filesystem->dumpFile("{$bundleDir}/Listener/.gitkeep", '');
}

private function updateRoutesConfig($name)
private function updateRoutesConfig($name, $templateDir)
{
$routesConfigContent = file_get_contents(__DIR__ . "/Template/routesConfig.txt");
$routesConfigContent = file_get_contents($templateDir . "/routesConfig.txt");
$routesConfigContent = str_replace('{{demo}}', $name, $routesConfigContent);
file_put_contents(__BASEDIR__ . "/config/routes.yml", PHP_EOL . $routesConfigContent, FILE_APPEND);
file_put_contents(__EXPORT_DIR__ . "/config/routes.yml", PHP_EOL . $routesConfigContent, FILE_APPEND);
}

private function updateConfig($name)
private function updateConfig($name, $templateDir)
{
$configContent = file_get_contents(__DIR__ . "/Template/config.txt");
$configContent = file_get_contents($templateDir . "/config.txt");
$configContent = str_replace('{{Demo}}', $name, $configContent);
$configContent = str_replace('{{demo}}', lcfirst($name), $configContent);
file_put_contents(__BASEDIR__ . "/config/config.yml", PHP_EOL . $configContent, FILE_APPEND);
file_put_contents(__EXPORT_DIR__ . "/config/config.yml", PHP_EOL . $configContent, FILE_APPEND);
}
}
27 changes: 14 additions & 13 deletions src/Framework/Console/Command/GenerateEntityServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->changeDir('src/');
$name = $input->getArgument('name');
if ($name) {
$this->generateEntityService($output, $name);
Expand All @@ -42,31 +41,33 @@ private function generateEntityService($output, $name)
$tableName = $name;
$name = $this->getGenerateEntityServiceNameByTableName($tableName);
$filesystem = new Filesystem();
$serviceDir = __EXPORT_DIR__ . "/src/Service/" . $name;
$templateDir = $this->getTemplateDir();
//service file
$filesystem->mkdir("Service/$name");
$serviceContent = file_get_contents(__DIR__ . "/Template/EntityService.txt");
$filesystem->mkdir($serviceDir);
$serviceContent = file_get_contents($templateDir . "/EntityService.txt");
$serviceContent = str_replace('Entity', $name, $serviceContent);
$serviceContent = str_replace('entity', lcfirst($name), $serviceContent);
$filesystem->dumpFile("Service/$name/{$name}Service.php", $serviceContent);
$filesystem->dumpFile($serviceDir . "/{$name}Service.php", $serviceContent);
//service Impl file
$filesystem->mkdir("Service/$name/Impl");
$serviceImplContent = file_get_contents(__DIR__ . "/Template/EntityServiceImpl.txt");
$filesystem->mkdir($serviceDir . "/Impl");
$serviceImplContent = file_get_contents($templateDir . "/EntityServiceImpl.txt");
$serviceImplContent = str_replace('Entity', $name, $serviceImplContent);
$serviceImplContent = str_replace('entity', lcfirst($name), $serviceImplContent);
$filesystem->dumpFile("Service/$name/Impl/{$name}ServiceImpl.php", $serviceImplContent);
$filesystem->dumpFile($serviceDir . "/Impl/{$name}ServiceImpl.php", $serviceImplContent);
//Dao file
$filesystem->mkdir("Service/$name/Dao");
$daoContent = file_get_contents(__DIR__ . "/Template/EntityDao.txt");
$filesystem->mkdir($serviceDir . "/Dao");
$daoContent = file_get_contents($templateDir . "/EntityDao.txt");
$daoContent = str_replace('Entity', $name, $daoContent);
$daoContent = str_replace('entity', lcfirst($name), $daoContent);
$filesystem->dumpFile("Service/$name/Dao/{$name}Dao.php", $daoContent);
$filesystem->dumpFile($serviceDir . "/Dao/{$name}Dao.php", $daoContent);
//Dao Impl file
$filesystem->mkdir("Service/$name/Dao/Impl");
$daoImplContent = file_get_contents(__DIR__ . "/Template/EntityDaoImpl.txt");
$filesystem->mkdir($serviceDir . "/Dao/Impl");
$daoImplContent = file_get_contents($templateDir . "/EntityDaoImpl.txt");
$daoImplContent = str_replace('Entity', $name, $daoImplContent);
$daoImplContent = str_replace('entity', lcfirst($name), $daoImplContent);
$daoImplContent = str_replace('{{tableName}}', $tableName, $daoImplContent);
$filesystem->dumpFile("Service/$name/Dao/Impl/{$name}DaoImpl.php", $daoImplContent);
$filesystem->dumpFile($serviceDir . "/Dao/Impl/{$name}DaoImpl.php", $daoImplContent);
$output->writeln("<fg=black;bg=green>You have success generate {$name}Service(Dao)</>");
}
}
22 changes: 12 additions & 10 deletions src/Framework/Console/Command/GenerateServiceCommand.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace TastPHP\Framework\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class GenerateServiceCommand extends Command
class GenerateServiceCommand extends BaseCommand
{
protected function configure()
{
Expand All @@ -24,29 +24,31 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->changeDir('src/App/');
$helper = $this->getHelper('question');
$question = new Question('Please enter the name of service:', 'demoservice');
$question = new Question('Please enter the name of service (default:demo):', 'demo');
$name = $helper->ask($input, $output, $question);
$filesystem = new Filesystem();
$ucName = ucfirst($name);
$lcName = lcfirst($name);
$filesystem->mkdir("$ucName");

$serviceContent = file_get_contents(__DIR__ . "/Template/demoservice.txt");
$appDir = __EXPORT_DIR__ . "/src/App";
$providerDir = $appDir . "/" . $ucName;
$templateDir = $this->getTemplateDir();
$serviceContent = file_get_contents($templateDir . "/demoservice.txt");
$serviceContent = str_replace('{{demoservice}}', $lcName, $serviceContent);
$serviceContent = str_replace('{{Demoservice}}', $ucName, $serviceContent);
$filesystem->dumpFile("$ucName/$ucName.php", $serviceContent);
$filesystem->dumpFile($providerDir . "/$ucName.php", $serviceContent);

$serviceContent = file_get_contents(__DIR__ . "/Template/demoserviceService.txt");
$serviceContent = file_get_contents($templateDir . "/demoserviceService.txt");
$serviceContent = str_replace('{{demoservice}}', $lcName, $serviceContent);
$serviceContent = str_replace('{{Demoservice}}', $ucName, $serviceContent);
$filesystem->dumpFile("$ucName/{$ucName}Service.php", $serviceContent);
$filesystem->dumpFile($providerDir . "/{$ucName}Service.php", $serviceContent);

$serviceContent = file_get_contents(__DIR__ . "/Template/demoserviceServiceProvider.txt");
$serviceContent = file_get_contents($templateDir . "/demoserviceServiceProvider.txt");
$serviceContent = str_replace('{{demoservice}}', $lcName, $serviceContent);
$serviceContent = str_replace('{{Demoservice}}', $ucName, $serviceContent);
$filesystem->dumpFile("$ucName/{$ucName}ServiceProvider.php", $serviceContent);
$filesystem->dumpFile($providerDir . "/{$ucName}ServiceProvider.php", $serviceContent);

$output->writeln("<fg=black;bg=green>You have success generate {$ucName}Service</>");
}
Expand Down

0 comments on commit f8e6619

Please sign in to comment.