Skip to content

Commit

Permalink
Fix compatibility with Nette 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
enumag authored and fprochazka committed Apr 19, 2016
1 parent f4a0942 commit 76d8bfc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
50 changes: 25 additions & 25 deletions src/Kdyby/Events/DI/EventsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public function loadConfiguration()
Nette\Utils\Validators::assertField($config, 'subscribers', 'array');
foreach ($config['subscribers'] as $subscriber) {
$def = $builder->addDefinition($this->prefix('subscriber.' . md5(Nette\Utils\Json::encode($subscriber))));
list($def->factory) = Nette\DI\Compiler::filterArguments(array(
$def->setFactory(Nette\DI\Compiler::filterArguments(array(
is_string($subscriber) ? new Nette\DI\Statement($subscriber) : $subscriber
));
))[0]);

list($subscriberClass) = (array) $builder->normalizeEntity($def->factory->entity);
list($subscriberClass) = (array) $builder->normalizeEntity($def->getEntity());
if (class_exists($subscriberClass)) {
$def->class = $subscriberClass;
$def->setClass($subscriberClass);
}

$def->setAutowired(FALSE);
Expand Down Expand Up @@ -145,12 +145,12 @@ public function beforeCompile()

public function afterCompile(Code\ClassType $class)
{
$init = $class->methods['initialize'];
$init = $class->getMethod('initialize');

/** @hack This tries to add the event invokation right after the code, generated by NetteExtension. */
$foundNetteInitStart = $foundNetteInitEnd = FALSE;
$lines = explode(";\n", trim($init->body));
$init->body = NULL;
$lines = explode(";\n", trim($init->getBody()));
$init->setBody(NULL);
while (($line = array_shift($lines)) || $lines) {
if ($foundNetteInitStart && !$foundNetteInitEnd &&
stripos($line, 'Nette\\') === FALSE && stripos($line, 'set_include_path') === FALSE && stripos($line, 'date_default_timezone_set') === FALSE
Expand Down Expand Up @@ -191,8 +191,8 @@ public function afterCompile(Code\ClassType $class)
*/
private function validateSubscribers(Nette\DI\ContainerBuilder $builder, Nette\DI\ServiceDefinition $manager)
{
foreach ($manager->setup as $stt) {
if ($stt->entity !== 'addEventSubscriber') {
foreach ($manager->getSetup() as $stt) {
if ($stt->getEntity() !== 'addEventSubscriber') {
$this->allowedManagerSetup[] = $stt;
continue;
}
Expand All @@ -209,51 +209,51 @@ private function validateSubscribers(Nette\DI\ContainerBuilder $builder, Nette\D
);
}

if (!$def->class) {
if (!$def->getClass()) {
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)) {
} elseif (!class_exists($def->getClass())) {
throw new AssertionException(
"Class '{$def->class}' of " . (is_numeric($serviceName) ? 'anonymous ' : '') . "service '$serviceName' cannot be found. " .
"Class '" . $def->getClass() . "' 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))) {
if (!in_array('Doctrine\Common\EventSubscriber' , class_implements($def->getClass()))) {
// 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);
$listenerInst = self::createInstanceWithoutConstructor($def->getClass());
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.");
throw new AssertionException("Event listener " . $def->getClass() . "::{$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.");
throw new AssertionException("Event listener " . $def->getClass() . "::{$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.");
throw new AssertionException("Event listener " . $def->getClass() . "::{$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.");
throw new AssertionException("Event listener " . $def->getClass() . "::{$listener[0]}() is not implemented.");
}
}
}
Expand All @@ -268,9 +268,9 @@ private function validateSubscribers(Nette\DI\ContainerBuilder $builder, Nette\D

private function isAlias(Nette\DI\ServiceDefinition $definition)
{
return $definition->factory instanceof Nette\DI\Statement && (
$definition->factory->entity instanceof Nette\DI\ServiceDefinition
|| (is_string($definition->factory->entity) && substr($definition->factory->entity, 0, 1) === '@')
return $definition->getFactory() && (
$definition->getEntity() instanceof Nette\DI\ServiceDefinition
|| (is_string($definition->getEntity()) && substr($definition->getEntity(), 0, 1) === '@')
);
}

Expand All @@ -287,11 +287,11 @@ private function autowireEvents(Nette\DI\ContainerBuilder $builder)
continue; // alias
}

if (!class_exists($class = $builder->expand($def->class))) {
if (!$def->factory) {
if (!class_exists($class = $builder->expand($def->getClass()))) {
if (!$def->getFactory()) {
continue;

} elseif (is_array($class = $builder->expand($def->factory->entity))) {
} elseif (is_array($class = $builder->expand($def->getEntity()))) {
continue;

} elseif (!class_exists($class)) {
Expand Down Expand Up @@ -364,7 +364,7 @@ private function optimizeListeners(Nette\DI\ContainerBuilder $builder)

$builder->getDefinition($this->prefix('manager'))
->setClass('Kdyby\Events\LazyEventManager', array($listeners))
->setup = $this->allowedManagerSetup;
->setSetup($this->allowedManagerSetup);
}


Expand Down
2 changes: 1 addition & 1 deletion tests/KdybyTests/Events/Extension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ExtensionTest extends Tester\TestCase
Assert::match('Please, specify existing class for service \'events.subscriber.%a%\' explicitly, and make sure, that the class exists and can be autoloaded.', $e->getMessage());

} catch (Nette\DI\ServiceCreationException $e) {
Assert::match("Class NonExistingClass_%a% used in service 'events.subscriber.%a%' not found or is not instantiable.", $e->getMessage());
Assert::match("Class NonExistingClass_%a% used in service 'events.subscriber.%a%' not found%a?%.", $e->getMessage());

} catch (\Exception $e) {
Assert::fail($e->getMessage());
Expand Down

0 comments on commit 76d8bfc

Please sign in to comment.