Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.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. /**
  28. * {@inheritdoc}
  29. */
  30. public function process(ContainerBuilder $container)
  31. {
  32.  
  33. $resourceCollectionDefinition = $container->getDefinition('api.resource_collection');
  34. $resourceReferences = [];
  35.  
  36. foreach($container->findTaggedServiceIds('api.resource') as $serviceId => $tags) {
  37. $resourceDefinition = $container->getDefinition($serviceId);
  38. $resourceReferences[] = new Reference($serviceId);
  39.  
  40. if($resourceDefinition->hasMethodCall('initItemOperations')) {
  41. $resourceDefinition->removeMethodCall('initItemOperations');
  42. }
  43.  
  44. $resourceDefinition->addMethodCall('initItemOperations', [[
  45. $this->createOperation($container, $serviceId, 'GET', false),
  46. $this->createOperation($container, $serviceId, 'PUT', false),
  47. $this->createOperation($container, $serviceId, 'DELETE', false),
  48. $this->createFormOperation($container, $serviceId, 'GET', false),
  49. $this->createFormOperation($container, $serviceId, 'GET', true),
  50. ]]);
  51.  
  52. }
  53.  
  54.  
  55. if($resourceCollectionDefinition->hasMethodCall('init')) {
  56. $resourceCollectionDefinition->removeMethodCall('init');
  57. }
  58.  
  59. $resourceCollectionDefinition->addMethodCall('init', [$resourceReferences]);
  60. }
  61.  
  62. /**
  63. * hasForm - test if resource has a setForm method call
  64. *
  65. * @param array $methodCalls
  66. * @return bool
  67. */
  68. private function hasForm($methodCalls) {
  69.  
  70. foreach($methodCalls as $method) {
  71. if ($method[0] == 'setForm') {
  72. return true;
  73. }
  74. }
  75.  
  76. return false;
  77. }
  78.  
  79. /**
  80. * Adds an operation.
  81. *
  82. * @param ContainerBuilder $container
  83. * @param string $serviceId
  84. * @param string $method
  85. * @param bool $collection
  86. *
  87. * @return Reference
  88. */
  89. private function createOperation(ContainerBuilder $container, $serviceId, $method, $collection)
  90. {
  91. if ($collection) {
  92. $factoryMethodName = 'createCollectionOperation';
  93. $operationId = '.collection_operation.';
  94. } else {
  95. $factoryMethodName = 'createItemOperation';
  96. $operationId = '.item_operation.';
  97. }
  98. $operation = new Definition(
  99. 'Dunglas\ApiBundle\Api\Operation\Operation',
  100. [new Reference($serviceId), $method]
  101. );
  102. $operation->setFactory([new Reference('api.operation_factory'), $factoryMethodName]);
  103. $operation->setLazy(true);
  104. $operationId = $serviceId.$operationId.$method;
  105. $container->setDefinition($operationId, $operation);
  106. return new Reference($operationId);
  107. }
  108.  
  109. private function createFormOperation(ContainerBuilder $container, $serviceId, $method, $collection)
  110. {
  111.  
  112. $factoryMethodName = 'createFormItemOperation';
  113. $operationId = "$serviceId.item_operation.form";
  114.  
  115. if ($collection === true) {
  116. $factoryMethodName = 'createFormCollectionOperation';
  117. $operationId = "$serviceId.item_operation.cform";
  118. }
  119.  
  120. $operation = new Definition(
  121. 'Dunglas\ApiBundle\Api\Operation\Operation',
  122. [new Reference($serviceId), $method]
  123. );
  124.  
  125. $operation->setFactory([new Reference('bender_api.form_operation_factory'), $factoryMethodName]);
  126. $operation->setLazy(true);
  127.  
  128. $container->setDefinition($operationId, $operation);
  129. //$definition->addMethodCall('createItemOperation', [new Reference($operationId)]);
  130.  
  131. return new Reference($operationId);
  132.  
  133. }
  134.  
  135. /**
  136. * Gets class of the given definition.
  137. *
  138. * @param ContainerBuilder $container
  139. * @param Definition $definition
  140. *
  141. * @return string|null
  142. */
  143. private function getClass(ContainerBuilder $container, Definition $definition)
  144. {
  145. if ($class = $definition->getClass()) {
  146. return $class;
  147. }
  148. if ($definition instanceof DefinitionDecorator) {
  149. return $container->getDefinition($definition->getParent())->getClass();
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement