Source of file MakeController.php

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

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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
<?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;

/**
 *
 */
final class MakeController extends BaseMaker
{

	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");
		$command->addOption('crud',  null, InputOption::VALUE_NONE, "Ajouter les actions 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 : '$inputName'");
		
		// détermination des variables à utiliser
		$variables = $this->getVariablesFromName($inputName);
		$tplFolder = $input->getOption('crud') ? "crud" : "controller";
		
		// affichage des valeurs des variables
		$this->io->section("Paramètres utilisés :");
		$this->io->table(["Variable","Valeur"], self::hash2list($variables));
		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 . $tplFolder . '/Controller.tpl.php',
			$variables + [
				'entity_key'    => "id",
				'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 Twig templates
		if ($this->twig) {
			$this->io->section("Construction des templates...");
			$tpls = ['index'];
			if ($input->getOption('crud')) {
				$tpls = ['base', 'list', '_list_table', 'show', '_show_main'];
				$tpls = array_merge($tpls, $input->getOption('split') ? ['create', 'update'] : ['form']);
			}
			$paths = $this->ticGenerateTemplates($tplFolder, $tpls, $variables + [
				'entity_key'    => "id",
				'entity_fields' => ["id" => ["fieldName"=>"id", "type"=>"integer"]],
				'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);
	}

}