AddExpressionCompilersPass.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SecurityExtraBundle\DependencyInjection\Compiler;
  18. use JMS\SecurityExtraBundle\Exception\RuntimeException;
  19. use Symfony\Component\DependencyInjection\Reference;
  20. use Symfony\Component\DependencyInjection\ContainerBuilder;
  21. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  22. class AddExpressionCompilersPass implements CompilerPassInterface
  23. {
  24. public function process(ContainerBuilder $container)
  25. {
  26. if (!$container->hasDefinition('security.expressions.compiler')) {
  27. return;
  28. }
  29. $compilerDef = $container->getDefinition('security.expressions.compiler');
  30. foreach ($container->findTaggedServiceIds('security.expressions.function_compiler')
  31. as $id => $attr) {
  32. $compilerDef->addMethodCall('addFunctionCompiler', array(new Reference($id)));
  33. }
  34. foreach ($container->findTaggedServiceIds('security.expressions.type_compiler')
  35. as $id => $attr) {
  36. $compilerDef->addMethodCall('addTypeCompiler', array(new Reference($id)));
  37. }
  38. $serviceMap = $parameterMap = array();
  39. foreach ($container->findTaggedServiceIds('security.expressions.variable') as $id => $attributes) {
  40. foreach ($attributes as $attr) {
  41. if (!isset($attr['variable']) || (!isset($attr['service']) && !isset($attr['parameter']))) {
  42. throw new RuntimeException(sprintf('"variable", and either "service" or "parameter" must be given for tag "security.expressions.variable" for service id "%s".', $id));
  43. }
  44. if (isset($attr['service'])) {
  45. $serviceMap[$attr['variable']] = $attr['service'];
  46. $container
  47. ->findDefinition($attr['service'])
  48. ->setPublic(true)
  49. ;
  50. } else {
  51. $parameterMap[$attr['variable']] = $attr['parameter'];
  52. }
  53. }
  54. }
  55. $container->getDefinition('security.expressions.variable_compiler')
  56. ->addMethodCall('setMaps', array($serviceMap, $parameterMap));
  57. }
  58. }