Skip to content

Commit

Permalink
Merge pull request #15 from adespresso/mixed-improvements
Browse files Browse the repository at this point in the history
Mixed improvements
  • Loading branch information
EmanueleMinotto committed Jun 1, 2016
2 parents ad56cc8 + b1410ef commit 3373d12
Show file tree
Hide file tree
Showing 20 changed files with 467 additions and 295 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
composer.lock
phpunit.xml
Tests/autoload.php

vendor/*
vendor/
20 changes: 20 additions & 0 deletions .php_cs
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;
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ cache:

matrix:
include:
- php: 5.4
- php: 5.5
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
- php: 5.5
- php: 5.6
- php: 7.0
Expand All @@ -23,10 +24,11 @@ before_install:
- 'if [[ -n "$GH_TOKEN" ]]; then composer config github-oauth.github.com ${GH_TOKEN}; fi;'
- 'if [[ -n "$GH_TOKEN" ]]; then composer config preferred-install dist; fi;'

install: composer install --no-interaction --no-progress
install: 'composer update ${COMPOSER_FLAGS} --no-progress'

script:
- phpunit -v
- ./vendor/bin/php-cs-fixer fix --diff --dry-run
- ./vendor/bin/phpunit -v
- 'if [[ $TRAVIS_PULL_REQUEST == "false" && $TRAVIS_BRANCH == "master" && $TRAVIS_PHP_VERSION == "5.6" ]]; then sh generate-api.sh; fi;'

notifications:
Expand Down
87 changes: 54 additions & 33 deletions Admin/FeatureAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
);
}
}
77 changes: 57 additions & 20 deletions Command/LoadFeatureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/AeFeatureExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class AeFeatureExtension extends Extension
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
Expand Down
Loading

0 comments on commit 3373d12

Please sign in to comment.