pax_global_header 0000666 0000000 0000000 00000000064 12510010553 0014502 g ustar 00root root 0000000 0000000 52 comment=8049e0fc7abb48178b4a2a9af230eceebe1a83bc
Events-2.4.0/ 0000775 0000000 0000000 00000000000 12510010553 0012751 5 ustar 00root root 0000000 0000000 Events-2.4.0/README.md 0000664 0000000 0000000 00000002067 12510010553 0014235 0 ustar 00root root 0000000 0000000 Kdyby/Events
======
[](https://travis-ci.org/Kdyby/Events)
[](https://packagist.org/packages/kdyby/events)
[](https://packagist.org/packages/kdyby/events)
Requirements
------------
Kdyby/Events requires PHP 5.3.2 or higher.
- [Nette Framework](https://github.com/nette/nette)
Kdyby/Events are also compatible with events dispatcher in [doctrine/common](https://github.com/doctrine/common), so you can use them with Doctrine.
Installation
------------
The best way to install Kdyby/Events is using [Composer](http://getcomposer.org/):
```sh
$ composer require kdyby/events:~2.2
```
Documentation
------------
Learn more in the [documentation](https://github.com/Kdyby/Events/blob/master/docs/en/index.md).
-----
Homepage [http://www.kdyby.org](http://www.kdyby.org) and repository [http://github.com/kdyby/events](http://github.com/kdyby/events).
Events-2.4.0/composer.json 0000664 0000000 0000000 00000003076 12510010553 0015501 0 ustar 00root root 0000000 0000000 {
"name": "kdyby/events",
"type": "library",
"description": "Events for Nette Framework",
"keywords": ["nette", "kdyby"],
"homepage": "http://kdyby.org",
"license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"],
"authors": [
{
"name": "Filip Procházka",
"homepage": "http://filip-prochazka.com",
"email": "filip@prochazka.su"
}
],
"support": {
"email": "filip@prochazka.su",
"issues": "https://github.com/kdyby/events/issues"
},
"require": {
"nette/di": "~2.3@dev",
"nette/utils": "~2.3@dev"
},
"require-dev": {
"nette/nette": "~2.3@dev",
"nette/application": "~2.3@dev",
"nette/bootstrap": "~2.3@dev",
"nette/caching": "~2.3@dev",
"nette/component-model": "~2.2@dev",
"nette/database": "~2.3@dev",
"nette/deprecated": "~2.3@dev",
"nette/di": "~2.3@dev",
"nette/finder": "~2.3@dev",
"nette/forms": "~2.3@dev",
"nette/http": "~2.3@dev",
"nette/mail": "~2.3@dev",
"nette/neon": "~2.3@dev",
"nette/php-generator": "~2.3@dev",
"nette/reflection": "~2.3@dev",
"nette/robot-loader": "~2.3@dev",
"nette/safe-stream": "~2.3@dev",
"nette/security": "~2.3@dev",
"nette/tokenizer": "~2.2@dev",
"latte/latte": "~2.3@dev",
"tracy/tracy": "~2.3@dev",
"nette/utils": "~2.3@dev",
"symfony/event-dispatcher": "~2.5",
"nette/tester": "~1.4@rc",
"jakub-onderka/php-parallel-lint": "~0.7"
},
"autoload": {
"psr-0": {
"Kdyby\\Events\\": "src/"
},
"classmap": [
"src/Kdyby/Events/exceptions.php"
],
"files": [
"src/Doctrine/compatibility.php"
]
},
"extra": {
"branch-alias": {
"dev-master": "2.4-dev"
}
}
}
Events-2.4.0/license.md 0000664 0000000 0000000 00000004536 12510010553 0014725 0 ustar 00root root 0000000 0000000 Licenses
========
Good news! You may use Kdyby Framework under the terms of either
the New BSD License or the GNU General Public License (GPL) version 2 or 3.
The BSD License is recommended for most projects. It is easy to understand and it
places almost no restrictions on what you can do with the framework. If the GPL
fits better to your project, you can use the framework under this license.
You don't have to notify anyone which license you are using. You can freely
use Kdyby Framework in commercial projects as long as the copyright header
remains intact.
New BSD License
---------------
Copyright (c) 2008 Filip Procházka (http://filip-prochazka.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of "Kdyby Framework" nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright owner or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
GNU General Public License
--------------------------
GPL licenses are very very long, so instead of including them here we offer
you URLs with full text:
- [GPL version 2](http://www.gnu.org/licenses/gpl-2.0.html)
- [GPL version 3](http://www.gnu.org/licenses/gpl-3.0.html)
Events-2.4.0/src/ 0000775 0000000 0000000 00000000000 12510010553 0013540 5 ustar 00root root 0000000 0000000 Events-2.4.0/src/Doctrine/ 0000775 0000000 0000000 00000000000 12510010553 0015307 5 ustar 00root root 0000000 0000000 Events-2.4.0/src/Doctrine/compatibility.php 0000664 0000000 0000000 00000002014 12510010553 0020666 0 ustar 00root root 0000000 0000000 addEventListener(\$subscriber->getSubscribedEvents(), \$subscriber); }
public function removeEventSubscriber(EventSubscriber \$subscriber) { \$this->removeEventListener(\$subscriber->getSubscribedEvents(), \$subscriber); }
abstract public function addEventListener(\$events, \$listener);
abstract public function removeEventListener(\$events, \$listener = NULL);
}
interface EventSubscriber { function getSubscribedEvents(); }
}
GEN;
eval($aliases);
define('KDYBY_TO_DOCTRINE_EVENTS', 1);
}
Events-2.4.0/src/Kdyby/ 0000775 0000000 0000000 00000000000 12510010553 0014622 5 ustar 00root root 0000000 0000000 Events-2.4.0/src/Kdyby/Events/ 0000775 0000000 0000000 00000000000 12510010553 0016066 5 ustar 00root root 0000000 0000000 Events-2.4.0/src/Kdyby/Events/DI/ 0000775 0000000 0000000 00000000000 12510010553 0016362 5 ustar 00root root 0000000 0000000 Events-2.4.0/src/Kdyby/Events/DI/EventsExtension.php 0000664 0000000 0000000 00000027300 12510010553 0022236 0 ustar 00root root 0000000 0000000
*/
class EventsExtension extends Nette\DI\CompilerExtension
{
/** @deprecated */
const EVENT_TAG = self::TAG_EVENT;
/** @deprecated */
const SUBSCRIBER_TAG = self::TAG_SUBSCRIBER;
const TAG_EVENT = 'kdyby.event';
const TAG_SUBSCRIBER = 'kdyby.subscriber';
const PANEL_COUNT_MODE = 'count';
/**
* @var array
*/
public $defaults = array(
'subscribers' => array(),
'validate' => TRUE,
'autowire' => TRUE,
'optimize' => TRUE,
'debugger' => '%debugMode%',
'exceptionHandler' => NULL,
);
/**
* @var array
*/
private $listeners = array();
/**
* @var array
*/
private $allowedManagerSetup = array();
public function loadConfiguration()
{
$this->listeners = array();
$this->allowedManagerSetup = array();
$builder = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);
$userConfig = $this->getConfig();
if (!isset($userConfig['debugger']) && !$config['debugger']) {
$config['debugger'] = self::PANEL_COUNT_MODE;
}
$evm = $builder->addDefinition($this->prefix('manager'))
->setClass('Kdyby\Events\EventManager')
->setInject(FALSE);
if ($config['debugger']) {
$defaults = array('dispatchTree' => FALSE, 'dispatchLog' => TRUE, 'events' => TRUE, 'listeners' => FALSE);
if (is_array($config['debugger'])) {
$config['debugger'] = Nette\DI\Config\Helpers::merge($config['debugger'], $defaults);
} else {
$config['debugger'] = $config['debugger'] !== self::PANEL_COUNT_MODE;
}
$evm->addSetup('Kdyby\Events\Diagnostics\Panel::register(?, ?)->renderPanel = ?', array('@self', '@container', $config['debugger']));
}
if ($config['exceptionHandler'] !== NULL) {
$evm->addSetup('setExceptionHandler', $this->filterArgs($config['exceptionHandler']));
}
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(
is_string($subscriber) ? new Nette\DI\Statement($subscriber) : $subscriber
));
list($subscriberClass) = (array) $builder->normalizeEntity($def->factory->entity);
if (class_exists($subscriberClass)) {
$def->class = $subscriberClass;
}
$def->setAutowired(FALSE);
$def->addTag(self::SUBSCRIBER_TAG);
}
if (class_exists('Symfony\Component\EventDispatcher\Event')) {
$builder->addDefinition($this->prefix('symfonyProxy'))
->setClass('Symfony\Component\EventDispatcher\EventDispatcherInterface')
->setFactory('Kdyby\Events\SymfonyDispatcher');
}
}
public function beforeCompile()
{
$builder = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);
$manager = $builder->getDefinition($this->prefix('manager'));
foreach (array_keys($builder->findByTag(self::SUBSCRIBER_TAG)) as $serviceName) {
$manager->addSetup('addEventSubscriber', array('@' . $serviceName));
}
Nette\Utils\Validators::assertField($config, 'validate', 'bool');
if ($config['validate']) {
$this->validateSubscribers($builder, $manager);
}
Nette\Utils\Validators::assertField($config, 'autowire', 'bool');
if ($config['autowire']) {
$this->autowireEvents($builder);
}
Nette\Utils\Validators::assertField($config, 'optimize', 'bool');
if ($config['optimize']) {
if (!$config['validate']) {
throw new Kdyby\Events\InvalidStateException("Cannot optimize without validation.");
}
$this->optimizeListeners($builder);
}
}
public function afterCompile(Code\ClassType $class)
{
$init = $class->methods['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;
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
) {
$init->addBody(Code\Helpers::format(
'$this->getService(?)->createEvent(?)->dispatch($this);',
$this->prefix('manager'),
array('Nette\\DI\\Container', 'onInitialize')
));
$foundNetteInitEnd = TRUE;
}
if (!$foundNetteInitEnd && (
stripos($line, 'Nette\\') !== FALSE || stripos($line, 'set_include_path') !== FALSE || stripos($line, 'date_default_timezone_set') !== FALSE
)) {
$foundNetteInitStart = TRUE;
}
$init->addBody($line . ';');
}
if (!$foundNetteInitEnd) {
$init->addBody(Code\Helpers::format(
'$this->getService(?)->createEvent(?)->dispatch($this);',
$this->prefix('manager'),
array('Nette\\DI\\Container', 'onInitialize')
));
}
}
/**
* @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);
}
}
/**
* @param \Nette\DI\ContainerBuilder $builder
*/
private function autowireEvents(Nette\DI\ContainerBuilder $builder)
{
foreach ($builder->getDefinitions() as $def) {
/** @var Nette\DI\ServiceDefinition $def */
if ($def->factory instanceof Nette\DI\Statement && $def->factory->entity instanceof Nette\DI\ServiceDefinition) {
continue; // alias
}
if (!class_exists($class = $builder->expand($def->class))) {
if (!$def->factory) {
continue;
} elseif (is_array($class = $builder->expand($def->factory->entity))) {
continue;
} elseif (!class_exists($class)) {
continue;
}
}
$this->bindEventProperties($def, Nette\Reflection\ClassType::from($class));
}
}
protected function bindEventProperties(Nette\DI\ServiceDefinition $def, Nette\Reflection\ClassType $class)
{
foreach ($class->getProperties(Nette\Reflection\Property::IS_PUBLIC) as $property) {
if (!preg_match('#^on[A-Z]#', $name = $property->getName())) {
continue;
}
if ($property->hasAnnotation('persistent') || $property->hasAnnotation('inject')) { // definitely not an event
continue;
}
$def->addSetup('$' . $name, array(
new Nette\DI\Statement($this->prefix('@manager') . '::createEvent', array(
array($class->getName(), $name),
new Code\PhpLiteral('$service->' . $name)
))
));
}
}
/**
* @param \Nette\DI\ContainerBuilder $builder
*/
private function optimizeListeners(Nette\DI\ContainerBuilder $builder)
{
$listeners = array();
foreach ($this->listeners as $serviceName => $eventNames) {
foreach ($eventNames as $eventName) {
list($namespace, $event) = Kdyby\Events\Event::parseName($eventName);
$listeners[$eventName][] = $serviceName;
if (!$namespace || !class_exists($namespace)) {
continue; // it might not even be a "classname" event namespace
}
// find all subclasses and register the listener to all the classes dispatching them
foreach ($builder->getDefinitions() as $def) {
if (!$class = $def->getClass()) {
continue; // ignore unresolved classes
}
if (is_subclass_of($class, $namespace)) {
$listeners["$class::$event"][] = $serviceName;
}
}
}
}
foreach ($listeners as $id => $subscribers) {
$listeners[$id] = array_unique($subscribers);
}
$builder->getDefinition($this->prefix('manager'))
->setClass('Kdyby\Events\LazyEventManager', array($listeners))
->setup = $this->allowedManagerSetup;
}
/**
* @param string|\stdClass $statement
* @return Nette\DI\Statement[]
*/
private function filterArgs($statement)
{
return Nette\DI\Compiler::filterArguments(array(is_string($statement) ? new Nette\DI\Statement($statement) : $statement));
}
/**
* @param \Nette\Configurator $configurator
*/
public static function register(Nette\Configurator $configurator)
{
$configurator->onCompile[] = function ($config, Nette\DI\Compiler $compiler) {
$compiler->addExtension('events', new EventsExtension());
};
}
/**
* @param string $class
* @return \Doctrine\Common\EventSubscriber
*/
private static function createInstanceWithoutConstructor($class)
{
if (method_exists('ReflectionClass', 'newInstanceWithoutConstructor')) {
$listenerInst = Nette\Reflection\ClassType::from($class)->newInstanceWithoutConstructor();
} else {
$listenerInst = unserialize(sprintf('O:%d:"%s":0:{}', strlen($class), $class));
}
return $listenerInst;
}
}
Events-2.4.0/src/Kdyby/Events/Diagnostics/ 0000775 0000000 0000000 00000000000 12510010553 0020335 5 ustar 00root root 0000000 0000000 Events-2.4.0/src/Kdyby/Events/Diagnostics/Panel.php 0000664 0000000 0000000 00000031236 12510010553 0022112 0 ustar 00root root 0000000 0000000
*/
class Panel extends Nette\Object implements Tracy\IBarPanel
{
/**
* @var EventManager
*/
private $evm;
/**
* @var Nette\DI\Container
*/
private $sl;
/**
* @var array
*/
private $events = array();
/**
* @var array
*/
private $dispatchLog = array();
/**
* @var array
*/
private $dispatchTree = array();
/**
* @var array|NULL
*/
private $dispatchTreePointer = NULL;
/**
* @var array
*/
private $listenerIds = array();
/**
* @var array
*/
private $inlineCallbacks = array();
/**
* @var array
*/
private $registeredClasses;
/**
* @var bool
*/
public $renderPanel = TRUE;
public function __construct(Nette\DI\Container $sl)
{
$this->sl = $sl;
}
/**
* @param EventManager $evm
*/
public function setEventManager(EventManager $evm)
{
$this->evm = $evm;
$evm->setPanel($this);
}
public function setServiceIds(array $listenerIds)
{
if (!$this->renderPanel || (is_array($this->renderPanel) && !$this->renderPanel['listeners'])) {
return;
}
$this->listenerIds = $listenerIds;
}
public function registerEvent(Event $event)
{
$this->events[] = $event;
$event->setPanel($this);
}
public function eventDispatch($eventName, EventArgs $args = NULL)
{
if (!$this->renderPanel) {
return;
}
if (!is_array($this->renderPanel) || $this->renderPanel['dispatchLog']) {
$this->dispatchLog[$eventName][] = $args;
}
if (!is_array($this->renderPanel) || $this->renderPanel['dispatchTree']) {
// [parent-ref, name, args, children]
$meta = array(&$this->dispatchTreePointer, $eventName, $args, array());
if ($this->dispatchTreePointer === NULL) {
$this->dispatchTree[] = & $meta;
} else {
$this->dispatchTreePointer[3][] = & $meta;
}
$this->dispatchTreePointer = & $meta;
}
}
public function eventDispatched($eventName, EventArgs $args = NULL)
{
if (!$this->renderPanel || (is_array($this->renderPanel) && !$this->renderPanel['dispatchTree'])) {
return;
}
$this->dispatchTreePointer = &$this->dispatchTreePointer[0];
}
public function inlineCallbacks($eventName, $inlineCallbacks)
{
if (!$this->renderPanel) {
return;
}
$this->inlineCallbacks[$eventName] = (array) $inlineCallbacks;
}
/**
* Renders HTML code for custom tab.
*
* @return string
*/
public function getTab()
{
if (empty($this->events)) {
return NULL;
}
return ''
. ''
. count(Arrays::flatten($this->dispatchLog)) . ' calls'
. '';
}
/**
* Renders HTML code for custom panel.
*
* @return string
*/
public function getPanel()
{
if (!$this->renderPanel) {
return '';
}
if (empty($this->events)) {
return NULL;
}
$visited = array();
$h = 'htmlspecialchars';
$s = '';
$s .= $this->renderPanelDispatchLog($visited);
$s .= $this->renderPanelEvents($visited);
$s .= $this->renderPanelListeners($visited);
if ($s) {
$s .= '
' .
Callback::toString($id) . ($id instanceof \Closure ? $shortFilename(Callback::toReflection($id)) : '') .
'
' .
$h(Nette\Reflection\ClassType::from($class)->getName()) .
'