Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * This file is part of the DunglasApiBundle package.
- *
- * (c) Kévin Dunglas <dunglas@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Bender\ApiBundle\DependencyInjection\Compiler;
- use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
- use Symfony\Component\DependencyInjection\ContainerBuilder;
- use Symfony\Component\DependencyInjection\Definition;
- use Symfony\Component\DependencyInjection\Reference;
- /**
- * Add resources to the resource collection and populate operations if necessary.
- *
- * @author Kévin Dunglas <dunglas@gmail.com>
- */
- class FormHandlerPass implements CompilerPassInterface
- {
- /**
- * {@inheritdoc}
- */
- public function process(ContainerBuilder $container)
- {
- $resourceCollectionDefinition = $container->getDefinition('api.resource_collection');
- $resourceReferences = [];
- foreach ($container->findTaggedServiceIds('api.resource') as $serviceId => $tags) {
- $resourceReferences[] = new Reference($serviceId);
- $resourceDefinition = $container->getDefinition($serviceId);
- if ($this->hasForm($resourceDefinition->getMethodCalls()) === true) {
- $resourceDefinition->addMethodCall('initItemOperations', [[
- $this->createOperation($container, $serviceId, 'GET', false),
- $this->createOperation($container, $serviceId, 'GET', true),
- ]]);
- }
- }
- $resourceCollectionDefinition->addMethodCall('init', [$resourceReferences]);
- }
- /**
- * hasForm - test if resource has a setForm method call
- *
- * @param array $methodCalls
- * @return bool
- */
- private function hasForm($methodCalls) {
- foreach($methodCalls as $method) {
- if ($method[0] == 'setForm') {
- return true;
- }
- }
- return false;
- }
- private function createOperation(ContainerBuilder $container, $serviceId, $method, $collection)
- {
- $factoryMethodName = 'createFormItemOperation';
- $operationId = "$serviceId.item_operation.form";
- if ($collection === true) {
- $factoryMethodName = 'createFormCollectionOperation';
- $operationId = "$serviceId.item_operation.cform";
- }
- $operation = new Definition(
- 'Dunglas\ApiBundle\Api\Operation\Operation',
- [new Reference($serviceId), $method]
- );
- $operation->setFactory([new Reference('bender_api.form_operation_factory'), $factoryMethodName]);
- $operation->setLazy(true);
- $container->setDefinition($operationId, $operation);
- //$definition->addMethodCall('createItemOperation', [new Reference($operationId)]);
- return new Reference($operationId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement