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

Work with handlers as service #12

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function registerBundles()
new FOS\RestBundle\FOSRestBundle(),
new JMS\SerializerBundle\JMSSerializerBundle($this),
new Nelmio\CorsBundle\NelmioCorsBundle(),
new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
//...
);
//...
Expand Down Expand Up @@ -73,6 +74,8 @@ sensio_framework_extra:
request: { converters: true }
view: { annotations: false }
router: { annotations: true }

nelmio_api_doc: ~
```

## Generating the Controller
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"php": ">=5.3.0",
"friendsofsymfony/rest-bundle": "1.4.*",
"jms/serializer-bundle": "0.13.*",
"nelmio/cors-bundle": "1.3.*"
"nelmio/cors-bundle": "1.3.*",
"nelmio/api-doc-bundle": "~2.7"
},
"autoload": {
"psr-0": {
Expand Down
168 changes: 117 additions & 51 deletions src/Voryx/RESTGeneratorBundle/Command/GenerateDoctrineRESTCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,39 @@ class GenerateDoctrineRESTCommand extends GenerateDoctrineCommand
protected function configure()
{
$this
->setDefinition(array(
new InputOption('entity', '', InputOption::VALUE_REQUIRED, 'The entity class name to initialize (shortcut notation)'),
new InputOption('route-prefix', '', InputOption::VALUE_REQUIRED, 'The route prefix'),
new InputOption('overwrite', '', InputOption::VALUE_NONE, 'Do not stop the generation if rest api controller already exist, thus overwriting all generated files'),
))
->setDefinition(
array(
new InputOption(
'entity',
'',
InputOption::VALUE_REQUIRED,
'The entity class name to initialize (shortcut notation)'
),
new InputOption('route-prefix', '', InputOption::VALUE_REQUIRED, 'The route prefix'),
new InputOption(
'overwrite',
'',
InputOption::VALUE_NONE,
'Do not stop the generation if rest api controller already exist, thus overwriting all generated files'
),
new InputOption(
'resource',
'',
InputOption::VALUE_NONE,
'The object will return with the resource name'
),
new InputOption(
'document',
'',
InputOption::VALUE_NONE,
'Use NelmioApiDocBundle to document the controller'
),
)
)
->setDescription('Generates a REST api based on a Doctrine entity')
->setHelp(<<<EOT
The <info>voryx:generate:rest</info> command generates a REST api based on a Doctrine entity.
->setHelp(
<<<EOT
The <info>voryx:generate:rest</info> command generates a REST api based on a Doctrine entity.

<info>php app/console voryx:generate:rest --entity=AcmeBlogBundle:Post --route-prefix=post_admin</info>

Expand All @@ -58,8 +83,7 @@ protected function configure()
EOT
)
->setName('voryx:generate:rest')
->setAliases(array('generate:voryx:rest'))
;
->setAliases(array('generate:voryx:rest'));
}

/**
Expand All @@ -70,7 +94,12 @@ 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)) {
if (!$dialog->askConfirmation(
$output,
$dialog->getQuestion('Do you confirm generation', 'yes', '?'),
true
)
) {
$output->writeln('<error>Command aborted</error>');

return 1;
Expand All @@ -85,12 +114,14 @@ protected function execute(InputInterface $input, OutputInterface $output)

$dialog->writeSection($output, 'REST api generation');

$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle).'\\'.$entity;
$metadata = $this->getEntityMetadata($entityClass);
$bundle = $this->getContainer()->get('kernel')->getBundle($bundle);
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle) . '\\' . $entity;
$metadata = $this->getEntityMetadata($entityClass);
$bundle = $this->getContainer()->get('kernel')->getBundle($bundle);
$resource = $input->getOption('resource');
$document = $input->getOption('document');

$generator = $this->getGenerator($bundle);
$generator->generate($bundle, $entity, $metadata[0], $prefix, $forceOverwrite);
$generator->generate($bundle, $entity, $metadata[0], $prefix, $forceOverwrite, $resource, $document);

$output->writeln('Generating the REST api code: <info>OK</info>');

Expand All @@ -115,41 +146,53 @@ protected function interact(InputInterface $input, OutputInterface $output)
$dialog->writeSection($output, 'Welcome to the Doctrine2 REST api generator');

// namespace
$output->writeln(array(
'',
'This command helps you generate a REST api controller.',
'',
'First, you need to give the entity for which you want to generate a REST api.',
'You can give an entity that does not exist yet and the wizard will help',
'you defining it.',
'',
'You must use the shortcut notation like <comment>AcmeBlogBundle:Post</comment>.',
'',
));

$entity = $dialog->askAndValidate($output, $dialog->getQuestion('The Entity shortcut name', $input->getOption('entity')), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateEntityName'), false, $input->getOption('entity'));
$output->writeln(
array(
'',
'This command helps you generate a REST api controller.',
'',
'First, you need to give the entity for which you want to generate a REST api.',
'You can give an entity that does not exist yet and the wizard will help',
'you defining it.',
'',
'You must use the shortcut notation like <comment>AcmeBlogBundle:Post</comment>.',
'',
)
);

$entity = $dialog->askAndValidate(
$output,
$dialog->getQuestion('The Entity shortcut name', $input->getOption('entity')),
array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateEntityName'),
false,
$input->getOption('entity')
);
$input->setOption('entity', $entity);
list($bundle, $entity) = $this->parseShortcutNotation($entity);

// route prefix
$prefix = $this->getRoutePrefix($input, $entity);
$output->writeln(array(
'',
'Determine the routes prefix (all the routes will be "mounted" under this',
'prefix: /prefix/, /prefix/new, ...).',
'',
));
$prefix = $dialog->ask($output, $dialog->getQuestion('Routes prefix', '/'.$prefix), '/'.$prefix);
$output->writeln(
array(
'',
'Determine the routes prefix (all the routes will be "mounted" under this',
'prefix: /prefix/, /prefix/new, ...).',
'',
)
);
$prefix = $dialog->ask($output, $dialog->getQuestion('Routes prefix', '/' . $prefix), '/' . $prefix);
$input->setOption('route-prefix', $prefix);

// summary
$output->writeln(array(
'',
$this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true),
'',
sprintf("You are going to generate a REST api controller for \"<info>%s:%s</info>\"", $bundle, $entity),
'',
));
$output->writeln(
array(
'',
$this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true),
'',
sprintf("You are going to generate a REST api controller for \"<info>%s:%s</info>\"", $bundle, $entity),
'',
)
);
}

/**
Expand All @@ -159,38 +202,61 @@ protected function generateForm($bundle, $entity, $metadata)
{
try {
$this->getFormGenerator($bundle)->generate($bundle, $entity, $metadata[0]);
} catch (\RuntimeException $e ) {
} catch (\RuntimeException $e) {
// form already exists
}
}

protected function updateRouting(DialogHelper $dialog, InputInterface $input, OutputInterface $output, BundleInterface $bundle, $entity, $prefix)
{
protected function updateRouting(
DialogHelper $dialog,
InputInterface $input,
OutputInterface $output,
BundleInterface $bundle,
$entity,
$prefix
) {
$auto = true;
if ($input->isInteractive()) {
$auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic update of the Routing', 'yes', '?'), true);
$auto = $dialog->askConfirmation(
$output,
$dialog->getQuestion('Confirm automatic update of the Routing', 'yes', '?'),
true
);
}

$output->write('Importing the REST api routes: ');
$this->getContainer()->get('filesystem')->mkdir($bundle->getPath().'/Resources/config/');
$routing = new RoutingManipulator($bundle->getPath().'/Resources/config/routing.yml');
$this->getContainer()->get('filesystem')->mkdir($bundle->getPath() . '/Resources/config/');
$routing = new RoutingManipulator($bundle->getPath() . '/Resources/config/routing.yml');
try {
// TODO: fix the format parameter - leaving it for now
$format = "annotation";
$ret = $auto ? $routing->addResource($bundle->getName(), $format, '/'.$prefix, 'routing/'.strtolower(str_replace('\\', '_', $entity))) : false;
$ret = $auto ? $routing->addResource(
$bundle->getName(),
$format,
'/' . $prefix,
'routing/' . strtolower(str_replace('\\', '_', $entity))
) : false;
} catch (\RuntimeException $exc) {
$ret = false;
}

if (!$ret) {
$help = sprintf(" <comment>resource: \"@%s/Resources/config/routing/%s.%s\"</comment>\n", $bundle->getName(), strtolower(str_replace('\\', '_', $entity)), $format);
$help = sprintf(
" <comment>resource: \"@%s/Resources/config/routing/%s.%s\"</comment>\n",
$bundle->getName(),
strtolower(str_replace('\\', '_', $entity)),
$format
);
$help .= sprintf(" <comment>prefix: /%s</comment>\n", $prefix);

return array(
'- Import the bundle\'s routing resource in the bundle routing file',
sprintf(' (%s).', $bundle->getPath().'/Resources/config/routing.yml'),
sprintf(' (%s).', $bundle->getPath() . '/Resources/config/routing.yml'),
'',
sprintf(' <comment>%s:</comment>', $bundle->getName().('' !== $prefix ? '_'.str_replace('/', '_', $prefix) : '')),
sprintf(
' <comment>%s:</comment>',
$bundle->getName() . ('' !== $prefix ? '_' . str_replace('/', '_', $prefix) : '')
),
$help,
'',
);
Expand Down
8 changes: 1 addition & 7 deletions src/Voryx/RESTGeneratorBundle/Controller/VoryxController.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<?php

namespace Voryx\RESTGeneratorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;

class VoryxController extends Controller
{

/**
* Create a form without a name
*
Expand All @@ -27,10 +23,8 @@ public function createForm($type = null, $data = null, array $options = array())
$data,
$options
);

return $form;
}

/**
* Get rid on any fields that don't appear in the form
*
Expand All @@ -44,4 +38,4 @@ protected function removeExtraFields(Request $request, Form $form)
$data = array_intersect_key($data, $children);
$request->request->replace($data);
}
}
}
Loading