Source of file MakeCrud.php

Size: 3,947 Bytes - Last Modified: 2023-11-16T22:56:03+01:00

/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/make-bundle/src/Maker/MakeCrud.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
<?php
namespace TIC\MakeBundle\Maker;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Doctrine\Common\Annotations\Annotation;

use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;

use TIC\MakeBundle\Renderer\FormTypeRenderer;

/**
 *
 */
final class MakeCrud extends BaseMaker
{
	private $doctrineHelper;
	private $formTypeRenderer;

	public function __construct(DoctrineHelper $doctrineHelper, FormTypeRenderer $formTypeRenderer)
	{
		parent::__construct();
		$this->doctrineHelper   = $doctrineHelper;
		$this->formTypeRenderer = $formTypeRenderer;
	}

	protected $dependencies = array(
		['class' => Annotation::class, 'value' => 'doctrine/annotations'],
	);

	/**
	 * Configure the command: set description, input arguments, options, etc.
	 */
	protected function ticConfigure(Command $command)
	{
		$command->addArgument('name',      InputArgument::OPTIONAL, "Nom, chemin ou classname du controller CRUD");
		$command->addOption('split', null, InputOption::VALUE_NONE, "Séparer les actions create & update");
	}

	/**
	 * Called after normal code generation: allows you to do anything.
	 */
	protected function ticGenerate(InputInterface $input)
	{
		$inputName = $this->fixClassName($input->getArgument('name'));
		$this->io->title("Construction du controller CRUD : '$inputName'");
		
		// détermination des variables à utiliser
		$variables = $this->getVariablesFromName($inputName);
		$doctrineDetails = $this->doctrineHelper->createDoctrineDetails($variables['item_class']);
		$entityDetails   = new ClassNameDetails($variables['item_class'], '');
		$formDetails     = new ClassNameDetails($variables['form_class'], '');
		
		// affichage des valeurs des variables
		$this->io->section("Paramètres utilisés :");
		$this->io->table(["Variable","Valeur"], self::hash2list($variables));
		if (null === $doctrineDetails)
			throw new RuntimeCommandException("Entité introuvable : " . $variables['item_class']);
		if ($this->test) return;
		
		if (! $this->confirm("Lancer la construction ?")) return;
		$results = array();
		
		// construction Controller
		$this->io->section("Construction du controlleur...");
		$controllerPath = $this->ticGenerateController(
			$variables['ctrl_class'],
			self::$skelPath . 'crud/Controller.tpl.php',
			$variables + [
				'entity_key'    => $doctrineDetails->getIdentifier(),
				'split_form'    => $input->getOption('split'),
				'with_template' => $this->twig,
			]
		);
		$results[] = [ $variables['ctrl_class'], $controllerPath ];
		$this->io->success("Génération du controlleur : " . $variables['ctrl_class']);
		
		// construction FormType
		$this->io->section("Construction du formulaire...");
		$formPath = $this->ticGenerateFormType($this->formTypeRenderer, $formDetails, $doctrineDetails, $entityDetails);
		$results[] = [ $variables['form_class'], $formPath ];
		$this->io->success("Génération du FormType : " . $variables['form_class']);
		
		// construction Twig templates
		if ($this->twig) {
			$this->io->section("Construction des templates...");
			$tpls = ['base', 'list', '_list_table', 'show', '_show_main'];
			$tpls = array_merge($tpls, $input->getOption('split') ? ['create', 'update'] : ['form']);
			$paths = $this->ticGenerateTemplates('crud', $tpls, $variables + [
				'entity_key'    => $doctrineDetails->getIdentifier(),
				'entity_fields' => $doctrineDetails->getDisplayFields(),
				'ctrl_path'     => $controllerPath,
			]);
			$this->io->success("Génération des views twig : " . implode(", ", $tpls));
			foreach ($tpls as $id => $tpl) $results[] = [ "Template '$tpl'", $paths[$id] ];
		}
		
		// récapitulatif des fichiers générés
		return $this->summary($results);
	}

}