Converts @service or @\Class -> service name and checks its existence.
public getServiceName ( $arg ) : string | ||
Результат | string | of FALSE, if argument is not service name |
/** * Formats PHP code for class instantiating, function calling or property setting in PHP. * @return string */ private function formatStatement(Statement $statement) { $entity = $statement->getEntity(); $arguments = $statement->arguments; if (is_string($entity) && Strings::contains($entity, '?')) { // PHP literal return $this->formatPhp($entity, $arguments); } elseif ($service = $this->builder->getServiceName($entity)) { // factory calling return $this->formatPhp('$this->?(?*)', [Container::getMethodName($service), $arguments]); } elseif ($entity === 'not') { // operator return $this->formatPhp('!?', [$arguments[0]]); } elseif (is_string($entity)) { // class name return $this->formatPhp("new {$entity}" . ($arguments ? '(?*)' : ''), [$arguments]); } elseif ($entity[0] === '') { // globalFunc return $this->formatPhp("{$entity['1']}(?*)", [$arguments]); } elseif ($entity[0] instanceof Statement) { $inner = $this->formatPhp('?', [$entity[0]]); if (substr($inner, 0, 4) === 'new ') { $inner = "({$inner})"; } return $this->formatPhp("{$inner}->?(?*)", [$entity[1], $arguments]); } elseif ($entity[1][0] === '$') { // property getter, setter or appender $name = substr($entity[1], 1); if ($append = substr($name, -2) === '[]') { $name = substr($name, 0, -2); } if ($this->builder->getServiceName($entity[0])) { $prop = $this->formatPhp('?->?', [$entity[0], $name]); } else { $prop = $this->formatPhp($entity[0] . '::$?', [$name]); } return $arguments ? $this->formatPhp($prop . ($append ? '[]' : '') . ' = ?', [$arguments[0]]) : $prop; } elseif ($service = $this->builder->getServiceName($entity[0])) { // service method return $this->formatPhp('?->?(?*)', [$entity[0], $entity[1], $arguments]); } else { // static method return $this->formatPhp("{$entity['0']}::{$entity['1']}(?*)", [$arguments]); } }
/** * @param \Nette\DI\ContainerBuilder $builder * @param \Nette\DI\ServiceDefinition $manager * @throws AssertionException */ private function validateSubscribers(Nette\DI\ContainerBuilder $builder, Nette\DI\ServiceDefinition $manager) { foreach ($manager->setup as $stt) { if ($stt->entity !== 'addEventSubscriber') { $this->allowedManagerSetup[] = $stt; continue; } try { $serviceName = $builder->getServiceName(reset($stt->arguments)); $def = $builder->getDefinition($serviceName); } catch (\Exception $e) { throw new AssertionException("Please, do not register listeners directly to service '" . $this->prefix('manager') . "'. " . "Use section '" . $this->name . ": subscribers: ', or tag the service as '" . self::SUBSCRIBER_TAG . "'.", 0, $e); } if (!$def->class) { throw new AssertionException("Please, specify existing class for " . (is_numeric($serviceName) ? 'anonymous ' : '') . "service '{$serviceName}' explicitly, " . "and make sure, that the class exists and can be autoloaded."); } elseif (!class_exists($def->class)) { throw new AssertionException("Class '{$def->class}' of " . (is_numeric($serviceName) ? 'anonymous ' : '') . "service '{$serviceName}' cannot be found. " . "Please make sure, that the class exists and can be autoloaded."); } if (!in_array('Doctrine\\Common\\EventSubscriber', class_implements($def->class))) { // the minimum is Doctrine EventSubscriber, but recommend is Kdyby Subscriber throw new AssertionException("Subscriber '{$serviceName}' doesn't implement Kdyby\\Events\\Subscriber."); } $eventNames = array(); $listenerInst = self::createInstanceWithoutConstructor($def->class); foreach ($listenerInst->getSubscribedEvents() as $eventName => $params) { if (is_numeric($eventName) && is_string($params)) { // [EventName, ...] list(, $method) = Kdyby\Events\Event::parseName($params); $eventNames[] = ltrim($params, '\\'); if (!method_exists($listenerInst, $method)) { throw new AssertionException("Event listener " . $def->class . "::{$method}() is not implemented."); } } elseif (is_string($eventName)) { // [EventName => ???, ...] $eventNames[] = ltrim($eventName, '\\'); if (is_string($params)) { // [EventName => method, ...] if (!method_exists($listenerInst, $params)) { throw new AssertionException("Event listener " . $def->class . "::{$params}() is not implemented."); } } elseif (is_string($params[0])) { // [EventName => [method, priority], ...] if (!method_exists($listenerInst, $params[0])) { throw new AssertionException("Event listener " . $def->class . "::{$params[0]}() is not implemented."); } } else { foreach ($params as $listener) { // [EventName => [[method, priority], ...], ...] if (!method_exists($listenerInst, $listener[0])) { throw new AssertionException("Event listener " . $def->class . "::{$listener[0]}() is not implemented."); } } } } } $this->listeners[$serviceName] = array_unique($eventNames); } }