Skip to content

Commit

Permalink
fixed error with using tags, improved avro templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jdewit committed May 10, 2012
1 parent a8a847c commit 808270d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 25 deletions.
51 changes: 36 additions & 15 deletions Command/GenerateCommand.php
Expand Up @@ -65,15 +65,21 @@ protected function execute(InputInterface $input, OutputInterface $output)

$dialog->writeSection($output, 'Welcome to the Avro code generator!');

$output->writeln(array(
'Enter the name of the entity you wish to create code for.',
'(ex. AvroDemoBundle:Blog)',
'',
'If you wish to generate code for all entities in the bundle,',
'enter just the bundle name. (ex. AvroDemoBundle)'
));
$fromEntity = $this->dialog->askConfirmation($this->output, $this->dialog->getQuestion('Would you like to generate code based on an entity?', 'yes', '?'), true);

$input = $dialog->ask($output, $dialog->getQuestion('Bundle name with or without entity name', '', ':'));
if ($fromEntity) {
$output->writeln(array(
'Enter the name of the entity you wish to create code for.',
'(ex. AvroDemoBundle:Blog)',
'',
'If you wish to generate code for all entities in the bundle,',
'enter just the bundle name. (ex. AvroDemoBundle)'
));

$input = $dialog->ask($output, $dialog->getQuestion('Bundle name with or without entity name', '', ':'));
} else {
$input = $dialog->ask($output, $dialog->getQuestion('Enter the bundle name', '', ':'));
}

list($bundleName, $entities) = $this->parseShortcutNotation($input);

Expand All @@ -90,7 +96,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
, '', ':'));

if ($bundleExists) {
$this->generateCodeFromEntities($bundle, $entities, $tag);
if ($fromEntity) {
$this->generateCodeFromEntities($bundle, $entities, $tag);
} else {
$this->generateStandaloneFiles($bundle, $tag);
}
} else {
$this->generateBundle($bundleName, $tag);
}
Expand Down Expand Up @@ -134,8 +144,8 @@ public function generateCodeFromEntities($bundle, $entities, $tag)

//Generate Bundle/Entity files
$avroGenerator = new Generator($this->container, $this->output);
$avroGenerator->generateBundleParameters($bundle->getName());
$avroGenerator->generateEntityParameters($entity, $fields);
$avroGenerator->initializeBundleParameters($bundle->getName());
$avroGenerator->initializeEntityParameters($entity, $fields);

$files = $this->container->getParameter('avro_generator.files');

Expand All @@ -150,8 +160,19 @@ public function generateCodeFromEntities($bundle, $entities, $tag)
}
}
}

}
}

/*
* Generate Standalone Files
*
* @param $bundle
* @param $tag
*/
public function generateStandaloneFiles($bundle, $tag)
{
$avroGenerator = new Generator($this->container, $this->output);
$avroGenerator->initializeBundleParameters($bundle->getName());

$standaloneFiles = $this->container->getParameter('avro_generator.standalone_files');
if (is_array($standaloneFiles)) {
Expand Down Expand Up @@ -454,12 +475,12 @@ public function generateBundle($bundleName, $tag)
$bundlePath = $this->getContainer()->getParameter('kernel.root_dir').'/../vendor/'.lcfirst($vendor).'/'.strtolower(str_replace('Bundle', '', $basename).'-bundle').'/'.$vendor.'/'.$basename.'/';

$generator = new Generator($this->container, $this->output);
$generator->generateBundleParameters($vendor.$basename);
$generator->initializeBundleParameters($vendor.$basename);

$folders = $this->container->getParameter('avro_generator.bundle_folders');
if (is_array($folders)) {
foreach($folders as $folder) {
if ($tag) {
if ($tag && array_key_exists('tags', $folder)) {
if (in_array($tag, $folder['tags'])) {
$generator->renderFolder($bundlePath.$folder['path']);
}
Expand All @@ -472,7 +493,7 @@ public function generateBundle($bundleName, $tag)
$files = $this->container->getParameter('avro_generator.bundle_files');
if (is_array($files)) {
foreach($files as $file) {
if ($tag) {
if ($tag && array_key_exists('tags', $file)) {
if (in_array($tag, $file['tags'])) {
$generator->renderFile($file['template'], $file['filename']);
}
Expand Down
8 changes: 4 additions & 4 deletions Generator/Generator.php
Expand Up @@ -59,11 +59,11 @@ public function setParameters(array $parameters)
}

/*
* Generate bundle parameters
* Initialize bundle parameters
*
* @param string $bundleName
*/
public function generateBundleParameters($bundleName)
public function initializeBundleParameters($bundleName)
{
$arr = preg_split('/(?<=[a-z])(?=[A-Z])/x',$bundleName);

Expand All @@ -90,12 +90,12 @@ public function generateBundleParameters($bundleName)
}

/*
* Generate parameters
* Initialize parameters
*
* @param string $entity The entity name
* @param array $fields Array of the entities fields
*/
public function generateEntityParameters($entity, $fields)
public function initializeEntityParameters($entity, $fields)
{
$parameters = array(
'entity' => $entity,
Expand Down
34 changes: 33 additions & 1 deletion Resources/config/templates/avro.yml
Expand Up @@ -109,6 +109,7 @@ parameters:
avro_generator.bundle_folders:
controller:
path: 'Controller'
tags: ['basic']
entity:
path: 'Entity'
form:
Expand All @@ -123,56 +124,87 @@ parameters:
path: 'Listener'
resources:
path: 'Resources'
tags: ['basic']
views:
path: 'Resources/views'
tags: ['basic']
doc:
path: 'Resources/doc'
meta:
path: 'Resources/meta'
css:
path: 'Resources/public/css'
tags: ['basic']
images:
path: 'Resources/public/images'
tags: ['basic']
js:
path: 'Resources/public/js'
tags: ['basic']
uploads:
path: 'Resources/public/uploads'
translations:
path: 'Resources/translations'
config:
path: 'Resources/config'
tags: ['basic']
services:
path: 'Resources/config/services'
tags: ['basic']

avro_generator.bundle_files:
bundle:
filename: '{{ bundleName }}.php'
template: 'AvroGeneratorBundle:Templates/Avro/Bundle.php'
tags: ['basic']
configuration:
filename: 'DependencyInjection/Configuration.php'
template: 'AvroGeneratorBundle:Templates/Avro/DependencyInjection/Configuration.php'
tags: ['basic']
extension:
filename: 'DependencyInjection/{{ bundleVendor }}/{{ bundleCoreName }}Extension.php'
filename: 'DependencyInjection/{{ bundleVendor }}{{ bundleCoreName }}Extension.php'
template: 'AvroGeneratorBundle:Templates/Avro/DependencyInjection/Extension.php'
tags: ['basic']
readme:
filename: 'README.md'
template: 'AvroGeneratorBundle:Templates/Avro/README.md'
tags: ['basic']
layout:
filename: 'Resources/views/layout.html.twig'
template: 'AvroGeneratorBundle:Templates/Avro/Resources/views/layout.html.twig'
tags: ['basic']
routing:
filename: 'Resources/config/routing.yml'
template: 'AvroGeneratorBundle:Templates/Avro/Resources/config/routing.yml'
tags: ['basic']
config:
filename: 'Resources/config/config.yml'
template: 'AvroGeneratorBundle:Templates/Avro/Resources/config/config.yml'
tags: ['basic']
license:
filename: 'Resources/meta/LICENSE'
template: 'AvroGeneratorBundle:Templates/Avro/Resources/meta/LICENSE'
tags: ['basic']
git_ignore:
filename: '.gitignore'
template: 'AvroGeneratorBundle:Templates/Avro/gitignore.html.twig'
tags: ['basic']
composer:
filename: 'composer.json'
template: 'AvroGeneratorBundle:Templates/Avro/composer.json'
tags: ['basic']

avro_generator.standalone_files:
controller:
filename: 'Controller/IndexController.php'
template: 'AvroGeneratorBundle:Templates/Avro/Controller/Controller.php'
parameters:
actions: ['index', 'controller']
tags: ['basic']
manipulator:
service: 'avro_generator.routing.manipulator'
method: 'updateBundleRouting'
filename: 'Resources/config/routing.yml'
setters:
format: 'yml'

5 changes: 0 additions & 5 deletions Templates/Avro/DependencyInjection/Configuration.php
Expand Up @@ -21,11 +21,6 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('{{ bundleAlias }}');

$rootNode
->children()
->scalarNode('db_driver')->cannotBeOverwritten()->defaultValue('{{ db_driver }}')->cannotBeEmpty()->end()
->end();

return $treeBuilder;
}
}
19 changes: 19 additions & 0 deletions Templates/Avro/Resources/meta/LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2011 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.

0 comments on commit 808270d

Please sign in to comment.