From 0c09f5a2762355e1c392ed4e63911b6c2889628f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Jura=CC=81sek?= Date: Fri, 5 Apr 2019 15:03:08 +0200 Subject: [PATCH] Update with released stable Nette version --- composer.json | 6 +-- src/Events/DI/EventsExtension.php | 65 ++++++++++++++--------- src/Events/Diagnostics/Panel.php | 6 +-- tests/KdybyTests/Events/PanelTest.phpt | 50 +++++++++++++++++ tests/KdybyTests/Events/config/panel.neon | 3 ++ 5 files changed, 100 insertions(+), 30 deletions(-) create mode 100644 tests/KdybyTests/Events/PanelTest.phpt create mode 100644 tests/KdybyTests/Events/config/panel.neon diff --git a/composer.json b/composer.json index ec713bb..e99618d 100644 --- a/composer.json +++ b/composer.json @@ -24,17 +24,17 @@ }, "require": { "php": "^7.1", - "nette/di": "^3.0-RC", + "nette/di": "^3.0", "nette/utils": "^3.0", "nette/reflection": "^2.4" }, "require-dev": { "kdyby/coding-standard": "^1.0@dev", - "nette/application": "^3.0-RC3", + "nette/application": "^3.0", "nette/bootstrap": "^3.0", "nette/caching": "^3.0", "nette/security": "^3.0", - "nette/http": "^3.0-RC", + "nette/http": "^3.0", "tracy/tracy": "^2.5", "symfony/event-dispatcher": "^3.0 || ^4.0", "nette/tester": "^2.2", diff --git a/src/Events/DI/EventsExtension.php b/src/Events/DI/EventsExtension.php index 162311a..af359a8 100644 --- a/src/Events/DI/EventsExtension.php +++ b/src/Events/DI/EventsExtension.php @@ -19,9 +19,7 @@ use Kdyby\Events\SymfonyDispatcher; use Nette\Configurator; use Nette\DI\Compiler; -use Nette\DI\Config\Helpers as DIConfigHelpers; use Nette\DI\Config\Helpers; -use Nette\DI\Config\Processor; use Nette\DI\Container as DIContainer; use Nette\DI\ContainerBuilder as DIContainerBuilder; use Nette\DI\Definitions\AccessorDefinition; @@ -31,7 +29,7 @@ use Nette\DI\Definitions\Reference; use Nette\DI\Definitions\ServiceDefinition; use Nette\DI\Definitions\Statement; -use Nette\DI\Resolver; +use Nette\DI\Helpers as DIHelpers; use Nette\PhpGenerator\ClassType as ClassTypeGenerator; use Nette\PhpGenerator\Helpers as GeneratorHelpers; use Nette\PhpGenerator\PhpLiteral; @@ -88,8 +86,10 @@ public function loadConfiguration() $this->allowedManagerSetup = []; $builder = $this->getContainerBuilder(); + /** @var array $config */ $config = Helpers::merge($this->getConfig(), $this->defaults); + /** @var array $userConfig */ $userConfig = $this->getConfig(); if (!array_key_exists('debugger', $userConfig)) { if (in_array(php_sapi_name(), ['cli', 'phpdbg'], TRUE)) { @@ -101,11 +101,11 @@ public function loadConfiguration() } $evm = $builder->addDefinition($this->prefix('manager')) - ->setClass(EventManager::class); + ->setType(EventManager::class); if ($config['debugger']) { $defaults = ['dispatchTree' => FALSE, 'dispatchLog' => TRUE, 'events' => TRUE, 'listeners' => FALSE]; if (is_array($config['debugger'])) { - $config['debugger'] = DIConfigHelpers::merge($config['debugger'], $defaults); + $config['debugger'] = Helpers::merge($config['debugger'], $defaults); } else { $config['debugger'] = $config['debugger'] !== self::PANEL_COUNT_MODE; } @@ -121,18 +121,18 @@ public function loadConfiguration() foreach ($config['subscribers'] as $i => $subscriber) { $def = $builder->addDefinition($this->prefix('subscriber.' . $i)); - $def->setFactory(Processor::processArguments([ + $def->setFactory(DIHelpers::filterArguments([ is_string($subscriber) ? new Statement($subscriber) : $subscriber, ])[0]); -// [$subscriberClass] = (array) $builder->normalizeEntity($def->getEntity()); + /** @var string $subscriberClass */ $subscriberClass = $def->getEntity(); if (class_exists($subscriberClass)) { - $def->setClass($subscriberClass); + $def->setType($subscriberClass); } $def->setAutowired(FALSE); - $def->addTag(self::SUBSCRIBER_TAG); + $def->addTag(self::TAG_SUBSCRIBER); } if (class_exists(SymfonyEvent::class)) { @@ -149,8 +149,9 @@ public function beforeCompile() $builder = $this->getContainerBuilder(); $config = $this->loadedConfig; + /** @var \Nette\DI\Definitions\ServiceDefinition $manager */ $manager = $builder->getDefinition($this->prefix('manager')); - foreach (array_keys($builder->findByTag(self::SUBSCRIBER_TAG)) as $serviceName) { + foreach (array_keys($builder->findByTag(self::TAG_SUBSCRIBER)) as $serviceName) { $manager->addSetup('addEventSubscriber', ['@' . $serviceName]); } @@ -219,7 +220,7 @@ public function afterCompile(ClassTypeGenerator $class) * @param \Nette\DI\ServiceDefinition $manager * @throws \Nette\Utils\AssertionException */ - private function validateSubscribers(DIContainerBuilder $builder, ServiceDefinition $manager) + private function validateSubscribers(DIContainerBuilder $builder, Definition $manager) { foreach ($manager->getSetup() as $stt) { if ($stt->getEntity() !== 'addEventSubscriber') { @@ -233,7 +234,17 @@ private function validateSubscribers(DIContainerBuilder $builder, ServiceDefinit $serviceName = $argument->getValue(); $def = $builder->getDefinition($serviceName); } elseif ($argument instanceof Statement) { - $serviceName = (string) $argument->getEntity(); + $entity = $argument->getEntity(); + + if ($entity instanceof Definition) { + $serviceName = $entity->getName(); + } elseif ($entity instanceof Reference) { + $serviceName = $entity->getValue(); + } elseif (is_array($entity)) { + $serviceName = $entity[0]; + } else { + $serviceName = (string) $entity; + } $def = $builder->getDefinition($serviceName); } else { $serviceName = ltrim($argument, '@'); @@ -322,13 +333,13 @@ private function validateSubscribers(DIContainerBuilder $builder, ServiceDefinit private function isAlias(Definition $definition) { if ($definition instanceof ServiceDefinition) { - return $definition->getFactory() && ( - $definition->getEntity() instanceof Reference + return $definition->getFactory()->getEntity() !== NULL && ( + $definition->getFactory()->getEntity() instanceof Reference || (is_string($definition->getEntity()) && substr($definition->getEntity(), 0, 1) === '@') ); } - return $definition instanceof Reference; + return FALSE; } /** @@ -340,33 +351,37 @@ private function autowireEvents(DIContainerBuilder $builder) if ($def instanceof ImportedDefinition) { continue; } - + /** @var \Nette\DI\Definitions\ServiceDefinition $def */ if ($this->isAlias($def)) { continue; // alias } $class = $def->getType(); - if (!class_exists($class)) { + if ($class === NULL || !class_exists($class)) { if ($def instanceof AccessorDefinition) { continue; } - if ($def instanceof FactoryDefinition && !$def->getResultDefinition()->getFactory()) { + if ($def instanceof FactoryDefinition && $def->getResultDefinition()->getFactory()->getEntity() === NULL) { continue; } if ($def instanceof FactoryDefinition) { - $class = $def->getResultDefinition()->getEntity(); - } else { + $entity = $def->getResultDefinition()->getEntity(); + $class = $entity instanceof Definition ? $entity->getType() : $entity; + } elseif ($def instanceof ServiceDefinition) { + /** @var string $class */ $class = $def->getEntity(); + } else { + continue; } if (is_array($class)) { continue; } - if (!class_exists($class)) { + if ($class === NULL || !class_exists($class)) { continue; } } @@ -380,6 +395,7 @@ private function autowireEvents(DIContainerBuilder $builder) protected function bindEventProperties(Definition $def, ClassTypeReflection $class) { + /** @var \Nette\DI\Definitions\ServiceDefinition $def */ $def = $def instanceof FactoryDefinition ? $def->getResultDefinition() : $def; foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { @@ -438,8 +454,9 @@ private function optimizeListeners(DIContainerBuilder $builder) $listeners[$id] = array_unique($subscribers); } - $builder->getDefinition($this->prefix('manager')) - ->setFactory(LazyEventManager::class, [$listeners]) + /** @var \Nette\DI\Definitions\ServiceDefinition $manager */ + $manager = $builder->getDefinition($this->prefix('manager')); + $manager->setFactory(LazyEventManager::class, [$listeners]) ->setSetup($this->allowedManagerSetup); } @@ -449,7 +466,7 @@ private function optimizeListeners(DIContainerBuilder $builder) */ private function filterArgs($statement) { - return Processor::processArguments([is_string($statement) ? new Statement($statement) : $statement]); + return DIHelpers::filterArguments([is_string($statement) ? new Statement($statement) : $statement]); } /** diff --git a/src/Events/Diagnostics/Panel.php b/src/Events/Diagnostics/Panel.php index c20cfe8..b0eae90 100644 --- a/src/Events/Diagnostics/Panel.php +++ b/src/Events/Diagnostics/Panel.php @@ -412,12 +412,12 @@ private function getClassMap() return $this->registeredClasses; } - $refl = new ReflectionProperty(DIContainer::class, 'meta'); + $refl = new ReflectionProperty(DIContainer::class, 'types'); $refl->setAccessible(TRUE); - $meta = $refl->getValue($this->sl); + $types = $refl->getValue($this->sl); $this->registeredClasses = []; - foreach ($meta[DIContainer::TYPES] as $type => $serviceIds) { + foreach ($types as $type => $serviceIds) { if (isset($this->registeredClasses[$type])) { $this->registeredClasses[$type] = FALSE; continue; diff --git a/tests/KdybyTests/Events/PanelTest.phpt b/tests/KdybyTests/Events/PanelTest.phpt new file mode 100644 index 0000000..36685cb --- /dev/null +++ b/tests/KdybyTests/Events/PanelTest.phpt @@ -0,0 +1,50 @@ +setTempDirectory(TEMP_DIR); + $config->addParameters(['container' => ['class' => 'SystemContainer_' . md5($configFile)]]); + EventsExtension::register($config); + $config->addConfig(__DIR__ . '/../nette-reset.neon'); + $config->addConfig(__DIR__ . '/config/' . $configFile . '.neon'); + return $config->createContainer(); + } + + public function testRender() + { + $container = $this->createContainer('panel'); + + $panel = new Panel($container); + + Assert::noError(function () use ($panel) { + $panel->getTab(); + $panel->getPanel(); + }); + } + +} + +(new PanelTest())->run(); diff --git a/tests/KdybyTests/Events/config/panel.neon b/tests/KdybyTests/Events/config/panel.neon new file mode 100644 index 0000000..2714aa5 --- /dev/null +++ b/tests/KdybyTests/Events/config/panel.neon @@ -0,0 +1,3 @@ +events: + +services: