Skip to content

Commit

Permalink
Use tag for rule constraints and a compiler pass to get them
Browse files Browse the repository at this point in the history
  • Loading branch information
maxailloud committed Mar 9, 2015
1 parent ff7d107 commit 5703682
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 73 deletions.
2 changes: 2 additions & 0 deletions ClarolineCoreBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Claroline\CoreBundle\DependencyInjection\Compiler\DoctrineEntityListenerPass;
use Claroline\CoreBundle\DependencyInjection\Compiler\DynamicConfigPass;
use Claroline\CoreBundle\DependencyInjection\Compiler\ImportersConfigPass;
use Claroline\CoreBundle\DependencyInjection\Compiler\RuleConstraintsConfigPass;
use FOS\OAuthServerBundle\FOSOAuthServerBundle;
use IDCI\Bundle\ExporterBundle\IDCIExporterBundle;
use Nelmio\ApiDocBundle\NelmioApiDocBundle;
Expand All @@ -35,6 +36,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new DynamicConfigPass());
$container->addCompilerPass(new ImportersConfigPass());
$container->addCompilerPass(new DoctrineEntityListenerPass());
$container->addCompilerPass(new RuleConstraintsConfigPass());
}

public function supports($environment)
Expand Down
37 changes: 37 additions & 0 deletions DependencyInjection/Compiler/RuleConstraintsConfigPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Claroline Connect package.
*
* (c) Claroline Consortium <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Claroline\CoreBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class RuleConstraintsConfigPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$ruleValidatorServiceKey = 'claroline.rule.validator';

if (false === $container->hasDefinition($ruleValidatorServiceKey)) {
return;
}

$ruleValidator = $container->getDefinition($ruleValidatorServiceKey);

foreach ($container->findTaggedServiceIds('claroline.rule.constraint') as $id => $attributes) {
// echo "<pre>";
// var_dump($id);
// echo "</pre>" . PHP_EOL;
$ruleValidator->addMethodCall('addConstraint', array(new Reference($id)));
}
}
}
5 changes: 5 additions & 0 deletions Rule/Constraints/ActionConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

use Claroline\CoreBundle\Rule\Entity\Rule;
use Doctrine\ORM\QueryBuilder;
use JMS\DiExtraBundle\Annotation as DI;

/**
* @DI\Service
* @DI\Tag("claroline.rule.constraint")
*/
class ActionConstraint extends AbstractConstraint
{
/**
Expand Down
62 changes: 0 additions & 62 deletions Rule/Constraints/BadgeConstraint.php

This file was deleted.

5 changes: 5 additions & 0 deletions Rule/Constraints/DoerConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

use Claroline\CoreBundle\Rule\Entity\Rule;
use Doctrine\ORM\QueryBuilder;
use JMS\DiExtraBundle\Annotation as DI;

/**
* @DI\Service
* @DI\Tag("claroline.rule.constraint")
*/
class DoerConstraint extends AbstractConstraint
{
/**
Expand Down
5 changes: 5 additions & 0 deletions Rule/Constraints/OccurenceConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

use Claroline\CoreBundle\Rule\Entity\Rule;
use Doctrine\ORM\QueryBuilder;
use JMS\DiExtraBundle\Annotation as DI;

/**
* @DI\Service
* @DI\Tag("claroline.rule.constraint")
*/
class OccurenceConstraint extends AbstractConstraint
{
/**
Expand Down
5 changes: 5 additions & 0 deletions Rule/Constraints/ReceiverConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

use Claroline\CoreBundle\Rule\Entity\Rule;
use Doctrine\ORM\QueryBuilder;
use JMS\DiExtraBundle\Annotation as DI;

/**
* @DI\Service
* @DI\Tag("claroline.rule.constraint")
*/
class ReceiverConstraint extends AbstractConstraint
{
/**
Expand Down
5 changes: 5 additions & 0 deletions Rule/Constraints/ResourceConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

use Claroline\CoreBundle\Rule\Entity\Rule;
use Doctrine\ORM\QueryBuilder;
use JMS\DiExtraBundle\Annotation as DI;

/**
* @DI\Service
* @DI\Tag("claroline.rule.constraint")
*/
class ResourceConstraint extends AbstractConstraint
{
/**
Expand Down
5 changes: 5 additions & 0 deletions Rule/Constraints/ResultConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

use Claroline\CoreBundle\Rule\Entity\Rule;
use Doctrine\ORM\QueryBuilder;
use JMS\DiExtraBundle\Annotation as DI;

/**
* @DI\Service
* @DI\Tag("claroline.rule.constraint")
*/
class ResultConstraint extends AbstractConstraint
{
/**
Expand Down
5 changes: 5 additions & 0 deletions Rule/Constraints/RuleActiveDateConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

use Claroline\CoreBundle\Rule\Entity\Rule;
use Doctrine\ORM\QueryBuilder;
use JMS\DiExtraBundle\Annotation as DI;

/**
* @DI\Service
* @DI\Tag("claroline.rule.constraint")
*/
class RuleActiveDateConstraint extends AbstractConstraint
{
/**
Expand Down
50 changes: 39 additions & 11 deletions Rule/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Claroline\CoreBundle\Rule\Constraints\ActionConstraint;
use Claroline\CoreBundle\Rule\Constraints\BadgeConstraint;
use Claroline\CoreBundle\Rule\Constraints\AbstractConstraint;
use Claroline\CoreBundle\Rule\Constraints\DoerConstraint;
use Claroline\CoreBundle\Rule\Constraints\OccurenceConstraint;
use Claroline\CoreBundle\Rule\Constraints\ReceiverConstraint;
Expand All @@ -24,6 +25,7 @@
use Claroline\CoreBundle\Entity\User;
use Claroline\CoreBundle\Entity\Log\Log;
use Claroline\CoreBundle\Repository\Log\LogRepository;
use Doctrine\Common\Collections\ArrayCollection;
use JMS\DiExtraBundle\Annotation as DI;

/**
Expand All @@ -36,6 +38,11 @@ class Validator
*/
private $logRepository;

/**
* @var AbstractConstraint[]
*/
protected $constraints;

/**
* @DI\InjectParams({
* "logRepository" = @DI\Inject("claroline.repository.log"),
Expand All @@ -44,6 +51,37 @@ class Validator
public function __construct(LogRepository $logRepository)
{
$this->logRepository = $logRepository;
$this->constraints = new ArrayCollection();
}

/**
* @return Constraints\AbstractConstraint[]
*/
public function getConstraints()
{
return $this->constraints;
}

/**
* @param Constraints\AbstractConstraint[] $constraints
*
* @return Validator
*/
public function setConstraints($constraints)
{
$this->constraints = $constraints;

return $this;
}

/**
* @param AbstractConstraint $constraint
*
* @return bool
*/
public function addConstraint(AbstractConstraint $constraint)
{
return $this->constraints->add($constraint);
}

/**
Expand Down Expand Up @@ -95,17 +133,7 @@ public function validateRule(Rule $rule, array $restrictions = array())
{
/** @var \Claroline\CoreBundle\Rule\Constraints\AbstractConstraint[] $usedConstraints */
$usedConstraints = array();
/** @var \Claroline\CoreBundle\Rule\Constraints\AbstractConstraint[] $existedConstraints */
$existedConstraints = array(
new OccurenceConstraint(),
new ResultConstraint(),
new ResourceConstraint(),
new DoerConstraint(),
new ReceiverConstraint(),
new ActionConstraint(),
new BadgeConstraint(),
new RuleActiveDateConstraint()
);
$existedConstraints = $this->getConstraints();

foreach ($existedConstraints as $existedConstraint) {
if ($existedConstraint->isApplicableTo($rule)) {
Expand Down

0 comments on commit 5703682

Please sign in to comment.