Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * This file is part of the DunglasApiBundle package.
  5. *
  6. * (c) Kévin Dunglas <dunglas@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11.  
  12. namespace Bender\ApiBundle\DependencyInjection\Compiler;
  13.  
  14. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Definition;
  17. use Symfony\Component\DependencyInjection\Reference;
  18.  
  19. /**
  20. * Add resources to the resource collection and populate operations if necessary.
  21. *
  22. * @author Kévin Dunglas <dunglas@gmail.com>
  23. */
  24. class FormHandlerPass implements CompilerPassInterface
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function process(ContainerBuilder $container)
  30. {
  31. $resourceCollectionDefinition = $container->getDefinition('api.resource_collection');
  32. $resourceReferences = [];
  33.  
  34. foreach ($container->findTaggedServiceIds('api.resource') as $serviceId => $tags) {
  35. $resourceReferences[] = new Reference($serviceId);
  36. $resourceDefinition = $container->getDefinition($serviceId);
  37.  
  38. if ($this->hasForm($resourceDefinition->getMethodCalls()) === true) {
  39.  
  40. $resourceDefinition->addMethodCall('initItemOperations', [[
  41. $this->createOperation($container, $serviceId, 'GET', false),
  42. $this->createOperation($container, $serviceId, 'GET', true),
  43. ]]);
  44. }
  45. }
  46.  
  47. $resourceCollectionDefinition->addMethodCall('init', [$resourceReferences]);
  48. }
  49.  
  50. /**
  51. * hasForm - test if resource has a setForm method call
  52. *
  53. * @param array $methodCalls
  54. * @return bool
  55. */
  56. private function hasForm($methodCalls) {
  57.  
  58. foreach($methodCalls as $method) {
  59. if ($method[0] == 'setForm') {
  60. return true;
  61. }
  62. }
  63.  
  64. return false;
  65. }
  66.  
  67. private function createOperation(ContainerBuilder $container, $serviceId, $method, $collection)
  68. {
  69.  
  70. $factoryMethodName = 'createFormItemOperation';
  71. $operationId = "$serviceId.item_operation.form";
  72.  
  73. if ($collection === true) {
  74. $factoryMethodName = 'createFormCollectionOperation';
  75. $operationId = "$serviceId.item_operation.cform";
  76. }
  77.  
  78. $operation = new Definition(
  79. 'Dunglas\ApiBundle\Api\Operation\Operation',
  80. [new Reference($serviceId), $method]
  81. );
  82.  
  83. $operation->setFactory([new Reference('bender_api.form_operation_factory'), $factoryMethodName]);
  84. $operation->setLazy(true);
  85.  
  86. $container->setDefinition($operationId, $operation);
  87. //$definition->addMethodCall('createItemOperation', [new Reference($operationId)]);
  88.  
  89. return new Reference($operationId);
  90.  
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement