Skip to content
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

Generate service #77

Merged
merged 5 commits into from
Jun 10, 2014
Merged
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
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@
/box.phar
/console.phar

#Test
# Test
/phpunit.xml

# IDEs
.idea
/nbproject

# OS
.DS_Store
Thumbs.db
6 changes: 4 additions & 2 deletions bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Drupal\AppConsole\Command\GeneratorPluginBlockCommand;
use Drupal\AppConsole\Command\GeneratorCommandCommand;
use Drupal\AppConsole\Command\DrushCommand;
use Drupal\AppConsole\Command\GeneratorServiceCommand;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Finder\Finder;
Expand All @@ -34,13 +35,14 @@
'register_commands' => new RegisterCommandsHelper($application),
)));

$application->addCommands(array(
$application->addCommands([
new GeneratorModuleCommand(),
new GeneratorControllerCommand(),
new GeneratorFormCommand(),
new GeneratorPluginBlockCommand(),
new GeneratorCommandCommand(),
new DrushCommand(),
));
new GeneratorServiceCommand(),
]);

$application->run();
156 changes: 156 additions & 0 deletions lib/Drupal/AppConsole/Command/GeneratorServiceCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
*@file
* Contains \Drupal\AppConsole\Command\GeneratorServiceCommand.
*/

namespace Drupal\AppConsole\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\AppConsole\Generator\ServiceGenerator;
use Drupal\AppConsole\Command\Validators;

class GeneratorServiceCommand extends GeneratorCommand {

protected function configure() {
$this
->setDefinition(array(
new InputOption('module','',InputOption::VALUE_REQUIRED, 'The name of the module'),
new InputOption('service_name','',InputOption::VALUE_OPTIONAL, 'Service name'),
new InputOption('class_name','',InputOption::VALUE_OPTIONAL, 'Class name'),
new InputOption('services','',InputOption::VALUE_OPTIONAL, 'Load services'),
))
->setDescription('Generate service')
->setHelp('The <info>generate:service</info> command helps you generate a new service.')
->setName('generate:service');
}

/**
*
* @param InputInterface $input [description]
* @param OutputInterface $output [description]
* @return [type] [description]
*/
protected function execute(InputInterface $input, OutputInterface $output) {

$dialog = $this->getDialogHelper();

if ($input->isInteractive()) {
if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) {
$output->writeln('<error>Command aborted</error>');
return 1;
}
}

$module = $input->getOption('module');
$service_name = $input->getOption('service_name');
$class_name = $input->getOption('class_name');
$services = $input->getOption('services');

$map_service = [];
if (!empty($services)){
foreach ($services as $service) {
$class = get_class($this->getContainer()->get($service));
$map_service[$service] = array(
'name' => $service,
'machine_name' => str_replace('.', '_', $service),
'class' => $class,
'short' => end(explode('\\',$class))
);
}
}

$this
->getGenerator()
->generate($module, $service_name, $class_name, $map_service)
;
}

protected function interact(InputInterface $input, OutputInterface $output) {
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Welcome to the Drupal service generator');

$helper_set = $this->getHelperSet()->get('dialog');

// --module option
$module = $input->getOption('module');
if (!$module){
// Module names
$modules = $this->getModules();
$module = $helper_set->askAndValidate(
$output,
$dialog->getQuestion('Enter your module',''),
function($module) use ($modules){
return Validators::validateModuleExist($module, $modules);
},
false,
'',
$modules
);
}

$input->setOption('module', $module);

// --service_name option
$service_name = $input->getOption('service_name');
if (!$service_name){
$service_name = $dialog->ask($output, $dialog->getQuestion('Enter the service name', $module.'.default'), $module.'.default');
}
$input->setOption('service_name', $service_name);

// --class option
$class_name = $input->getOption('class_name');
if (!$class_name){
$class_name = $dialog->ask($output, $dialog->getQuestion('Enter the Class name', 'DefaultService'), 'DefaultService');
$input->setOption('class_name', $class_name);
}
$input->setOption('class_name', $class_name);

// --services option
if ($dialog->askConfirmation(
$output,
$dialog->getQuestion('Do you like add service(s)', 'yes', '?'),
true
)) {
$service_collection = array();
$services = $this->getServices();
$output->writeln([
'',
'You can add some services, type the name or use keyup and keydown',
'This is optional, press <info>enter</info> to <info>continue</info>',
''
]);

while(true){
$service = $helper_set->askAndValidate(
$output,
$dialog->getQuestion(' Enter your service',''),
function($service) use ($services){
return Validators::validateServiceExist($service, $services);
},
false,
null,
$services
);

if ($service == null) {
break;
}
array_push($service_collection, $service);
$service_key = array_search($service, $services, true);
if ($service_key >= 0)
unset($services[$service_key]);
}
$input->setOption('services', $service_collection);
}

}

protected function createGenerator() {
return new ServiceGenerator();
}

}
13 changes: 13 additions & 0 deletions lib/Drupal/AppConsole/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected function render($template, $parameters) {
));

$twig->addFunction($this->getServiceAsParamater());
$twig->addFunction($this->getServiceAsParamaterKeys());

return $twig->render($template, $parameters);
}
Expand Down Expand Up @@ -64,4 +65,16 @@ public function getServiceAsParamater() {
});
return $servicesAsParameters;
}

public function getServiceAsParamaterKeys() {
$servicesAsParametersKeys = new \Twig_SimpleFunction('servicesAsParametersKeys', function ($services) {
$parameters = [];
foreach ($services as $service) {
$parameters[] = sprintf('"@%s"', $service['name']);
}
return $parameters;
});
return $servicesAsParametersKeys;
}

}
60 changes: 60 additions & 0 deletions lib/Drupal/AppConsole/Generator/ServiceGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
*@file
* Contains \Drupal\AppConsole\Generator\ServiceGenerator.
*/

namespace Drupal\AppConsole\Generator;

class ServiceGenerator extends Generator {

/**
* Generator Plugin Block
* @param string $module Module name
* @param string $service_name Service name
* @param string $class_name Class name
* @param array $services list of services
*/
public function generate($module, $service_name, $class_name, $services) {

$module_path = DRUPAL_ROOT . '/' . drupal_get_path('module', $module);

// set syntax for arguments
$args = ', ';
$i = 0;
foreach ($services as $service) {
$args .= $service['short'] . ' $' . $service['machine_name'];
if ( ++$i != count($services)) {
$args .= ', ';
}
}

$parameters = [
'module' => $module,
'service_name' => $service_name,
'name' => [
'class' => $class_name,
'underscore' => $this->camelCaseToUnderscore($class_name)
],
'services' => $services,
'args' => $args
];

$parameters['file_exists'] = file_exists($module_path.'/'.$module.'.services.yml');

$this->renderFile(
'module/services.yml.twig',
$module_path.'/'.$module.'.services.yml',
$parameters,
FILE_APPEND
);

$this->renderFile(
'module/services.class.php.twig',
$module_path.'/src/'. $class_name .'.php',
$parameters
);

}

}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @file
* Contains \Drupal\{{module}}\{{ name.class }}.
*/

namespace Drupal\{{module}};

{% if services is not empty %}
{{ include ('skeleton/module/shared/services-use-operator.php.twig', {useContainerInterface: false}) }}
{% endif %}

class {{ name.class }}
{

{% if services is not empty %}
{% include 'skeleton/module/shared/services-class-properties-declaration.php.twig' %}
{% endif %}

public function __construct({{ servicesAsParameters(services)|join(', ') }})
{
{% include 'skeleton/module/shared/services-class-properties-initialization.php.twig' %}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% if service_name is defined %}
{% if not file_exists %}
services:
{% endif %}
{{ service_name | lower }}:
class: Drupal\{{module}}\{{ name.class }}
arguments: [{{ servicesAsParametersKeys(services)|join(', ') }}]
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% if useContainerInterface is not defined %}
{% set useContainerInterface = true %}
{% endif %}
{% if useContainerInterface %}
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% for service in services %}
use {{ service.class }};
{% endfor %}