hasMethodCall() public method

Check if the current definition has a given method to call after service initialization.
public hasMethodCall ( string $method ) : boolean
$method string The method name to search for
return boolean
 /**
  * Apply the default values required by the AdminInterface to the Admin service definition
  *
  * @param \Symfony\Component\DependencyInjection\Definition $definition
  * @param array $attributes
  * @return \Symfony\Component\DependencyInjection\Definition
  */
 public function applyDefaults(Definition $definition, array $attributes = array())
 {
     $definition->setScope(ContainerInterface::SCOPE_PROTOTYPE);
     $manager_type = $attributes[0]['manager_type'];
     if (!$definition->hasMethodCall('setModelManager')) {
         $definition->addMethodCall('setModelManager', array(new Reference(sprintf('sonata.admin.manager.%s', $manager_type))));
     }
     if (!$definition->hasMethodCall('setFormContractor')) {
         $definition->addMethodCall('setFormContractor', array(new Reference(sprintf('sonata.admin.builder.%s_form', $manager_type))));
     }
     if (!$definition->hasMethodCall('setListBuilder')) {
         $definition->addMethodCall('setListBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_list', $manager_type))));
     }
     if (!$definition->hasMethodCall('setDatagridBuilder')) {
         $definition->addMethodCall('setDatagridBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_datagrid', $manager_type))));
     }
     if (!$definition->hasMethodCall('setTranslator')) {
         $definition->addMethodCall('setTranslator', array(new Reference('translator')));
     }
     if (!$definition->hasMethodCall('setConfigurationPool')) {
         $definition->addMethodCall('setConfigurationPool', array(new Reference('sonata.admin.pool')));
     }
     if (!$definition->hasMethodCall('setRouter')) {
         $definition->addMethodCall('setRouter', array(new Reference('router')));
     }
     if (!$definition->hasMethodCall('setLabel')) {
         $label = isset($attributes[0]['label']) ? $attributes[0]['label'] : '-';
         $definition->addMethodCall('setLabel', array($label));
     }
     $definition->addMethodCall('configure');
     return $definition;
 }
 /**
  * @param Definition $definition
  * @param string     $class
  * @param string     $repository
  * @param bool       $filterType
  */
 private function assertDefinition(Definition $definition, $class, $repository, $filterType)
 {
     $this->assertSame($definition->getClass(), $repository);
     $factory = $definition->getFactory();
     $this->assertSame($factory[1], "getRepository");
     $this->assertSame($definition->getArgument(0), $class);
     $this->assertTrue($definition->hasMethodCall('setAggregationQueryBuilder'));
     if ($filterType) {
         $this->assertTrue($definition->hasMethodCall('setFilterTypeManager'));
     }
 }
 /**
  * @param Definition $definition
  */
 private function clearMethodCalls(Definition $definition)
 {
     $methodCall = 'offsetSet';
     while ($definition->hasMethodCall($methodCall)) {
         $definition->removeMethodCall($methodCall);
     }
 }
 public function testProcess()
 {
     $dispatcherDef = new Definition();
     $slotDef = new Definition();
     $signalIdentifier = 'FooSignal';
     $slotDef->addTag('ezpublish.api.slot', array('signal' => $signalIdentifier));
     $containerBuilder = new ContainerBuilder();
     $slotId = 'acme.foo_slot';
     $containerBuilder->addDefinitions(array($slotId => $slotDef, 'ezpublish.signalslot.signal_dispatcher' => $dispatcherDef));
     $pass = new SignalSlotPass();
     $pass->process($containerBuilder);
     $this->assertTrue($dispatcherDef->hasMethodCall('attach'));
     $calls = $dispatcherDef->getMethodCalls();
     list($method, $arguments) = $calls[0];
     $this->assertSame('attach', $method);
     list($signal, $serviceId) = $arguments;
     $this->assertSame($signalIdentifier, $signal);
     $this->assertEquals($slotId, new Reference($serviceId));
 }
 /**
  * This method read the attribute keys and configure admin class to use the related dependency
  *
  * @param \Symfony\Component\DependencyInjection\Definition $definition
  * @param array                                             $attributes
  */
 public function applyConfigurationFromAttribute(Definition $definition, array $attributes)
 {
     $keys = array('model_manager', 'form_contractor', 'show_builder', 'list_builder', 'datagrid_builder', 'translator', 'configuration_pool', 'router', 'validator', 'security_handler', 'menu_factory', 'route_builder', 'label_translator_strategy');
     foreach ($keys as $key) {
         $method = 'set' . BaseFieldDescription::camelize($key);
         if (!isset($attributes[$key]) || $definition->hasMethodCall($method)) {
             continue;
         }
         $definition->addMethodCall($method, array(new Reference($attributes[$key])));
     }
 }
Example #6
0
 private function setDefaultValue(Definition $definition, $method, $values)
 {
     if (!$definition->hasMethodCall($method)) {
         $definition->addMethodCall($method, $values);
     }
 }
 /**
  * Loads an ODM document managers bundle mapping information.
  *
  * There are two distinct configuration possibilities for mapping information:
  *
  * 1. Specify a bundle and optionally details where the entity and mapping information reside.
  * 2. Specify an arbitrary mapping location.
  *
  * @example
  *
  *  doctrine.orm:
  *     mappings:
  *         MyBundle1: ~
  *         MyBundle2: yml
  *         MyBundle3: { type: annotation, dir: Documents/ }
  *         MyBundle4: { type: xml, dir: Resources/config/doctrine/mapping }
  *         MyBundle5:
  *             type: yml
  *             dir: [bundle-mappings1/, bundle-mappings2/]
  *             alias: BundleAlias
  *         arbitrary_key:
  *             type: xml
  *             dir: %kernel.dir%/../src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Documents
  *             prefix: DoctrineExtensions\Documents\
  *             alias: DExt
  *
  * In the case of bundles everything is really optional (which leads to autodetection for this bundle) but
  * in the mappings key everything except alias is a required argument.
  *
  * @param array $documentManager A configured ODM entity manager.
  * @param Definition A Definition instance
  * @param ContainerBuilder $container A ContainerBuilder instance
  */
 protected function loadDocumentManagerBundlesMappingInformation(array $documentManager, Definition $odmConfigDef, ContainerBuilder $container)
 {
     // reset state of drivers and alias map. They are only used by this methods and children.
     $this->drivers = array();
     $this->aliasMap = array();
     $this->loadMappingInformation($documentManager, $container);
     $this->registerMappingDrivers($documentManager, $container);
     if ($odmConfigDef->hasMethodCall('setDocumentNamespaces')) {
         // TODO: Can we make a method out of it on Definition? replaceMethodArguments() or something.
         $calls = $odmConfigDef->getMethodCalls();
         foreach ($calls as $call) {
             if ($call[0] == 'setDocumentNamespaces') {
                 $this->aliasMap = array_merge($call[1][0], $this->aliasMap);
             }
         }
         $method = $odmConfigDef->removeMethodCall('setDocumentNamespaces');
     }
     $odmConfigDef->addMethodCall('setDocumentNamespaces', array($this->aliasMap));
 }
Example #8
0
 /**
  * Check if the current definition has a given method to call after service initialization.
  *
  * @param string $method The method name to search for
  *
  * @return Boolean
  *
  * @api
  * @since 4.0.0
  */
 public function hasMethodCall($method)
 {
     return $this->_underlyingSymfonyDefinition->hasMethodCall($method);
 }
 /**
  * @param string $className
  * @param ReflectionClass $reflectionClass
  * @param Definition $definition
  * @param string $fastAnnotationChecksRegex
  * @param string[] $preferredServices
  * @param ParameterBagInterface $parameterBag
  */
 private function autowireClass($className, ReflectionClass $reflectionClass, Definition $definition, $fastAnnotationChecksRegex, $preferredServices, ParameterBagInterface $parameterBag)
 {
     // constructor - autowire always
     if ($reflectionClass->getConstructor()) {
         $definition->setArguments($this->autowireMethod($className, $reflectionClass->getConstructor(), $definition->getArguments(), $preferredServices));
     }
     if ($fastAnnotationChecksRegex === null || $reflectionClass->getDocComment() && preg_match($fastAnnotationChecksRegex, $reflectionClass->getDocComment())) {
         // method calls @Autowired
         foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
             if ($reflectionMethod->getName() === "__construct") {
                 continue;
             }
             if ($definition->hasMethodCall($reflectionMethod->getName())) {
                 continue;
             }
             if (strpos($reflectionMethod->getDocComment(), "@Autowired") === false) {
                 continue;
             }
             /** @var Autowired $annotation */
             $annotation = $this->annotationReader->getMethodAnnotation($reflectionMethod, "Skrz\\Bundle\\AutowiringBundle\\Annotation\\Autowired");
             if ($annotation === null) {
                 continue;
             }
             if ($annotation->name !== null) {
                 throw new AutowiringException(sprintf("@Autowired parameter can be used only on properties. %s::%s(...)", $className, $reflectionMethod->getName()));
             }
             $definition->addMethodCall($reflectionMethod->getName(), $this->autowireMethod($className, $reflectionMethod, [], $preferredServices));
         }
         // properties @Autowired, @Value
         $manualProperties = $definition->getProperties();
         foreach ($reflectionClass->getProperties() as $reflectionProperty) {
             if (isset($manualProperties[$reflectionProperty->getName()])) {
                 continue;
             }
             if (strpos($reflectionProperty->getDocComment(), "@Autowired") === false && strpos($reflectionProperty->getDocComment(), "@Value") === false) {
                 continue;
             }
             $annotations = $this->annotationReader->getPropertyAnnotations($reflectionProperty);
             $autowiredAnnotation = false;
             $valueAnnotation = false;
             $incorrectUsage = false;
             foreach ($annotations as $annotation) {
                 if ($annotation instanceof Autowired) {
                     if ($valueAnnotation) {
                         $incorrectUsage = true;
                         break;
                     }
                     $autowiredAnnotation = true;
                     try {
                         if ($annotation->name !== null) {
                             $definition->setProperty($reflectionProperty->getName(), new Reference($annotation->name));
                         } else {
                             $definition->setProperty($reflectionProperty->getName(), $this->getValue($reflectionProperty->getDeclaringClass(), $reflectionProperty->getDocComment(), null, null, false, null, $preferredServices));
                         }
                     } catch (AutowiringException $exception) {
                         throw new AutowiringException(sprintf("%s (Property %s::\$%s)", $exception->getMessage(), $className, $reflectionProperty->getName()), $exception->getCode(), $exception);
                     }
                 } elseif ($annotation instanceof Value) {
                     if ($autowiredAnnotation) {
                         $incorrectUsage = true;
                         break;
                     }
                     try {
                         $definition->setProperty($reflectionProperty->getName(), $parameterBag->resolveValue($annotation->value));
                     } catch (RuntimeException $exception) {
                         throw new AutowiringException(sprintf("%s (Property %s::\$%s)", $exception->getMessage(), $className, $reflectionProperty->getName()), $exception->getCode(), $exception);
                     }
                 }
             }
             if ($incorrectUsage) {
                 throw new AutowiringException(sprintf("Property can have either @Autowired, or @Value annotation, not both. (Property %s::\$%s)", $className, $reflectionProperty->getName()));
             }
         }
     }
 }