Skip to content

Adding Path option to make:Controller Command #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 30 additions & 33 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bundle\MakerBundle;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;

Expand Down Expand Up @@ -85,22 +84,6 @@ public function dumpFile(string $targetPath, string $contents)
];
}

public function getFileContentsForPendingOperation(string $targetPath): string
{
if (!isset($this->pendingOperations[$targetPath])) {
throw new RuntimeCommandException(sprintf('File "%s" is not in the Generator\'s pending operations', $targetPath));
}

$templatePath = $this->pendingOperations[$targetPath]['template'];
$parameters = $this->pendingOperations[$targetPath]['variables'];

$templateParameters = array_merge($parameters, [
'relative_path' => $this->fileManager->relativizePath($targetPath),
]);

return $this->fileManager->parseTemplate($templatePath, $templateParameters);
}

/**
* Creates a helper object to get data about a class name.
*
Expand Down Expand Up @@ -152,6 +135,27 @@ public function createClassNameDetails(string $name, string $namespacePrefix, st
return new ClassNameDetails($className, $fullNamespacePrefix, $suffix);
}

public function createControllerClassNameDetails(string $name, string $namespacePrefix, string $suffix = '', $Infolder, string $validationErrorMessage = ''): ClassNameDetails
{
$fullNamespacePrefix = $this->namespacePrefix.'\\'.$namespacePrefix;
if ('\\' === $name[0]) {
// class is already "absolute" - leave it alone (but strip opening \)
$className = substr($name, 1);
} else {
$className = ($Infolder) ? rtrim($fullNamespacePrefix, '\\') . '\\' . $suffix . '\\' . Str::asClassName($name) : rtrim($fullNamespacePrefix, '\\').'\\'.Str::asClassName($name, $suffix) ;
}

Validator::validateClassName($className, $validationErrorMessage);

// if this is a custom class, we may be completely different than the namespace prefix
// the best way can do, is find the PSR4 prefix and use that
if (0 !== strpos($className, $fullNamespacePrefix)) {
$fullNamespacePrefix = $this->fileManager->getNamespacePrefixForClass($className);
}

return new ClassNameDetails($className, $fullNamespacePrefix, $suffix);
}

private function addOperation(string $targetPath, string $templateName, array $variables)
{
if ($this->fileManager->fileExists($targetPath)) {
Expand Down Expand Up @@ -195,10 +199,15 @@ public function writeChanges()
continue;
}

$this->fileManager->dumpFile(
$targetPath,
$this->getFileContentsForPendingOperation($targetPath, $templateData)
);
$templatePath = $templateData['template'];
$parameters = $templateData['variables'];

$templateParameters = array_merge($parameters, [
'relative_path' => $this->fileManager->relativizePath($targetPath),
]);

$fileContents = $this->fileManager->parseTemplate($templatePath, $templateParameters);
$this->fileManager->dumpFile($targetPath, $fileContents);
}

$this->pendingOperations = [];
Expand All @@ -208,16 +217,4 @@ public function getRootNamespace(): string
{
return $this->namespacePrefix;
}

public function generateController(string $controllerClassName, string $controllerTemplatePath, array $parameters = []): string
{
return $this->generateClass(
$controllerClassName,
$controllerTemplatePath,
$parameters +
[
'parent_class_name' => \method_exists(AbstractController::class, 'getParameter') ? 'AbstractController' : 'Controller',
]
);
}
}
26 changes: 18 additions & 8 deletions src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
namespace Symfony\Bundle\MakerBundle\Maker;

use Doctrine\Common\Annotations\Annotation;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\TwigBundle\TwigBundle;
Expand All @@ -38,31 +40,39 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
$command
->setDescription('Creates a new controller class')
->addArgument('controller-class', InputArgument::OPTIONAL, sprintf('Choose a name for your controller class (e.g. <fg=yellow>%sController</>)', Str::asClassName(Str::getRandomTerm())))
->addOption('path', null, InputOption::VALUE_OPTIONAL, 'Choose a path for your controller (e.g. <fg=yellow>"Foo/Bar"</>)')
->addOption('template', true, InputOption::VALUE_OPTIONAL, 'If this option is set to false we will skip template creation')
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))
;
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
{
$controllerClassNameDetails = $generator->createClassNameDetails(
$ControllerPathName = $input->getOption('path');
$Infolder = ($ControllerPathName) ? true : false;
$SuffixName = ($Infolder) ? str_replace('/',"\\",$ControllerPathName) : 'Controller';
$controllerClassNameDetails = $generator->createControllerClassNameDetails(
$input->getArgument('controller-class'),
'Controller\\',
'Controller'
$SuffixName,
$Infolder
);

$templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()).'/index.html.twig';
$controllerPath = $generator->generateController(
$templateName = ($Infolder) ?
Str::asFilePath($ControllerPathName).'/index.html.twig' :
Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()).'/index.html.twig';
$controllerPath = $generator->generateClass(
$controllerClassNameDetails->getFullName(),
'controller/Controller.tpl.php',
[
'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'parent_class_name' => \method_exists(AbstractController::class, 'getParameter') ? 'AbstractController' : 'Controller',
'route_path' => ($Infolder) ? str_replace("/controller","",Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix())) : Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'route_name' => ($Infolder) ? str_replace("_controller","",Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix())) : Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'twig_installed' => $this->isTwigInstalled(),
'template_name' => $templateName,
]
);

if ($this->isTwigInstalled()) {
if ($input->getOption('template') != false && $this->isTwigInstalled()) {
$generator->generateFile(
'templates/'.$templateName,
'controller/twig_template.tpl.php',
Expand Down