Bugfixes
This commit is contained in:
parent
ff4dc90b6d
commit
8aeec9b01a
|
@ -3,6 +3,7 @@
|
|||
namespace Shyim\DatabaseStructBuilder\Command;
|
||||
|
||||
use Shyim\DatabaseStructBuilder\Generator;
|
||||
use Shyim\DatabaseStructBuilder\Structs\Request;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
|
@ -30,7 +31,8 @@ class GenerateModelCommand extends Command implements ContainerAwareInterface
|
|||
->addArgument('namespace', InputArgument::REQUIRED, 'Namespace that should be used "Example\\Models"')
|
||||
->addArgument('target', InputArgument::REQUIRED, 'Target directory (should be exists)')
|
||||
->addOption('filter', 'f', InputOption::VALUE_OPTIONAL, 'Filter tables which should be generated')
|
||||
->addOption('php-version', 'p', InputOption::VALUE_OPTIONAL, 'min PHP Version (php70, php71)', 'php71');
|
||||
->addOption('php-version', 'p', InputOption::VALUE_OPTIONAL, 'min PHP Version (php70, php71)', 'php71')
|
||||
->addOption('prefix-remove', null, InputOption::VALUE_OPTIONAL, 'Remove prefix from entity, example s_core');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -41,12 +43,14 @@ class GenerateModelCommand extends Command implements ContainerAwareInterface
|
|||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$this->container->get(Generator::class)->generateModels(
|
||||
$input->getArgument('namespace'),
|
||||
$input->getArgument('target'),
|
||||
$input->getOption('filter'),
|
||||
$input->getOption('php-version')
|
||||
);
|
||||
$request = new Request();
|
||||
$request->folder = $input->getArgument('target');
|
||||
$request->namespace = $input->getArgument('namespace');
|
||||
$request->filter = $input->getOption('filter');
|
||||
$request->phpVersion = $input->getOption('php-version');
|
||||
$request->removePrefixEntity = $input->getOption('prefix-remove');
|
||||
|
||||
$this->container->get(Generator::class)->generateModels($request);
|
||||
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->success('Models generated');
|
||||
|
|
|
@ -7,6 +7,7 @@ use Shyim\DatabaseStructBuilder\Services\DatabaseReader;
|
|||
use Shyim\DatabaseStructBuilder\Services\ModelGenerator;
|
||||
use Shyim\DatabaseStructBuilder\Services\RepositoryGenerator;
|
||||
use Shyim\DatabaseStructBuilder\Services\ServiceGenerator;
|
||||
use Shyim\DatabaseStructBuilder\Structs\Request;
|
||||
use Shyim\DatabaseStructBuilder\Structs\Table;
|
||||
use RuntimeException;
|
||||
|
||||
|
@ -68,18 +69,19 @@ class Generator
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $namespace
|
||||
* @param string $folder
|
||||
* @param string $filter
|
||||
* @param string $phpVersion
|
||||
* @author Soner Sayakci <shyim@posteo.de>
|
||||
* @param Request $request
|
||||
*/
|
||||
public function generateModels(string $namespace, string $folder, $filter, $phpVersion = 'php71'): void
|
||||
public function generateModels(Request $request): void
|
||||
{
|
||||
$tables = $this->reader->buildSchema($filter);
|
||||
$folder = realpath($folder);
|
||||
$tables = $this->reader->buildSchema($request);
|
||||
$folder = realpath($request->folder);
|
||||
|
||||
$this->baseClassGenerator->generate($namespace, $folder);
|
||||
if ($folder === false) {
|
||||
throw new RuntimeException(sprintf('Directory at path "%s" does not exist', $request->folder));
|
||||
}
|
||||
|
||||
$this->baseClassGenerator->generate($request->namespace, $folder);
|
||||
|
||||
/** @var Table $table */
|
||||
foreach ($tables as $table) {
|
||||
|
@ -90,9 +92,9 @@ class Generator
|
|||
}
|
||||
}
|
||||
|
||||
file_put_contents($modelDir . '/' . $table->camelCaseName . '.php', '<?php' . PHP_EOL . PHP_EOL . $this->modelGenerator->generate($namespace, $table, $phpVersion), LOCK_EX);
|
||||
file_put_contents($modelDir . '/' . $table->camelCaseName . 'Repository.php', '<?php' . PHP_EOL . PHP_EOL . $this->repositoryGenerator->generate($namespace, $table), LOCK_EX);
|
||||
file_put_contents($modelDir . '/' . $table->camelCaseName . 'Service.php', '<?php' . PHP_EOL . PHP_EOL . $this->serviceGenerator->generate($namespace, $table), LOCK_EX);
|
||||
file_put_contents($modelDir . '/' . $table->camelCaseName . '.php', '<?php' . PHP_EOL . PHP_EOL . $this->modelGenerator->generate($request->namespace, $table, $request->phpVersion), LOCK_EX);
|
||||
file_put_contents($modelDir . '/' . $table->camelCaseName . 'Repository.php', '<?php' . PHP_EOL . PHP_EOL . $this->repositoryGenerator->generate($request->namespace, $table), LOCK_EX);
|
||||
file_put_contents($modelDir . '/' . $table->camelCaseName . 'Service.php', '<?php' . PHP_EOL . PHP_EOL . $this->serviceGenerator->generate($request->namespace, $table), LOCK_EX);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace Shyim\DatabaseStructBuilder\Services;
|
|||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Shyim\DatabaseStructBuilder\Structs\Column;
|
||||
use Shyim\DatabaseStructBuilder\Structs\Request;
|
||||
use Shyim\DatabaseStructBuilder\Structs\Table;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
|
@ -33,16 +34,16 @@ class DatabaseReader
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $filter
|
||||
* @param Request $request
|
||||
* @return array
|
||||
* @author Soner Sayakci <shyim@posteo.de>
|
||||
*/
|
||||
public function buildSchema($filter): array
|
||||
public function buildSchema(Request $request): array
|
||||
{
|
||||
if ($filter !== null) {
|
||||
if ($request->filter !== null) {
|
||||
$tables = $this->connection->fetchAll(
|
||||
'SHOW TABLES WHERE Tables_in_' . $this->connection->getDatabase() . ' LIKE ?',
|
||||
['%' . $filter . '%']
|
||||
['%' . $request->filter . '%']
|
||||
);
|
||||
} else {
|
||||
$tables = $this->connection->fetchAll('SHOW TABLES');
|
||||
|
@ -57,7 +58,13 @@ class DatabaseReader
|
|||
foreach ($tables as $tableRow) {
|
||||
$table = new Table();
|
||||
$table->name = array_values($tableRow)[0];
|
||||
$table->camelCaseName = $this->camelCase($table->name);
|
||||
|
||||
if ($request->removePrefixEntity) {
|
||||
$table->camelCaseName = $this->camelCase(str_replace($request->removePrefixEntity, '', $table->name));
|
||||
} else {
|
||||
$table->camelCaseName = $this->camelCase($table->name);
|
||||
}
|
||||
|
||||
$table->columns = [];
|
||||
|
||||
$columnsRaw = $this->connection->fetchAll('SHOW COLUMNS FROM ' . $table->name);
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Shyim\DatabaseStructBuilder\Structs;
|
||||
use Shyim\DatabaseStructBuilder\Generator;
|
||||
|
||||
/**
|
||||
* Class Request
|
||||
* @author Soner Sayakci <shyim@posteo.de>
|
||||
*/
|
||||
class Request
|
||||
{
|
||||
// Required
|
||||
public $namespace;
|
||||
public $folder;
|
||||
|
||||
// Optional
|
||||
public $phpVersion = Generator::PHP71;
|
||||
public $filter;
|
||||
public $removePrefixEntity;
|
||||
}
|
Loading…
Reference in New Issue