Skip to content

Commit

Permalink
First import
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Nov 8, 2017
1 parent c243d63 commit a99c3f6
Show file tree
Hide file tree
Showing 53 changed files with 2,375 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2004-2017 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
38 changes: 38 additions & 0 deletions composer.json
@@ -0,0 +1,38 @@
{
"name": "symfony/maker-bundle",
"type": "symfony-bundle",
"license": "MIT",
"keywords": ["generator", "code generator", "scaffolding", "scaffold"],
"authors": [
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"minimum-stability": "beta",
"require": {
"php": "^7.1.3",
"symfony/config": "^3.4|^4.0",
"symfony/console": "^3.4|^4.0",
"symfony/dependency-injection": "^3.4|^4.0",
"symfony/filesystem": "^3.4|^4.0",
"symfony/framework-bundle": "^3.4|^4.0",
"symfony/http-kernel": "^3.4|^4.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^3.4|^4.0",
"symfony/process": "^3.4|^4.0"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": { "Symfony\\Bundle\\MakerBundle\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Symfony\\Bundle\\MakerBundle\\Tests\\": "tests/" }
}
}
19 changes: 19 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
138 changes: 138 additions & 0 deletions src/Command/AbstractCommand.php
@@ -0,0 +1,138 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle\Command;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Flex\Recipe;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Ryan Weaver <weaverryan@gmail.com>
*/
abstract class AbstractCommand extends Command
{
/** @var ConsoleStyle */
protected $io;
/** @var InputInterface */
protected $input;
private $generator;
private $checkDependencies = true;
private $nonInteractiveArguments = [];

public function __construct(Generator $generator)
{
parent::__construct();
$this->generator = $generator;
}

/**
* Returns the parameters used to parse the file templates, to generate the
* file names, etc.
*/
abstract protected function getParameters() : array;

/**
* Returns the list of files to generate and the templates used to do that.
*/
abstract protected function getFiles(array $params) : array;

/**
* Override to add a final "next steps" message.
*
* @param array $params
* @param ConsoleStyle $io
*/
abstract protected function writeNextStepsMessage(array $params, ConsoleStyle $io);

abstract protected function configureDependencies(DependencyBuilder $dependencies);

/**
* Call in configure() to disable the automatic interactive prompt for an arg.
*
* @param string $argumentName
*/
protected function setArgumentAsNonInteractive($argumentName)
{
$this->nonInteractiveArguments[] = $argumentName;
}

protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->io = new ConsoleStyle($input, $output);
$this->input = $input;

if ($this->checkDependencies) {
if (!class_exists(Recipe::class)) {
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'));
}

$dependencies = new DependencyBuilder();
$this->configureDependencies($dependencies);
if ($missingPackages = $dependencies->getMissingDependencies()) {
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)));
}
}
}

protected function interact(InputInterface $input, OutputInterface $output)
{
foreach ($this->getDefinition()->getArguments() as $argument) {
if ($input->getArgument($argument->getName())) {
continue;
}

if (in_array($argument->getName(), $this->nonInteractiveArguments)) {
continue;
}

$value = $this->io->ask($argument->getDescription(), $argument->getDefault());
$input->setArgument($argument->getName(), $value);
}
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->generator->setIO($this->io);
$params = $this->getParameters();
$this->generator->generate($params, $this->getFiles($params));

$this->io->writeln('');
$this->io->writeln(' <bg=green;fg=white> </>');
$this->io->writeln(' <bg=green;fg=white> Success! </>');
$this->io->writeln(' <bg=green;fg=white> </>');
$this->io->writeln('');

$this->writeNextStepsMessage($params, $this->io);
}

/**
* @internal Used for testing commands
*/
public function setCheckDependencies(bool $checkDeps)
{
$this->checkDependencies = $checkDeps;
}

/**
* @internal Used for testing commands
*/
public function setGenerator(Generator $generator)
{
$this->generator = $generator;
}
}
73 changes: 73 additions & 0 deletions src/Command/MakeCommandCommand.php
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle\Command;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Ryan Weaver <weaverryan@gmail.com>
*/
final class MakeCommandCommand extends AbstractCommand
{
protected static $defaultName = 'make:command';

public function configure()
{
$this
->setDescription('Creates a new console command class')
->addArgument('name', InputArgument::OPTIONAL, sprintf('Choose a command name (e.g. <fg=yellow>app:%s</>)', Str::asCommand(Str::getRandomTerm())))
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeCommand.txt'))
;
}

protected function getParameters(): array
{
$commandName = trim($this->input->getArgument('name'));
$commandClassName = Str::asClassName($commandName, 'Command');
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));

return [
'command_name' => $commandName,
'command_class_name' => $commandClassName,
];
}

protected function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/command/Command.php.txt' => 'src/Command/'.$params['command_class_name'].'.php'
];
}

protected function writeNextStepsMessage(array $params, ConsoleStyle $io)
{
$io->text([
'Next: open your new command class and customize it!',
'Find the documentation at <fg=yellow>https://symfony.com/doc/current/console.html</>'
]);
}

protected function configureDependencies(DependencyBuilder $dependencies)
{
$dependencies->addClassDependency(Command::class, [
'console'
]);
}
}
76 changes: 76 additions & 0 deletions src/Command/MakeControllerCommand.php
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle\Command;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Ryan Weaver <weaverryan@gmail.com>
*/
final class MakeControllerCommand extends AbstractCommand
{
protected static $defaultName = 'make:controller';

public function configure()
{
$this
->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())))
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))
;
}

protected function getParameters(): array
{
$controllerClassName = Str::asClassName($this->input->getArgument('controller-class'), 'Controller');
Validator::validateClassName($controllerClassName);

return [
'controller_class_name' => $controllerClassName,
'route_path' => Str::asRoutePath(str_replace('Controller', '', $controllerClassName)),
'route_name' => Str::asRouteNAme(str_replace('Controller', '', $controllerClassName))
];
}

protected function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/controller/Controller.php.txt' => 'src/Controller/'.$params['controller_class_name'].'.php'
];
}

protected function getResultMessage(array $params): string
{
return sprintf('%s created!', $params['controller_class_name']);
}

protected function writeNextStepsMessage(array $params, ConsoleStyle $io)
{
$io->text('Next: Open your new controller class and add some pages!');
}

protected function configureDependencies(DependencyBuilder $dependencies)
{
$dependencies->addClassDependency(
Route::class,
'annotations'
);
}
}

0 comments on commit a99c3f6

Please sign in to comment.