-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from adespresso/mixed-improvements
Mixed improvements
- Loading branch information
Showing
20 changed files
with
467 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
composer.lock | ||
phpunit.xml | ||
Tests/autoload.php | ||
|
||
vendor/* | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
$config = Symfony\CS\Config\Config::create() | ||
->level(Symfony\CS\FixerInterface::SYMFONY_LEVEL) | ||
->fixers([ | ||
'ordered_use', | ||
'phpdoc_order', | ||
'short_array_syntax', | ||
]); | ||
|
||
if (null === $input->getArgument('path')) { | ||
$config | ||
->finder( | ||
Symfony\CS\Finder\DefaultFinder::create() | ||
->exclude('Resources') | ||
->in(__DIR__) | ||
); | ||
} | ||
|
||
return $config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,75 +2,88 @@ | |
|
||
namespace Ae\FeatureBundle\Admin; | ||
|
||
use Ae\FeatureBundle\Entity\FeatureManager; | ||
use Sonata\AdminBundle\Admin\Admin; | ||
use Sonata\AdminBundle\Datagrid\ListMapper; | ||
use Sonata\AdminBundle\Datagrid\DatagridMapper; | ||
use Sonata\AdminBundle\Datagrid\ListMapper; | ||
use Sonata\AdminBundle\Form\FormMapper; | ||
use Ae\FeatureBundle\Entity\FeatureManager; | ||
|
||
/** | ||
* @author Carlo Forghieri <[email protected]> | ||
*/ | ||
class FeatureAdmin extends Admin | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configureDatagridFilters(DatagridMapper $datagridMapper) | ||
{ | ||
$datagridMapper | ||
->add('name') | ||
->add('enabled') | ||
; | ||
->add('name') | ||
->add('enabled'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configureListFields(ListMapper $listMapper) | ||
{ | ||
$listMapper | ||
->add('id') | ||
->add('name') | ||
->add('role') | ||
->add('enabled') | ||
->add('_action', 'actions', array( | ||
'actions' => array( | ||
'edit' => array(), | ||
'delete' => array(), | ||
), | ||
)) | ||
; | ||
->add('_action', 'actions', [ | ||
'actions' => [ | ||
'edit' => [], | ||
'delete' => [], | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configureFormFields(FormMapper $formMapper) | ||
{ | ||
$roles = $this->getRoles(); | ||
|
||
$formMapper | ||
->add('name', 'text', array( | ||
->add('name', 'text', [ | ||
'required' => true, | ||
)) | ||
->add('enabled', 'checkbox', array( | ||
]) | ||
->add('enabled', 'checkbox', [ | ||
'required' => false, | ||
)) | ||
->add('role', 'choice', array( | ||
'choices' => array_combine($roles, $roles), | ||
]) | ||
->add('role', 'choice', [ | ||
'choices' => array_combine($roles, $roles), | ||
'multiple' => false, | ||
'required' => false, | ||
)) | ||
; | ||
]); | ||
|
||
if (!$this->getSubject()->getParent()) { | ||
$formMapper | ||
->add('children', 'sonata_type_collection', array( | ||
'required' => false, | ||
), array( | ||
'edit' => 'inline', | ||
'inline' => 'table', | ||
)) | ||
; | ||
$formMapper->add( | ||
'children', | ||
'sonata_type_collection', | ||
[ | ||
'required' => false, | ||
], | ||
[ | ||
'edit' => 'inline', | ||
'inline' => 'table', | ||
] | ||
); | ||
} | ||
} | ||
|
||
protected function getRoles() | ||
{ | ||
$roleHierarchy = $this->getConfigurationPool()->getContainer()->getParameter('security.role_hierarchy.roles'); | ||
$roleHierarchy = $this | ||
->getConfigurationPool() | ||
->getContainer() | ||
->getParameter('security.role_hierarchy.roles'); | ||
|
||
$roles = array_keys($roleHierarchy); | ||
$roles = array_keys($roleHierarchy); | ||
$roles[] = 'ROLE_PREVIOUS_ADMIN'; | ||
|
||
return $roles; | ||
|
@@ -80,22 +93,30 @@ public function createQuery($context = 'list') | |
{ | ||
$query = parent::createQuery($context); | ||
if ($context === 'list') { | ||
$query->andWhere(current($query->getDQLPart('from'))->getAlias().'.parent IS NULL'); | ||
$alias = current($query->getDQLPart('from'))->getAlias(); | ||
$query->andWhere($alias.'.parent IS NULL'); | ||
} | ||
|
||
return $query; | ||
} | ||
|
||
public function postUpdate($object) | ||
{ | ||
$cache = $this->modelManager->getEntityManager($object)->getConfiguration()->getResultCacheImpl(); | ||
$cache = $this->modelManager | ||
->getEntityManager($object) | ||
->getConfiguration() | ||
->getResultCacheImpl(); | ||
|
||
foreach ($object->getChildren() as $child) { | ||
$cache->delete($this->getObjectCacheKey($child)); | ||
} | ||
} | ||
|
||
protected function getObjectCacheKey($object) | ||
{ | ||
return FeatureManager::generateCacheKey($object->getParent()->getName(), $object->getName()); | ||
return FeatureManager::generateCacheKey( | ||
$object->getParent()->getName(), | ||
$object->getName() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,75 +2,112 @@ | |
|
||
namespace Ae\FeatureBundle\Command; | ||
|
||
use Ae\FeatureBundle\Twig\Node\FeatureNode; | ||
use Exception; | ||
use InvalidArgumentException; | ||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Finder\Finder; | ||
use Ae\FeatureBundle\Twig\Node\FeatureNode; | ||
use Twig_Node; | ||
|
||
/** | ||
* @author Carlo Forghieri <[email protected]> | ||
*/ | ||
class LoadFeatureCommand extends ContainerAwareCommand | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('features:load') | ||
->setDescription('Persist new features found in templates') | ||
->setDefinition(array( | ||
new InputArgument('bundle', InputArgument::REQUIRED, 'The bundle where to load the features'), | ||
new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Do not persist new features'), | ||
)); | ||
->addArgument( | ||
'bundle', | ||
InputArgument::REQUIRED, | ||
'The bundle where to load the features' | ||
) | ||
->addOption( | ||
'dry-run', | ||
null, | ||
InputOption::VALUE_NONE, | ||
'Do not persist new features' | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$twig = $this->getContainer()->get('twig'); | ||
$bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle')); | ||
$twig = $this->getContainer()->get('twig'); | ||
$bundle = $this | ||
->getApplication() | ||
->getKernel() | ||
->getBundle($input->getArgument('bundle')); | ||
|
||
if (!$bundle) { | ||
new \InvalidArgumentException("Bundle `$bundle` does not exists"); | ||
throw new InvalidArgumentException("Bundle `$bundle` does not exists"); | ||
} | ||
$found = array(); | ||
$found = []; | ||
$dir = $bundle->getPath().'/Resources/views/'; | ||
if (!is_dir($dir)) { | ||
throw \Exception("'Directory `$dir` does not exists."); | ||
throw new Exception("'Directory `$dir` does not exists."); | ||
} | ||
$finder = new Finder(); | ||
$files = $finder->files()->name('*.html.twig')->in($dir); | ||
$files = $finder->files()->name('*.html.twig')->in($dir); | ||
foreach ($files as $file) { | ||
$tree = $twig->parse($twig->tokenize(file_get_contents($file->getPathname()))); | ||
$tree = $twig->parse( | ||
$twig->tokenize(file_get_contents($file->getPathname())) | ||
); | ||
$tags = $this->findFeatureNodes($tree); | ||
if ($tags) { | ||
$found = array_merge($found, $tags); | ||
|
||
foreach ($tags as $tag) { | ||
$output->writeln(sprintf('Found <info>%s</info>.<info>%s</info> in <info>%s</info>', $tag['parent'], $tag['name'], $file->getFilename())); | ||
$output->writeln(sprintf( | ||
'Found <info>%s</info>.<info>%s</info> in <info>%s</info>', | ||
$tag['parent'], | ||
$tag['name'], | ||
$file->getFilename() | ||
)); | ||
} | ||
} | ||
} | ||
|
||
if ($input->getOption('dry-run')) { | ||
return; | ||
} | ||
|
||
$manager = $this->getContainer()->get('cw_feature.manager'); | ||
foreach ($found as $tag) { | ||
$manager->findOrCreate($tag['name'], $tag['parent']); | ||
} | ||
} | ||
|
||
protected function findFeatureNodes(\Twig_Node $node) | ||
protected function findFeatureNodes(Twig_Node $node) | ||
{ | ||
$found = array(); | ||
$stack = array($node); | ||
$found = []; | ||
$stack = [$node]; | ||
while ($stack) { | ||
$node = array_pop($stack); | ||
if ($node instanceof FeatureNode) { | ||
$arguments = $node->getNode('tests')->getNode(0)->getNode('arguments')->getKeyValuePairs(); | ||
$tag = array(); | ||
$arguments = $node | ||
->getNode('tests') | ||
->getNode(0) | ||
->getNode('arguments') | ||
->getKeyValuePairs(); | ||
|
||
$tag = []; | ||
foreach ($arguments as $argument) { | ||
$tag[$argument['key']->getAttribute('value')] = $argument['value']->getAttribute('value'); | ||
$keyAttr = $argument['key']->getAttribute('value'); | ||
$valueAttr = $argument['value']->getAttribute('value'); | ||
|
||
$tag[$keyAttr] = $valueAttr; | ||
} | ||
$key = md5(serialize($tag)); | ||
$found[$key] = $tag; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.