Trinity updates

This commit is contained in:
Simon Vieille 2015-09-14 14:52:53 +02:00
parent 576a0d6b1a
commit a7a7b0e0da
3 changed files with 4252 additions and 0 deletions

View file

@ -0,0 +1,468 @@
<?php
namespace Trinity\Bundle\AdminBundle\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Filesystem\Exception\IOException;
use Sensio\Bundle\GeneratorBundle\Command\Validators;
class GenerateCrud27Command extends ContainerAwareCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setDefinition(array(
new InputOption('namespace', '', InputOption::VALUE_REQUIRED, 'The namespace of the bundle to create'),
new InputOption('bundle', '', InputOption::VALUE_REQUIRED, 'The bundle to generate model classes from'),
))
->setDescription('Generate basics files for the CRUD')
->setHelp(<<<EOT
EOT
)
->setName('generate:crud27')
;
}
/**
* @see Command
*
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getHelperSet()->get('dialog');
$filesystem = $this->getContainer()->get('filesystem');
$kernel = $this->getContainer()->get('kernel');
$output->writeln(array('<info>Welcome to the CRUD generation!</info>', ''));
$output->writeln(
array(
'',
'Use <comment> \ </comment> for the namespace delimiter to avoid any problem.',
'You can find the bundle name in the file <comment>app/AppKernel.php</comment> without <comment>()</comment>',
'',
)
);
$namespace = $dialog->askAndValidate(
$output,
sprintf('Bundle namespace [<comment>%s</comment>]: ', $input->getOption('namespace')),
array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleNamespace'),
false,
$input->getOption('namespace')
);
$output->writeln(
array(
'',
'',
'You can find the bundle name in the file <comment>app/config/routing.yml</comment>',
'Example: AcmeMyBundle',
'',
)
);
$bundle = $dialog->askAndValidate(
$output,
sprintf('Bundle name [<comment>%s</comment>]: ', $input->getOption('bundle')),
array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleName'),
false,
$input->getOption('bundle')
);
$namespace = trim($namespace, '\\');
$input->setOption('bundle', $bundle);
$input->setOption('namespace', $namespace);
$kernel->getBundle($bundle);
$namespaceExplode = explode('\\', $namespace);
/* ------------------------------------------ */
// target dir
$path = dirname($this->getContainer()->getParameter('kernel.root_dir')).'/src'.DIRECTORY_SEPARATOR;
$path .= implode(DIRECTORY_SEPARATOR, $namespaceExplode);
if (!$filesystem->exists($path)) {
$output->writeln(array(
'',
sprintf('<error>Command aborted. The directory %s does not exist.</error>', $path),
'',
));
return 1;
}
$output->writeln(array('', 'Step 1: <info>Generate CrudConfiguration</info>', ''));
$model = str_replace($namespaceExplode[0], '', $bundle);
$model = str_replace('Bundle', '', $model);
$model = $dialog->askAndValidate(
$output,
sprintf('Model [<comment>%s</comment>]: ', $model),
function ($anwser) use ($namespace) {
$className = $namespace.'\\Model\\'.$anwser;
if (!class_exists($className)) {
throw new \RunTimeException(sprintf('Invalid model %s', $className));
}
return $anwser;
},
false,
$model
);
$classNameConfiguration = $model.'CrudConfiguration';
$nameSpaceConfiguration = $namespace.'\Configuration';
$modelPeer = $model.'Peer';
$modelQuery = $model.'Query';
$routePrefix = $bundle.$model.'Admin';
$formNamespace = '';
$modelNamespace = '';
$class = '';
foreach ($namespaceExplode as $dir) {
$formNamespace .= $dir."\\\\";
$modelNamespace .= $dir."\\\\";
$class .= $dir."\\";
}
$formNamespace .= 'Form\\Type';
$modelNamespace .= 'Model';
$class .= 'Model\\'.$model;
$formEditNew = $model.'Type';
$formFilter = $model.'FilterType';
$maxPerPage = $dialog->askAndValidate(
$output,
sprintf('Max per page [<comment>%d</comment>]: ', 20),
function ($answer) {
if (!is_numeric($answer)) {
throw new \RunTimeException(
'The value must be an integer'
);
}
return (int) $answer;
},
false,
20
);
$rankable = $dialog->askConfirmation(
$output,
sprintf('Rankable? [<comment>No</comment>]: '),
false
);
$rankable = !$rankable ? 'false' : 'true';
$contentConfiguration = "<?php
namespace ".$nameSpaceConfiguration.";
class ".$classNameConfiguration." extends \\Trinity\\Bundle\\AdminBundle\\Configuration\\CrudConfiguration
{
public function __construct()
{
\$this
->setModelNamespace('".$modelNamespace."')
->setModel('".$model."')
->setModelPeer('".$modelPeer."')
->setModelQuery('".$modelQuery."')
->setRoutePrefix('".$routePrefix."')
->setFormNamespace('".$formNamespace."')
->setFormEdit('".$formEditNew."')
->setFormFilter('".$formFilter."')
->setFormNew('".$formEditNew."')
->setFieldsets(array(
'' => array(
'*',
),
))
->setMaxPerPage(".$maxPerPage.")
->setRankable(".$rankable.")";
if ($rankable === 'true') {
$contentConfiguration .= "
->setSort('orderByRank', \\Criteria::ASC)
";
}
$contentConfiguration .= "
->setListTitle('".$model."')
->setNewTitle('New ".strtolower($model)."')
->setEditTitle('Edition of \"%id%\"')
// Listing:
// ->setFieldTemplate('visible', 'TrinityAdminBundle:BaseAdmin:bool.html.twig')
// ->setDisplayFields(array(
//
// ))
// ->setFieldname('foo', 'bar')
;
}
}
";
$output->writeln($contentConfiguration);
if (!$dialog->askConfirmation($output, 'Do you agree? [<comment>Yes</comment>]: ', true)) {
$output->writeln(['', '<error>Command aborted</error>']);
return 1;
}
// on regarde si le dossier configuration existe
$pathConfiguration = $path.DIRECTORY_SEPARATOR.'Configuration';
$configurationFile = $pathConfiguration.DIRECTORY_SEPARATOR.$classNameConfiguration.'.php';
try {
$filesystem->mkdir($pathConfiguration, 0777);
} catch (\IOException $e) {
$output->writeln(['', '<error>$e</error>']);
return 1;
}
if ($filesystem->exists($configurationFile)) {
$output->writeln(['', '<info>The file '.$configurationFile.'</info> already exists.']);
if ($dialog->askConfirmation($output, 'Do you want to override? [<comment>No</comment>]: ', false)) {
try {
$filesystem->remove($configurationFile);
file_put_contents($configurationFile, $contentConfiguration);
} catch (\IOException $e) {
$output->writeln(['', '<error>$e</error>']);
return 1;
}
}
} else {
try {
file_put_contents($configurationFile, $contentConfiguration);
} catch (\IOException $e) {
$output->writeln(['', '<error>$e</error>']);
return 1;
}
}
$output->writeln(array('', 'Step 2: <info>Generate Controller</info>', ''));
$classNameController = $model.'AdminController';
$nameSpaceController = $namespace.'\Controller';
$contentController = '<?php
namespace '.$nameSpaceController.';
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Trinity\Bundle\AdminBundle\Controller\BaseAdminController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use '.$nameSpaceConfiguration.'\\'.$classNameConfiguration.' as CrudConfiguration;
/**
* @Route("/admin/'.strtolower($model).'")
*/
class '.$classNameController.' extends BaseAdminController
{
public function __construct()
{
$this->configuration = new CrudConfiguration();
}
/**
* @Route("/{page}", name="'.$routePrefix.'_index", defaults={"page" = "1"}, requirements={"page" = "\d+"})
* @Template()
*/
public function indexAction($page, Request $request)
{
return parent::indexAction($page, $request);
}
/**
* @Route("/new", name="'.$routePrefix.'_new")
* @Template()
*/
public function newAction(Request $request)
{
return parent::newAction($request);
}
/**
* @Route("/edit/{id}", name="'.$routePrefix.'_edit")
* @Template()
* @ParamConverter("object", class="'.$class.'")
*/
public function editAction($object, Request $request)
{
return parent::editAction($object, $request);
}
/**
* @Route("/remove/{id}/{token}", name="'.$routePrefix.'_remove")
* @Template()
* @ParamConverter("object", class="'.$class.'")
*/
public function removeAction($object, $token, Request $request)
{
return parent::removeAction($object, $token, $request);
}
/**
* @Route("/batch", name="'.$routePrefix.'_batch")
* @Template()
* @Method({"POST"})
*/
public function batchAction(Request $request)
{
return parent::batchAction($request);
}
/**
* @Route("/filter/clear", name="'.$routePrefix.'_filter_clear")
* @Template()
*/
public function clearFilterAction(Request $request)
{
return parent::clearFilterAction($request);
}';
if ($rankable) {
$contentController .= '
/**
* @Route("/rank", name="'.$routePrefix.'_rank")
* @Template("TrinityAdminBundle:BaseAdmin:rank.html.twig")
* @Method({"POST"})
*/
public function rankAction(Request $request)
{
return parent::rankAction($request);
}';
}
$contentController .= '
}
';
$output->writeln($contentController);
if (!$dialog->askConfirmation($output, 'Do you agree? [<comment>Yes</comment>]: ', true)) {
$output->writeln(['', '<error>Command aborted</error>']);
return 1;
}
$pathController = $path.DIRECTORY_SEPARATOR.'Controller';
$controllerFile = $pathController.DIRECTORY_SEPARATOR.$classNameController.'.php';
try {
$filesystem->mkdir($pathController, 0777);
} catch (\IOException $e) {
$output->writeln(['', '<error>$e</error>']);
return 1;
}
if ($filesystem->exists($controllerFile)) {
$output->writeln(['', '<info>The file '.$controllerFile.'</info> already exists.']);
if ($dialog->askConfirmation($output, 'Do you want to override? [<comment>No</comment>]: ', false)) {
try {
$filesystem->remove($controllerFile);
file_put_contents($controllerFile, $contentController);
} catch (\IOException $e) {
$output->writeln(['', '<error>$e</error>']);
return 1;
}
}
} else {
try {
$filesystem->touch($controllerFile, 0777);
file_put_contents($controllerFile, $contentController);
} catch (\IOException $e) {
$output->writeln(['', '<error>$e</error>']);
return 1;
}
}
$output->writeln(array('','Step 3: <info>Generate views</info>',''));
$pathViews = $path.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.$model.'Admin';
try {
$filesystem->mkdir($pathViews, 0777);
} catch (\IOException $e) {
$output->writeln(['', '<error>$e</error>']);
return 1;
}
foreach (['index', 'edit', 'new'] as $action) {
$file = $pathViews.DIRECTORY_SEPARATOR.$action.'.html.twig';
if ($filesystem->exists($file)) {
$output->writeln(['', '<info>The file '.$file.'</info> already exists.']);
if ($dialog->askConfirmation($output, 'Do you want to override? [<comment>No</comment>]: ', false)) {
try {
$filesystem->touch($file, 0777);
file_put_contents($file, '{% extends "TrinityAdminBundle:BaseAdmin:'.$action.'.html.twig" %}');
} catch (\IOException $e) {
$output->writeln(['', '<error>$e</error>']);
return 1;
}
}
} else {
try {
$filesystem->touch($file, 0777);
file_put_contents($file, '{% extends "TrinityAdminBundle:BaseAdmin:'.$action.'.html.twig" %}');
} catch (\IOException $e) {
$output->writeln(['', '<error>$e</error>']);
return 1;
}
}
}
$output->writeln(['', '<info>Finished!</info>']);
}
}

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 509 B