Skip to content

Commit a99c3f6

Browse files
committed
First import
1 parent c243d63 commit a99c3f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2375
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
phpunit.xml
3+
vendor/

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2004-2017 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "symfony/maker-bundle",
3+
"type": "symfony-bundle",
4+
"license": "MIT",
5+
"keywords": ["generator", "code generator", "scaffolding", "scaffold"],
6+
"authors": [
7+
{
8+
"name": "Symfony Community",
9+
"homepage": "https://symfony.com/contributors"
10+
}
11+
],
12+
"minimum-stability": "beta",
13+
"require": {
14+
"php": "^7.1.3",
15+
"symfony/config": "^3.4|^4.0",
16+
"symfony/console": "^3.4|^4.0",
17+
"symfony/dependency-injection": "^3.4|^4.0",
18+
"symfony/filesystem": "^3.4|^4.0",
19+
"symfony/framework-bundle": "^3.4|^4.0",
20+
"symfony/http-kernel": "^3.4|^4.0"
21+
},
22+
"require-dev": {
23+
"symfony/phpunit-bridge": "^3.4|^4.0",
24+
"symfony/process": "^3.4|^4.0"
25+
},
26+
"config": {
27+
"preferred-install": {
28+
"*": "dist"
29+
},
30+
"sort-packages": true
31+
},
32+
"autoload": {
33+
"psr-4": { "Symfony\\Bundle\\MakerBundle\\": "src/" }
34+
},
35+
"autoload-dev": {
36+
"psr-4": { "Symfony\\Bundle\\MakerBundle\\Tests\\": "tests/" }
37+
}
38+
}

phpunit.xml.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
bootstrap="vendor/autoload.php"
9+
>
10+
<php>
11+
<ini name="error_reporting" value="-1" />
12+
</php>
13+
14+
<testsuites>
15+
<testsuite name="Project Test Suite">
16+
<directory>tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
</phpunit>

src/Command/AbstractCommand.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Command;
13+
14+
use Symfony\Bundle\MakerBundle\ConsoleStyle;
15+
use Symfony\Bundle\MakerBundle\DependencyBuilder;
16+
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
17+
use Symfony\Bundle\MakerBundle\Generator;
18+
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
use Symfony\Flex\Recipe;
22+
23+
/**
24+
* @author Javier Eguiluz <[email protected]>
25+
* @author Ryan Weaver <[email protected]>
26+
*/
27+
abstract class AbstractCommand extends Command
28+
{
29+
/** @var ConsoleStyle */
30+
protected $io;
31+
/** @var InputInterface */
32+
protected $input;
33+
private $generator;
34+
private $checkDependencies = true;
35+
private $nonInteractiveArguments = [];
36+
37+
public function __construct(Generator $generator)
38+
{
39+
parent::__construct();
40+
$this->generator = $generator;
41+
}
42+
43+
/**
44+
* Returns the parameters used to parse the file templates, to generate the
45+
* file names, etc.
46+
*/
47+
abstract protected function getParameters() : array;
48+
49+
/**
50+
* Returns the list of files to generate and the templates used to do that.
51+
*/
52+
abstract protected function getFiles(array $params) : array;
53+
54+
/**
55+
* Override to add a final "next steps" message.
56+
*
57+
* @param array $params
58+
* @param ConsoleStyle $io
59+
*/
60+
abstract protected function writeNextStepsMessage(array $params, ConsoleStyle $io);
61+
62+
abstract protected function configureDependencies(DependencyBuilder $dependencies);
63+
64+
/**
65+
* Call in configure() to disable the automatic interactive prompt for an arg.
66+
*
67+
* @param string $argumentName
68+
*/
69+
protected function setArgumentAsNonInteractive($argumentName)
70+
{
71+
$this->nonInteractiveArguments[] = $argumentName;
72+
}
73+
74+
protected function initialize(InputInterface $input, OutputInterface $output)
75+
{
76+
$this->io = new ConsoleStyle($input, $output);
77+
$this->input = $input;
78+
79+
if ($this->checkDependencies) {
80+
if (!class_exists(Recipe::class)) {
81+
throw new RuntimeCommandException(sprintf('The generator commands require your app to use Symfony Flex & a Flex directory structure. See https://symfony.com/doc/current/setup/flex.html'));
82+
}
83+
84+
$dependencies = new DependencyBuilder();
85+
$this->configureDependencies($dependencies);
86+
if ($missingPackages = $dependencies->getMissingDependencies()) {
87+
throw new RuntimeCommandException(sprintf("Missing package%s: to use the %s command, run: \n\ncomposer require %s", count($missingPackages) === 1 ? '' : 's', $this->getName(), implode(' ', $missingPackages)));
88+
}
89+
}
90+
}
91+
92+
protected function interact(InputInterface $input, OutputInterface $output)
93+
{
94+
foreach ($this->getDefinition()->getArguments() as $argument) {
95+
if ($input->getArgument($argument->getName())) {
96+
continue;
97+
}
98+
99+
if (in_array($argument->getName(), $this->nonInteractiveArguments)) {
100+
continue;
101+
}
102+
103+
$value = $this->io->ask($argument->getDescription(), $argument->getDefault());
104+
$input->setArgument($argument->getName(), $value);
105+
}
106+
}
107+
108+
protected function execute(InputInterface $input, OutputInterface $output)
109+
{
110+
$this->generator->setIO($this->io);
111+
$params = $this->getParameters();
112+
$this->generator->generate($params, $this->getFiles($params));
113+
114+
$this->io->writeln('');
115+
$this->io->writeln(' <bg=green;fg=white> </>');
116+
$this->io->writeln(' <bg=green;fg=white> Success! </>');
117+
$this->io->writeln(' <bg=green;fg=white> </>');
118+
$this->io->writeln('');
119+
120+
$this->writeNextStepsMessage($params, $this->io);
121+
}
122+
123+
/**
124+
* @internal Used for testing commands
125+
*/
126+
public function setCheckDependencies(bool $checkDeps)
127+
{
128+
$this->checkDependencies = $checkDeps;
129+
}
130+
131+
/**
132+
* @internal Used for testing commands
133+
*/
134+
public function setGenerator(Generator $generator)
135+
{
136+
$this->generator = $generator;
137+
}
138+
}

src/Command/MakeCommandCommand.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Command;
13+
14+
use Symfony\Bundle\MakerBundle\ConsoleStyle;
15+
use Symfony\Bundle\MakerBundle\DependencyBuilder;
16+
use Symfony\Bundle\MakerBundle\Str;
17+
use Symfony\Bundle\MakerBundle\Validator;
18+
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Output\OutputInterface;
22+
23+
/**
24+
* @author Javier Eguiluz <[email protected]>
25+
* @author Ryan Weaver <[email protected]>
26+
*/
27+
final class MakeCommandCommand extends AbstractCommand
28+
{
29+
protected static $defaultName = 'make:command';
30+
31+
public function configure()
32+
{
33+
$this
34+
->setDescription('Creates a new console command class')
35+
->addArgument('name', InputArgument::OPTIONAL, sprintf('Choose a command name (e.g. <fg=yellow>app:%s</>)', Str::asCommand(Str::getRandomTerm())))
36+
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeCommand.txt'))
37+
;
38+
}
39+
40+
protected function getParameters(): array
41+
{
42+
$commandName = trim($this->input->getArgument('name'));
43+
$commandClassName = Str::asClassName($commandName, 'Command');
44+
Validator::validateClassName($commandClassName, sprintf('The "%s" command name is not valid because it would be implemented by "%s" class, which is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores).', $commandName, $commandClassName));
45+
46+
return [
47+
'command_name' => $commandName,
48+
'command_class_name' => $commandClassName,
49+
];
50+
}
51+
52+
protected function getFiles(array $params): array
53+
{
54+
return [
55+
__DIR__.'/../Resources/skeleton/command/Command.php.txt' => 'src/Command/'.$params['command_class_name'].'.php'
56+
];
57+
}
58+
59+
protected function writeNextStepsMessage(array $params, ConsoleStyle $io)
60+
{
61+
$io->text([
62+
'Next: open your new command class and customize it!',
63+
'Find the documentation at <fg=yellow>https://symfony.com/doc/current/console.html</>'
64+
]);
65+
}
66+
67+
protected function configureDependencies(DependencyBuilder $dependencies)
68+
{
69+
$dependencies->addClassDependency(Command::class, [
70+
'console'
71+
]);
72+
}
73+
}

src/Command/MakeControllerCommand.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Command;
13+
14+
use Symfony\Bundle\MakerBundle\ConsoleStyle;
15+
use Symfony\Bundle\MakerBundle\DependencyBuilder;
16+
use Symfony\Bundle\MakerBundle\Str;
17+
use Symfony\Bundle\MakerBundle\Validator;
18+
use Symfony\Component\Console\Input\InputArgument;
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
22+
23+
/**
24+
* @author Javier Eguiluz <[email protected]>
25+
* @author Ryan Weaver <[email protected]>
26+
*/
27+
final class MakeControllerCommand extends AbstractCommand
28+
{
29+
protected static $defaultName = 'make:controller';
30+
31+
public function configure()
32+
{
33+
$this
34+
->setDescription('Creates a new controller class')
35+
->addArgument('controller-class', InputArgument::OPTIONAL, sprintf('Choose a name for your controller class (e.g. <fg=yellow>%sController</>)', Str::asClassName(Str::getRandomTerm())))
36+
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))
37+
;
38+
}
39+
40+
protected function getParameters(): array
41+
{
42+
$controllerClassName = Str::asClassName($this->input->getArgument('controller-class'), 'Controller');
43+
Validator::validateClassName($controllerClassName);
44+
45+
return [
46+
'controller_class_name' => $controllerClassName,
47+
'route_path' => Str::asRoutePath(str_replace('Controller', '', $controllerClassName)),
48+
'route_name' => Str::asRouteNAme(str_replace('Controller', '', $controllerClassName))
49+
];
50+
}
51+
52+
protected function getFiles(array $params): array
53+
{
54+
return [
55+
__DIR__.'/../Resources/skeleton/controller/Controller.php.txt' => 'src/Controller/'.$params['controller_class_name'].'.php'
56+
];
57+
}
58+
59+
protected function getResultMessage(array $params): string
60+
{
61+
return sprintf('%s created!', $params['controller_class_name']);
62+
}
63+
64+
protected function writeNextStepsMessage(array $params, ConsoleStyle $io)
65+
{
66+
$io->text('Next: Open your new controller class and add some pages!');
67+
}
68+
69+
protected function configureDependencies(DependencyBuilder $dependencies)
70+
{
71+
$dependencies->addClassDependency(
72+
Route::class,
73+
'annotations'
74+
);
75+
}
76+
}

0 commit comments

Comments
 (0)