| Current Path : /proc/1908984/root/proc/1908984/root/home/digilove/www/41423/ |
| Current File : //proc/1908984/root/proc/1908984/root/home/digilove/www/41423/Dispatcher.php.tar |
home/digilove/public_html/components/com_akeeba/Dispatcher/Dispatcher.php 0000644 00000011066 15235175775 0022745 0 ustar 00 <?php
/**
* @package akeebabackup
* @copyright Copyright (c)2006-2023 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace Akeeba\Backup\Site\Dispatcher;
// Protect from unauthorized access
defined('_JEXEC') || die();
use Akeeba\Backup\Admin\Dispatcher\Dispatcher as AdminDispatcher;
use Akeeba\Backup\Admin\Helper\SecretWord;
use Akeeba\Engine\Factory;
use Akeeba\Engine\Platform;
use FOF40\Container\Container;
use FOF40\Dispatcher\Exception\AccessForbidden;
use Joomla\CMS\Document\Document;
use Joomla\CMS\Document\JsonDocument as JDocumentJSON;
use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Language\Text;
class Dispatcher extends AdminDispatcher
{
/** @var string The name of the default view, in case none is specified */
public $defaultView = 'Backup';
/**
* Dispatcher constructor. Overridden to set up a different default view and migrated views map than the back-end.
*
* @param Container $container The component's container
* @param array $config Optional configuration overrides
*/
public function __construct(Container $container, array $config)
{
parent::__construct($container, $config);
$this->defaultView = 'Backup';
$this->viewNameAliases = [
'backup' => 'Backup',
'backups' => 'Backup',
'check' => 'Check',
'checks' => 'Check',
'json' => 'Json',
'jsons' => 'Json',
];
}
/**
* Executes before dispatching the request to the appropriate controller
*/
public function onBeforeDispatch()
{
// Make sure we have a version loaded
@include_once($this->container->backEndPath . '/version.php');
if (!defined('AKEEBA_VERSION'))
{
define('AKEEBA_VERSION', 'dev');
define('AKEEBA_DATE', date('Y-m-d'));
}
// Core version: there is no front-end, throw a 403
if (!defined('AKEEBA_PRO') || !AKEEBA_PRO)
{
throw new AccessForbidden(Text::_('COM_AKEEBA_ERR_NO_FRONTEND_IN_CORE'));
}
// $this->container->platform->importPlugin('akeebabackup');
// $this->container->platform->runPlugins('onComAkeebaDispatcherBeforeDispatch', []);
$this->onBeforeDispatchViewAliases();
// Load the FOF language
$lang = $this->container->platform->getLanguage();
$lang->load('lib_fof40', JPATH_SITE, 'en-GB', true, true);
$lang->load('lib_fof40', JPATH_SITE, null, true, false);
// Necessary defines for Akeeba Engine
if (!defined('AKEEBAENGINE'))
{
define('AKEEBAENGINE', 1);
define('AKEEBAROOT', $this->container->backEndPath . '/BackupEngine');
define('ALICEROOT', $this->container->backEndPath . '/AliceEngine');
}
// Make sure we have a profile set throughout the component's lifetime
$profile_id = $this->container->platform->getSessionVar('profile', null, 'akeeba');
if (is_null($profile_id))
{
$this->container->platform->setSessionVar('profile', 1, 'akeeba');
}
// Load Akeeba Engine
$basePath = $this->container->backEndPath;
require_once $basePath . '/BackupEngine/Factory.php';
// Load the Akeeba Engine configuration
Platform::addPlatform('joomla3x', JPATH_COMPONENT_ADMINISTRATOR . '/BackupPlatform/Joomla3x');
$akeebaEngineConfig = Factory::getConfiguration();
Platform::getInstance()->load_configuration();
unset($akeebaEngineConfig);
// Prevents the "SQLSTATE[HY000]: General error: 2014" due to resource sharing with Akeeba Engine
$this->fixPDOMySQLResourceSharing();
// Load the utils helper library
Platform::getInstance()->load_version_defines();
// Make sure the front-end backup Secret Word is stored encrypted
$params = $this->container->params;
SecretWord::enforceEncryption($params, 'frontend_secret_word');
// Create a media file versioning tag
$this->container->mediaVersion = md5(AKEEBA_VERSION . AKEEBA_DATE);
}
public function onAfterDispatch()
{
// Make sure that Api and Json views forcibly get format=json
if (in_array($this->view, ['Api', 'Json']))
{
$format = $this->input->getCmd('format', 'html');
if ($format == 'json')
{
return;
}
$app = JFactory::getApplication();
// Disable caching, disable offline, force use of index.php
$app->set('caching', 0);
$app->set('offline', 0);
$app->set('themeFile', 'index.php');
/** @var \Joomla\CMS\Document\JsonDocument $doc */
$doc = Document::getInstance('json');
$app->loadDocument($doc);
if (property_exists(JFactory::class, 'document'))
{
JFactory::$document = $doc;
}
// Set a custom document name
/** @var JDocumentJSON $document */
$document = $this->container->platform->getDocument();
$document->setName('akeeba_backup');
}
}
}
home/digilove/public_html/110/libraries/fof40/Event/Dispatcher.php 0000644 00000014547 15235506205 0020671 0 ustar 00 <?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\Event;
defined('_JEXEC') || die;
use FOF40\Container\Container;
class Dispatcher implements Observable
{
/** @var Container The container this event dispatcher is attached to */
protected $container;
/** @var array The observers attached to the dispatcher */
protected $observers = [];
/** @var array Maps events to observers */
protected $events = [];
/**
* Public constructor
*
* @param Container $container The container this event dispatcher is attached to
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Returns the container this event dispatcher is attached to
*
* @return Container
*/
public function getContainer(): Container
{
return $this->container;
}
/**
* Attaches an observer to the object
*
* @param Observer $observer The observer to attach
*
* @return static Ourselves, for chaining
*/
public function attach(Observer $observer)
{
$className = get_class($observer);
// Make sure this observer is not already registered
if (isset($this->observers[$className]))
{
return $this;
}
// Attach observer
$this->observers[$className] = $observer;
// Register the observable events
$events = $observer->getObservableEvents();
foreach ($events as $event)
{
$event = strtolower($event);
if (!isset($this->events[$event]))
{
$this->events[$event] = [$className];
}
else
{
$this->events[$event][] = $className;
}
}
return $this;
}
/**
* Detaches an observer from the object
*
* @param Observer $observer The observer to detach
*
* @return static Ourselves, for chaining
*/
public function detach(Observer $observer)
{
$className = get_class($observer);
// Make sure this observer is already registered
if (!isset($this->observers[$className]))
{
return $this;
}
// Unregister the observable events
$events = $observer->getObservableEvents();
foreach ($events as $event)
{
$event = strtolower($event);
if (isset($this->events[$event]))
{
$key = array_search($className, $this->events[$event]);
if ($key !== false)
{
unset($this->events[$event][$key]);
if (empty($this->events[$event]))
{
unset ($this->events[$event]);
}
}
}
}
// Detach observer
unset($this->observers[$className]);
return $this;
}
/**
* Is an observer object already registered with this dispatcher?
*
* @param Observer $observer The observer to check if it's attached
*
* @return boolean
*/
public function hasObserver(Observer $observer): bool
{
$className = get_class($observer);
return $this->hasObserverClass($className);
}
/**
* Is there an observer of the specified class already registered with this dispatcher?
*
* @param string $className The observer class name to check if it's attached
*
* @return boolean
*/
public function hasObserverClass(string $className): bool
{
return isset($this->observers[$className]);
}
/**
* Returns an observer attached to this behaviours dispatcher by its class name
*
* @param string $className The class name of the observer object to return
*
* @return null|Observer
*/
public function getObserverByClass(string $className): ?Observer
{
if (!$this->hasObserverClass($className))
{
return null;
}
return $this->observers[$className];
}
/**
* Triggers an event in the attached observers
*
* @param string $event The event to attach
* @param array $args Arguments to the event handler
*
* @return array
*/
public function trigger(string $event, array $args = []): array
{
$event = strtolower($event);
$result = [];
// Make sure the event is known to us, otherwise return an empty array
if (!isset($this->events[$event]) || empty($this->events[$event]))
{
return $result;
}
foreach ($this->events[$event] as $className)
{
// Make sure the observer exists.
if (!isset($this->observers[$className]))
{
continue;
}
// Get the observer
$observer = $this->observers[$className];
// Make sure the method exists
if (!method_exists($observer, $event))
{
continue;
}
$result[] = $observer->{$event}(...$args);
}
// Return the observers' result in an array
return $result;
}
/**
* Asks each observer to handle an event based on the provided arguments. The first observer to return a non-null
* result wins. This is a *very* simplistic implementation of the Chain of Command pattern.
*
* @param string $event The event name to handle
* @param array $args The arguments to the event
*
* @return mixed Null if the event can't be handled by any observer
*/
public function chainHandle(string $event, array $args = [])
{
$event = strtolower($event);
$result = null;
// Make sure the event is known to us, otherwise return an empty array
if (!isset($this->events[$event]) || empty($this->events[$event]))
{
return $result;
}
foreach ($this->events[$event] as $className)
{
// Make sure the observer exists.
if (!isset($this->observers[$className]))
{
continue;
}
// Get the observer
$observer = $this->observers[$className];
// Make sure the method exists
if (!method_exists($observer, $event))
{
continue;
}
// Call the event handler and add its output to the return value. The switch allows for execution up to 2x
// faster than using call_user_func_array
switch (count($args))
{
case 0:
$result = $observer->{$event}();
break;
case 1:
$result = $observer->{$event}($args[0]);
break;
case 2:
$result = $observer->{$event}($args[0], $args[1]);
break;
case 3:
$result = $observer->{$event}($args[0], $args[1], $args[2]);
break;
case 4:
$result = $observer->{$event}($args[0], $args[1], $args[2], $args[3]);
break;
case 5:
$result = $observer->{$event}($args[0], $args[1], $args[2], $args[3], $args[4]);
break;
default:
$result = call_user_func_array([$observer, $event], $args);
break;
}
if (!is_null($result))
{
return $result;
}
}
// Return the observers' result in an array
return $result;
}
}
home/digilove/public_html/110/libraries/fof30/Dispatcher/Dispatcher.php 0000644 00000021162 15235524504 0021666 0 ustar 00 <?php
/**
* @package FOF
* @copyright Copyright (c)2010-2019 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU GPL version 2 or later
*/
namespace FOF30\Dispatcher;
use Exception;
use FOF30\Container\Container;
use FOF30\Controller\Controller;
use FOF30\Dispatcher\Exception\AccessForbidden;
use FOF30\TransparentAuthentication\TransparentAuthentication;
defined('_JEXEC') or die;
/**
* A generic MVC dispatcher
*
* @property-read \FOF30\Input\Input $input The input object (magic __get returns the Input from the Container)
*/
class Dispatcher
{
/** @var string The name of the default view, in case none is specified */
public $defaultView = null;
/** @var array Local cache of the dispatcher configuration */
protected $config = array();
/** @var Container The container we belong to */
protected $container = null;
/** @var string The view which will be rendered by the dispatcher */
protected $view = null;
/** @var string The layout for rendering the view */
protected $layout = null;
/** @var Controller The controller which will be used */
protected $controller = null;
/** @var bool Is this user transparently logged in? */
protected $isTransparentlyLoggedIn = false;
/**
* Public constructor
*
* The $config array can contain the following optional values:
* defaultView string The view to render if none is specified in $input
*
* Do note that $config is passed to the Controller and through it to the Model and View. Please see these classes
* for more information on the configuration variables they accept.
*
* @param \FOF30\Container\Container $container
* @param array $config
*/
public function __construct(Container $container, array $config = array())
{
$this->container = $container;
$this->config = $config;
$this->defaultView = $container->appConfig->get('dispatcher.defaultView', $this->defaultView);
if (isset($config['defaultView']))
{
$this->defaultView = $config['defaultView'];
}
// Get the default values for the view and layout names
$this->view = $this->input->getCmd('view', null);
$this->layout = $this->input->getCmd('layout', null);
// Not redundant; you may pass an empty but non-null view which is invalid, so we need the fallback
if (empty($this->view))
{
$this->view = $this->defaultView;
$this->container->input->set('view', $this->view);
}
}
/**
* Magic get method. Handles magic properties:
* $this->input mapped to $this->container->input
*
* @param string $name The property to fetch
*
* @return mixed|null
*/
public function __get($name)
{
// Handle $this->input
if ($name == 'input')
{
return $this->container->input;
}
// Property not found; raise error
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/**
* The main code of the Dispatcher. It spawns the necessary controller and
* runs it.
*
* @return void
*
* @throws AccessForbidden When the access is forbidden
*/
public function dispatch()
{
// Load the translations for this component;
$this->container->platform->loadTranslations($this->container->componentName);
// Perform transparent authentication
if ($this->container->platform->getUser()->guest)
{
$this->transparentAuthenticationLogin();
}
// Get the event names (different for CLI)
$onBeforeEventName = 'onBeforeDispatch';
$onAfterEventName = 'onAfterDispatch';
if ($this->container->platform->isCli())
{
$onBeforeEventName = 'onBeforeDispatchCLI';
$onAfterEventName = 'onAfterDispatchCLI';
}
try
{
$result = $this->triggerEvent($onBeforeEventName);
$error = '';
}
catch (\Exception $e)
{
$result = false;
$error = $e->getMessage();
}
if ($result === false)
{
if ($this->container->platform->isCli())
{
$this->container->platform->setHeader('Status', '403 Forbidden', true);
}
$this->transparentAuthenticationLogout();
$this->container->platform->showErrorPage(new AccessForbidden);
}
// Get and execute the controller
$view = $this->input->getCmd('view', $this->defaultView);
$task = $this->input->getCmd('task', 'default');
if (empty($task))
{
$task = 'default';
$this->input->set('task', $task);
}
try
{
$this->controller = $this->container->factory->controller($view, $this->config);
$status = $this->controller->execute($task);
}
catch (Exception $e)
{
$this->container->platform->showErrorPage($e);
// Redundant; just to make code sniffers happy
return;
}
if ($status !== false)
{
try
{
$this->triggerEvent($onAfterEventName);
}
catch (\Exception $e)
{
$status = false;
}
}
if (($status === false))
{
if ($this->container->platform->isCli())
{
$this->container->platform->setHeader('Status', '403 Forbidden', true);
}
$this->transparentAuthenticationLogout();
$this->container->platform->showErrorPage(new AccessForbidden);
}
$this->transparentAuthenticationLogout();
$this->controller->redirect();
}
/**
* Returns a reference to the Controller object currently in use by the dispatcher
*
* @return Controller
*/
public function &getController()
{
return $this->controller;
}
/**
* Triggers an object-specific event. The event runs both locally –if a suitable method exists– and through the
* Joomla! plugin system. A true/false return value is expected. The first false return cancels the event.
*
* EXAMPLE
* Component: com_foobar, Object name: item, Event: onBeforeDispatch, Arguments: array(123, 456)
* The event calls:
* 1. $this->onBeforeDispatch(123, 456)
* 2. Joomla! plugin event onComFoobarDispatcherBeforeDispatch($this, 123, 456)
*
* @param string $event The name of the event, typically named onPredicateVerb e.g. onBeforeKick
* @param array $arguments The arguments to pass to the event handlers
*
* @return bool
*/
protected function triggerEvent($event, array $arguments = array())
{
$result = true;
// If there is an object method for this event, call it
if (method_exists($this, $event))
{
switch (count($arguments))
{
case 0:
$result = $this->{$event}();
break;
case 1:
$result = $this->{$event}($arguments[0]);
break;
case 2:
$result = $this->{$event}($arguments[0], $arguments[1]);
break;
case 3:
$result = $this->{$event}($arguments[0], $arguments[1], $arguments[2]);
break;
case 4:
$result = $this->{$event}($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
break;
case 5:
$result = $this->{$event}($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
break;
default:
$result = call_user_func_array(array($this, $event), $arguments);
break;
}
}
if ($result === false)
{
return false;
}
// All other event handlers live outside this object, therefore they need to be passed a reference to this
// objects as the first argument.
array_unshift($arguments, $this);
// If we have an "on" prefix for the event (e.g. onFooBar) remove it and stash it for later.
$prefix = '';
if (substr($event, 0, 2) == 'on')
{
$prefix = 'on';
$event = substr($event, 2);
}
// Get the component/model prefix for the event
$prefix .= 'Com' . ucfirst($this->container->bareComponentName) . 'Dispatcher';
// The event name will be something like onComFoobarItemsBeforeSomething
$event = $prefix . $event;
// Call the Joomla! plugins
$results = $this->container->platform->runPlugins($event, $arguments);
if (!empty($results))
{
foreach ($results as $result)
{
if ($result === false)
{
return false;
}
}
}
return true;
}
/**
* Handles the transparent authentication log in
*/
protected function transparentAuthenticationLogin()
{
/** @var TransparentAuthentication $transparentAuth */
$transparentAuth = $this->container->transparentAuth;
$authInfo = $transparentAuth->getTransparentAuthenticationCredentials();
if (empty($authInfo))
{
return;
}
$this->isTransparentlyLoggedIn = $this->container->platform->loginUser($authInfo);
}
/**
* Handles the transparent authentication log out
*/
protected function transparentAuthenticationLogout()
{
if (!$this->isTransparentlyLoggedIn)
{
return;
}
/** @var TransparentAuthentication $transparentAuth */
$transparentAuth = $this->container->transparentAuth;
if (!$transparentAuth->getLogoutOnExit())
{
return;
}
$this->container->platform->logoutUser();
}
}
home/digilove/public_html/110/libraries/fof30/Event/Dispatcher.php 0000644 00000016075 15235526022 0020665 0 ustar 00 <?php
/**
* @package FOF
* @copyright Copyright (c)2010-2019 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU GPL version 2 or later
*/
namespace FOF30\Event;
use FOF30\Container\Container;
defined('_JEXEC') or die;
class Dispatcher implements Observable
{
/** @var Container The container this event dispatcher is attached to */
protected $container = null;
/** @var array The observers attached to the dispatcher */
protected $observers = array();
/** @var array Maps events to observers */
protected $events = array();
/**
* Public constructor
*
* @param Container $container The container this event dispatcher is attached to
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Returns the container this event dispatcher is attached to
*
* @return Container
*/
public function getContainer()
{
return $this->container;
}
/**
* Attaches an observer to the object
*
* @param Observer $observer The observer to attach
*
* @return Dispatcher Ourselves, for chaining
*/
public function attach(Observer $observer)
{
$className = get_class($observer);
// Make sure this observer is not already registered
if (isset($this->observers[$className]))
{
return $this;
}
// Attach observer
$this->observers[$className] = $observer;
// Register the observable events
$events = $observer->getObservableEvents();
foreach ($events as $event)
{
$event = strtolower($event);
if (!isset($this->events[$event]))
{
$this->events[$event] = array($className);
}
else
{
$this->events[$event][] = $className;
}
}
return $this;
}
/**
* Detaches an observer from the object
*
* @param Observer $observer The observer to detach
*
* @return Dispatcher Ourselves, for chaining
*/
public function detach(Observer $observer)
{
$className = get_class($observer);
// Make sure this observer is already registered
if (!isset($this->observers[$className]))
{
return $this;
}
// Unregister the observable events
$events = $observer->getObservableEvents();
foreach ($events as $event)
{
$event = strtolower($event);
if (isset($this->events[$event]))
{
$key = array_search($className, $this->events[$event]);
if ($key !== false)
{
unset($this->events[$event][$key]);
if (empty($this->events[$event]))
{
unset ($this->events[$event]);
}
}
}
}
// Detach observer
unset($this->observers[$className]);
return $this;
}
/**
* Is an observer object already registered with this dispatcher?
*
* @param Observer $observer The observer to check if it's attached
*
* @return boolean
*/
public function hasObserver(Observer $observer)
{
$className = get_class($observer);
return $this->hasObserverClass($className);
}
/**
* Is there an observer of the specified class already registered with this dispatcher?
*
* @param string $className The observer class name to check if it's attached
*
* @return boolean
*/
public function hasObserverClass($className)
{
return isset($this->observers[$className]);
}
/**
* Returns an observer attached to this behaviours dispatcher by its class name
*
* @param string $className The class name of the observer object to return
*
* @return null|Observer
*/
public function getObserverByClass($className)
{
if (!$this->hasObserverClass($className))
{
return null;
}
return $this->observers[$className];
}
/**
* Triggers an event in the attached observers
*
* @param string $event The event to attach
* @param array $args Arguments to the event handler
*
* @return array
*/
public function trigger($event, array $args = array())
{
$event = strtolower($event);
$result = array();
// Make sure the event is known to us, otherwise return an empty array
if (!isset($this->events[$event]) || empty($this->events[$event]))
{
return $result;
}
foreach ($this->events[$event] as $className)
{
// Make sure the observer exists.
if (!isset($this->observers[$className]))
{
continue;
}
// Get the observer
$observer = $this->observers[$className];
// Make sure the method exists
if (!method_exists($observer, $event))
{
continue;
}
// Call the event handler and add its output to the return value. The switch allows for execution up to 2x
// faster than using call_user_func_array
switch (count($args))
{
case 0:
$result[] = $observer->{$event}();
break;
case 1:
$result[] = $observer->{$event}($args[0]);
break;
case 2:
$result[] = $observer->{$event}($args[0], $args[1]);
break;
case 3:
$result[] = $observer->{$event}($args[0], $args[1], $args[2]);
break;
case 4:
$result[] = $observer->{$event}($args[0], $args[1], $args[2], $args[3]);
break;
case 5:
$result[] = $observer->{$event}($args[0], $args[1], $args[2], $args[3], $args[4]);
break;
default:
$result[] = call_user_func_array(array($observer, $event), $args);
break;
}
}
// Return the observers' result in an array
return $result;
}
/**
* Asks each observer to handle an event based on the provided arguments. The first observer to return a non-null
* result wins. This is a *very* simplistic implementation of the Chain of Command pattern.
*
* @param string $event The event name to handle
* @param array $args The arguments to the event
*
* @return mixed Null if the event can't be handled by any observer
*/
public function chainHandle($event, $args = array())
{
$event = strtolower($event);
$result = null;
// Make sure the event is known to us, otherwise return an empty array
if (!isset($this->events[$event]) || empty($this->events[$event]))
{
return $result;
}
foreach ($this->events[$event] as $className)
{
// Make sure the observer exists.
if (!isset($this->observers[$className]))
{
continue;
}
// Get the observer
$observer = $this->observers[$className];
// Make sure the method exists
if (!method_exists($observer, $event))
{
continue;
}
// Call the event handler and add its output to the return value. The switch allows for execution up to 2x
// faster than using call_user_func_array
switch (count($args))
{
case 0:
$result = $observer->{$event}();
break;
case 1:
$result = $observer->{$event}($args[0]);
break;
case 2:
$result = $observer->{$event}($args[0], $args[1]);
break;
case 3:
$result = $observer->{$event}($args[0], $args[1], $args[2]);
break;
case 4:
$result = $observer->{$event}($args[0], $args[1], $args[2], $args[3]);
break;
case 5:
$result = $observer->{$event}($args[0], $args[1], $args[2], $args[3], $args[4]);
break;
default:
$result = call_user_func_array(array($observer, $event), $args);
break;
}
if (!is_null($result))
{
return $result;
}
}
// Return the observers' result in an array
return $result;
}
}
home/digilove/public_html/libraries/fof40/Event/Dispatcher.php 0000644 00000014547 15235572215 0020374 0 ustar 00 <?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\Event;
defined('_JEXEC') || die;
use FOF40\Container\Container;
class Dispatcher implements Observable
{
/** @var Container The container this event dispatcher is attached to */
protected $container;
/** @var array The observers attached to the dispatcher */
protected $observers = [];
/** @var array Maps events to observers */
protected $events = [];
/**
* Public constructor
*
* @param Container $container The container this event dispatcher is attached to
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Returns the container this event dispatcher is attached to
*
* @return Container
*/
public function getContainer(): Container
{
return $this->container;
}
/**
* Attaches an observer to the object
*
* @param Observer $observer The observer to attach
*
* @return static Ourselves, for chaining
*/
public function attach(Observer $observer)
{
$className = get_class($observer);
// Make sure this observer is not already registered
if (isset($this->observers[$className]))
{
return $this;
}
// Attach observer
$this->observers[$className] = $observer;
// Register the observable events
$events = $observer->getObservableEvents();
foreach ($events as $event)
{
$event = strtolower($event);
if (!isset($this->events[$event]))
{
$this->events[$event] = [$className];
}
else
{
$this->events[$event][] = $className;
}
}
return $this;
}
/**
* Detaches an observer from the object
*
* @param Observer $observer The observer to detach
*
* @return static Ourselves, for chaining
*/
public function detach(Observer $observer)
{
$className = get_class($observer);
// Make sure this observer is already registered
if (!isset($this->observers[$className]))
{
return $this;
}
// Unregister the observable events
$events = $observer->getObservableEvents();
foreach ($events as $event)
{
$event = strtolower($event);
if (isset($this->events[$event]))
{
$key = array_search($className, $this->events[$event]);
if ($key !== false)
{
unset($this->events[$event][$key]);
if (empty($this->events[$event]))
{
unset ($this->events[$event]);
}
}
}
}
// Detach observer
unset($this->observers[$className]);
return $this;
}
/**
* Is an observer object already registered with this dispatcher?
*
* @param Observer $observer The observer to check if it's attached
*
* @return boolean
*/
public function hasObserver(Observer $observer): bool
{
$className = get_class($observer);
return $this->hasObserverClass($className);
}
/**
* Is there an observer of the specified class already registered with this dispatcher?
*
* @param string $className The observer class name to check if it's attached
*
* @return boolean
*/
public function hasObserverClass(string $className): bool
{
return isset($this->observers[$className]);
}
/**
* Returns an observer attached to this behaviours dispatcher by its class name
*
* @param string $className The class name of the observer object to return
*
* @return null|Observer
*/
public function getObserverByClass(string $className): ?Observer
{
if (!$this->hasObserverClass($className))
{
return null;
}
return $this->observers[$className];
}
/**
* Triggers an event in the attached observers
*
* @param string $event The event to attach
* @param array $args Arguments to the event handler
*
* @return array
*/
public function trigger(string $event, array $args = []): array
{
$event = strtolower($event);
$result = [];
// Make sure the event is known to us, otherwise return an empty array
if (!isset($this->events[$event]) || empty($this->events[$event]))
{
return $result;
}
foreach ($this->events[$event] as $className)
{
// Make sure the observer exists.
if (!isset($this->observers[$className]))
{
continue;
}
// Get the observer
$observer = $this->observers[$className];
// Make sure the method exists
if (!method_exists($observer, $event))
{
continue;
}
$result[] = $observer->{$event}(...$args);
}
// Return the observers' result in an array
return $result;
}
/**
* Asks each observer to handle an event based on the provided arguments. The first observer to return a non-null
* result wins. This is a *very* simplistic implementation of the Chain of Command pattern.
*
* @param string $event The event name to handle
* @param array $args The arguments to the event
*
* @return mixed Null if the event can't be handled by any observer
*/
public function chainHandle(string $event, array $args = [])
{
$event = strtolower($event);
$result = null;
// Make sure the event is known to us, otherwise return an empty array
if (!isset($this->events[$event]) || empty($this->events[$event]))
{
return $result;
}
foreach ($this->events[$event] as $className)
{
// Make sure the observer exists.
if (!isset($this->observers[$className]))
{
continue;
}
// Get the observer
$observer = $this->observers[$className];
// Make sure the method exists
if (!method_exists($observer, $event))
{
continue;
}
// Call the event handler and add its output to the return value. The switch allows for execution up to 2x
// faster than using call_user_func_array
switch (count($args))
{
case 0:
$result = $observer->{$event}();
break;
case 1:
$result = $observer->{$event}($args[0]);
break;
case 2:
$result = $observer->{$event}($args[0], $args[1]);
break;
case 3:
$result = $observer->{$event}($args[0], $args[1], $args[2]);
break;
case 4:
$result = $observer->{$event}($args[0], $args[1], $args[2], $args[3]);
break;
case 5:
$result = $observer->{$event}($args[0], $args[1], $args[2], $args[3], $args[4]);
break;
default:
$result = call_user_func_array([$observer, $event], $args);
break;
}
if (!is_null($result))
{
return $result;
}
}
// Return the observers' result in an array
return $result;
}
}
home/digilove/public_html/libraries/fof30/Dispatcher/Dispatcher.php 0000644 00000021162 15235576551 0021376 0 ustar 00 <?php
/**
* @package FOF
* @copyright Copyright (c)2010-2019 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU GPL version 2 or later
*/
namespace FOF30\Dispatcher;
use Exception;
use FOF30\Container\Container;
use FOF30\Controller\Controller;
use FOF30\Dispatcher\Exception\AccessForbidden;
use FOF30\TransparentAuthentication\TransparentAuthentication;
defined('_JEXEC') or die;
/**
* A generic MVC dispatcher
*
* @property-read \FOF30\Input\Input $input The input object (magic __get returns the Input from the Container)
*/
class Dispatcher
{
/** @var string The name of the default view, in case none is specified */
public $defaultView = null;
/** @var array Local cache of the dispatcher configuration */
protected $config = array();
/** @var Container The container we belong to */
protected $container = null;
/** @var string The view which will be rendered by the dispatcher */
protected $view = null;
/** @var string The layout for rendering the view */
protected $layout = null;
/** @var Controller The controller which will be used */
protected $controller = null;
/** @var bool Is this user transparently logged in? */
protected $isTransparentlyLoggedIn = false;
/**
* Public constructor
*
* The $config array can contain the following optional values:
* defaultView string The view to render if none is specified in $input
*
* Do note that $config is passed to the Controller and through it to the Model and View. Please see these classes
* for more information on the configuration variables they accept.
*
* @param \FOF30\Container\Container $container
* @param array $config
*/
public function __construct(Container $container, array $config = array())
{
$this->container = $container;
$this->config = $config;
$this->defaultView = $container->appConfig->get('dispatcher.defaultView', $this->defaultView);
if (isset($config['defaultView']))
{
$this->defaultView = $config['defaultView'];
}
// Get the default values for the view and layout names
$this->view = $this->input->getCmd('view', null);
$this->layout = $this->input->getCmd('layout', null);
// Not redundant; you may pass an empty but non-null view which is invalid, so we need the fallback
if (empty($this->view))
{
$this->view = $this->defaultView;
$this->container->input->set('view', $this->view);
}
}
/**
* Magic get method. Handles magic properties:
* $this->input mapped to $this->container->input
*
* @param string $name The property to fetch
*
* @return mixed|null
*/
public function __get($name)
{
// Handle $this->input
if ($name == 'input')
{
return $this->container->input;
}
// Property not found; raise error
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/**
* The main code of the Dispatcher. It spawns the necessary controller and
* runs it.
*
* @return void
*
* @throws AccessForbidden When the access is forbidden
*/
public function dispatch()
{
// Load the translations for this component;
$this->container->platform->loadTranslations($this->container->componentName);
// Perform transparent authentication
if ($this->container->platform->getUser()->guest)
{
$this->transparentAuthenticationLogin();
}
// Get the event names (different for CLI)
$onBeforeEventName = 'onBeforeDispatch';
$onAfterEventName = 'onAfterDispatch';
if ($this->container->platform->isCli())
{
$onBeforeEventName = 'onBeforeDispatchCLI';
$onAfterEventName = 'onAfterDispatchCLI';
}
try
{
$result = $this->triggerEvent($onBeforeEventName);
$error = '';
}
catch (\Exception $e)
{
$result = false;
$error = $e->getMessage();
}
if ($result === false)
{
if ($this->container->platform->isCli())
{
$this->container->platform->setHeader('Status', '403 Forbidden', true);
}
$this->transparentAuthenticationLogout();
$this->container->platform->showErrorPage(new AccessForbidden);
}
// Get and execute the controller
$view = $this->input->getCmd('view', $this->defaultView);
$task = $this->input->getCmd('task', 'default');
if (empty($task))
{
$task = 'default';
$this->input->set('task', $task);
}
try
{
$this->controller = $this->container->factory->controller($view, $this->config);
$status = $this->controller->execute($task);
}
catch (Exception $e)
{
$this->container->platform->showErrorPage($e);
// Redundant; just to make code sniffers happy
return;
}
if ($status !== false)
{
try
{
$this->triggerEvent($onAfterEventName);
}
catch (\Exception $e)
{
$status = false;
}
}
if (($status === false))
{
if ($this->container->platform->isCli())
{
$this->container->platform->setHeader('Status', '403 Forbidden', true);
}
$this->transparentAuthenticationLogout();
$this->container->platform->showErrorPage(new AccessForbidden);
}
$this->transparentAuthenticationLogout();
$this->controller->redirect();
}
/**
* Returns a reference to the Controller object currently in use by the dispatcher
*
* @return Controller
*/
public function &getController()
{
return $this->controller;
}
/**
* Triggers an object-specific event. The event runs both locally –if a suitable method exists– and through the
* Joomla! plugin system. A true/false return value is expected. The first false return cancels the event.
*
* EXAMPLE
* Component: com_foobar, Object name: item, Event: onBeforeDispatch, Arguments: array(123, 456)
* The event calls:
* 1. $this->onBeforeDispatch(123, 456)
* 2. Joomla! plugin event onComFoobarDispatcherBeforeDispatch($this, 123, 456)
*
* @param string $event The name of the event, typically named onPredicateVerb e.g. onBeforeKick
* @param array $arguments The arguments to pass to the event handlers
*
* @return bool
*/
protected function triggerEvent($event, array $arguments = array())
{
$result = true;
// If there is an object method for this event, call it
if (method_exists($this, $event))
{
switch (count($arguments))
{
case 0:
$result = $this->{$event}();
break;
case 1:
$result = $this->{$event}($arguments[0]);
break;
case 2:
$result = $this->{$event}($arguments[0], $arguments[1]);
break;
case 3:
$result = $this->{$event}($arguments[0], $arguments[1], $arguments[2]);
break;
case 4:
$result = $this->{$event}($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
break;
case 5:
$result = $this->{$event}($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
break;
default:
$result = call_user_func_array(array($this, $event), $arguments);
break;
}
}
if ($result === false)
{
return false;
}
// All other event handlers live outside this object, therefore they need to be passed a reference to this
// objects as the first argument.
array_unshift($arguments, $this);
// If we have an "on" prefix for the event (e.g. onFooBar) remove it and stash it for later.
$prefix = '';
if (substr($event, 0, 2) == 'on')
{
$prefix = 'on';
$event = substr($event, 2);
}
// Get the component/model prefix for the event
$prefix .= 'Com' . ucfirst($this->container->bareComponentName) . 'Dispatcher';
// The event name will be something like onComFoobarItemsBeforeSomething
$event = $prefix . $event;
// Call the Joomla! plugins
$results = $this->container->platform->runPlugins($event, $arguments);
if (!empty($results))
{
foreach ($results as $result)
{
if ($result === false)
{
return false;
}
}
}
return true;
}
/**
* Handles the transparent authentication log in
*/
protected function transparentAuthenticationLogin()
{
/** @var TransparentAuthentication $transparentAuth */
$transparentAuth = $this->container->transparentAuth;
$authInfo = $transparentAuth->getTransparentAuthenticationCredentials();
if (empty($authInfo))
{
return;
}
$this->isTransparentlyLoggedIn = $this->container->platform->loginUser($authInfo);
}
/**
* Handles the transparent authentication log out
*/
protected function transparentAuthenticationLogout()
{
if (!$this->isTransparentlyLoggedIn)
{
return;
}
/** @var TransparentAuthentication $transparentAuth */
$transparentAuth = $this->container->transparentAuth;
if (!$transparentAuth->getLogoutOnExit())
{
return;
}
$this->container->platform->logoutUser();
}
}
home/digilove/public_html/libraries/fof40/Configuration/Domain/Dispatcher.php 0000644 00000003242 15237674557 0023335 0 ustar 00 <?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\Configuration\Domain;
use SimpleXMLElement;
defined('_JEXEC') || die;
/**
* Configuration parser for the dispatcher-specific settings
*
* @since 2.1
*/
class Dispatcher implements DomainInterface
{
/**
* Parse the XML data, adding them to the $ret array
*
* @param SimpleXMLElement $xml The XML data of the component's configuration area
* @param array &$ret The parsed data, in the form of a hash array
*
* @return void
*/
public function parseDomain(SimpleXMLElement $xml, array &$ret): void
{
// Initialise
$ret['dispatcher'] = [];
// Parse the dispatcher configuration
$dispatcherData = $xml->dispatcher;
// Sanity check
if (empty($dispatcherData))
{
return;
}
$options = $xml->xpath('dispatcher/option');
foreach ($options as $option)
{
$key = (string) $option['name'];
$ret['dispatcher'][$key] = (string) $option;
}
}
/**
* Return a configuration variable
*
* @param string &$configuration Configuration variables (hashed array)
* @param string $var The variable we want to fetch
* @param mixed $default Default value
*
* @return mixed The variable's value
*/
public function get(array &$configuration, string $var, $default = null)
{
if ($var == '*')
{
return $configuration['dispatcher'];
}
if (isset($configuration['dispatcher'][$var]))
{
return $configuration['dispatcher'][$var];
}
else
{
return $default;
}
}
}
home/digilove/public_html/110/libraries/fof40/Dispatcher/Dispatcher.php 0000644 00000021473 15240001373 0021662 0 ustar 00 <?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\Dispatcher;
defined('_JEXEC') || die;
use Exception;
use FOF40\Container\Container;
use FOF40\Controller\Controller;
use FOF40\Dispatcher\Exception\AccessForbidden;
use FOF40\TransparentAuthentication\TransparentAuthentication;
/**
* A generic MVC dispatcher
*
* @property-read \FOF40\Input\Input $input The input object (magic __get returns the Input from the Container)
*/
class Dispatcher
{
/** @var string The name of the default view, in case none is specified */
public $defaultView;
/** @var array Local cache of the dispatcher configuration */
protected $config = [];
/** @var Container The container we belong to */
protected $container;
/** @var string The view which will be rendered by the dispatcher */
protected $view;
/** @var string The layout for rendering the view */
protected $layout;
/** @var Controller The controller which will be used */
protected $controller;
/** @var bool Is this user transparently logged in? */
protected $isTransparentlyLoggedIn = false;
/**
* Public constructor
*
* The $config array can contain the following optional values:
* defaultView string The view to render if none is specified in $input
*
* Do note that $config is passed to the Controller and through it to the Model and View. Please see these classes
* for more information on the configuration variables they accept.
*
* @param Container $container
* @param array $config
*/
public function __construct(Container $container, array $config = [])
{
$this->container = $container;
$this->config = $config;
$this->defaultView = $container->appConfig->get('dispatcher.defaultView', $this->defaultView);
if (isset($config['defaultView']))
{
$this->defaultView = $config['defaultView'];
}
$this->supportCustomViewAndTaskParameters();
// Get the default values for the view and layout names
$this->view = $this->input->getCmd('view', null);
$this->layout = $this->input->getCmd('layout', null);
// Not redundant; you may pass an empty but non-null view which is invalid, so we need the fallback
if (empty($this->view))
{
$this->view = $this->defaultView;
$this->container->input->set('view', $this->view);
}
}
/**
* Magic get method. Handles magic properties:
* $this->input mapped to $this->container->input
*
* @param string $name The property to fetch
*
* @return mixed|null
*/
public function __get(string $name)
{
// Handle $this->input
if ($name == 'input')
{
return $this->container->input;
}
// Property not found; raise error
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/**
* The main code of the Dispatcher. It spawns the necessary controller and
* runs it.
*
* @return void
*
* @throws AccessForbidden When the access is forbidden
* @throws Exception For displaying an error page
*/
public function dispatch(): void
{
// Load the translations for this component;
$this->container->platform->loadTranslations($this->container->componentName);
// Perform transparent authentication
if ($this->container->platform->getUser()->guest)
{
$this->transparentAuthenticationLogin();
}
// Get the event names (different for CLI)
$onBeforeEventName = 'onBeforeDispatch';
$onAfterEventName = 'onAfterDispatch';
if ($this->container->platform->isCli())
{
$onBeforeEventName = 'onBeforeDispatchCLI';
$onAfterEventName = 'onAfterDispatchCLI';
}
try
{
$result = $this->triggerEvent($onBeforeEventName);
$error = '';
}
catch (\Exception $e)
{
$result = false;
$error = $e->getMessage();
}
if ($result === false)
{
if ($this->container->platform->isCli())
{
$this->container->platform->setHeader('Status', '403 Forbidden', true);
}
$this->transparentAuthenticationLogout();
$this->container->platform->showErrorPage(new AccessForbidden);
}
// Get and execute the controller
$view = $this->input->getCmd('view', $this->defaultView);
$task = $this->input->getCmd('task', 'default');
if (empty($task))
{
$task = 'default';
$this->input->set('task', $task);
}
try
{
$this->controller = $this->container->factory->controller($view, $this->config);
$status = $this->controller->execute($task);
}
catch (Exception $e)
{
$this->container->platform->showErrorPage($e);
// Redundant; just to make code sniffers happy
return;
}
if ($status !== false)
{
try
{
$this->triggerEvent($onAfterEventName);
}
catch (\Exception $e)
{
$status = false;
}
}
if ($status === false)
{
if ($this->container->platform->isCli())
{
$this->container->platform->setHeader('Status', '403 Forbidden', true);
}
$this->transparentAuthenticationLogout();
$this->container->platform->showErrorPage(new AccessForbidden);
}
$this->transparentAuthenticationLogout();
$this->controller->redirect();
}
/**
* Returns a reference to the Controller object currently in use by the dispatcher
*
* @return Controller|null
*/
public function &getController(): ?Controller
{
return $this->controller;
}
/**
* Triggers an object-specific event. The event runs both locally –if a suitable method exists– and through the
* Joomla! plugin system. A true/false return value is expected. The first false return cancels the event.
*
* EXAMPLE
* Component: com_foobar, Object name: item, Event: onBeforeDispatch, Arguments: array(123, 456)
* The event calls:
* 1. $this->onBeforeDispatch(123, 456)
* 2. Joomla! plugin event onComFoobarDispatcherBeforeDispatch($this, 123, 456)
*
* @param string $event The name of the event, typically named onPredicateVerb e.g. onBeforeKick
* @param array $arguments The arguments to pass to the event handlers
*
* @return bool
*/
protected function triggerEvent(string $event, array $arguments = []): bool
{
$result = true;
// If there is an object method for this event, call it
if (method_exists($this, $event))
{
$result = $this->{$event}(...$arguments);
}
if ($result === false)
{
return false;
}
// All other event handlers live outside this object, therefore they need to be passed a reference to this
// objects as the first argument.
array_unshift($arguments, $this);
// If we have an "on" prefix for the event (e.g. onFooBar) remove it and stash it for later.
$prefix = '';
if (substr($event, 0, 2) == 'on')
{
$prefix = 'on';
$event = substr($event, 2);
}
// Get the component/model prefix for the event
$prefix .= 'Com' . ucfirst($this->container->bareComponentName) . 'Dispatcher';
// The event name will be something like onComFoobarItemsBeforeSomething
$event = $prefix . $event;
// Call the Joomla! plugins
$results = $this->container->platform->runPlugins($event, $arguments);
return !in_array(false, $results, true);
}
/**
* Handles the transparent authentication log in
*/
protected function transparentAuthenticationLogin(): void
{
/** @var TransparentAuthentication $transparentAuth */
$transparentAuth = $this->container->transparentAuth;
$authInfo = $transparentAuth->getTransparentAuthenticationCredentials();
if (empty($authInfo))
{
return;
}
$this->isTransparentlyLoggedIn = $this->container->platform->loginUser($authInfo);
}
/**
* Handles the transparent authentication log out
*/
protected function transparentAuthenticationLogout(): void
{
if (!$this->isTransparentlyLoggedIn)
{
return;
}
/** @var TransparentAuthentication $transparentAuth */
$transparentAuth = $this->container->transparentAuth;
if (!$transparentAuth->getLogoutOnExit())
{
return;
}
$this->container->platform->logoutUser();
}
/**
* Adds support for akview/aktask in lieu of view and task.
*
* This is for future-proofing FOF in case Joomla assigns special meaning to view and task, e.g. by trying to find a
* specific controller / task class instead of letting the component's front-end router handle it. If that happens
* FOF components can have a single Joomla-compatible view/task which launches the Dispatcher and perform internal
* routing using akview/aktask.
*
* @return void
* @since 3.6.3
*/
private function supportCustomViewAndTaskParameters()
{
$view = $this->input->getCmd('akview', null);
$task = $this->input->getCmd('aktask', null);
if (!is_null($view))
{
$this->input->remove('akview');
$this->input->set('view', $view);
}
if (!is_null($task))
{
$this->input->remove('aktask');
$this->input->set('task', $task);
}
}
}
home/digilove/public_html/libraries/fof30/Event/Dispatcher.php 0000644 00000016075 15241616115 0020364 0 ustar 00 <?php
/**
* @package FOF
* @copyright Copyright (c)2010-2019 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU GPL version 2 or later
*/
namespace FOF30\Event;
use FOF30\Container\Container;
defined('_JEXEC') or die;
class Dispatcher implements Observable
{
/** @var Container The container this event dispatcher is attached to */
protected $container = null;
/** @var array The observers attached to the dispatcher */
protected $observers = array();
/** @var array Maps events to observers */
protected $events = array();
/**
* Public constructor
*
* @param Container $container The container this event dispatcher is attached to
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Returns the container this event dispatcher is attached to
*
* @return Container
*/
public function getContainer()
{
return $this->container;
}
/**
* Attaches an observer to the object
*
* @param Observer $observer The observer to attach
*
* @return Dispatcher Ourselves, for chaining
*/
public function attach(Observer $observer)
{
$className = get_class($observer);
// Make sure this observer is not already registered
if (isset($this->observers[$className]))
{
return $this;
}
// Attach observer
$this->observers[$className] = $observer;
// Register the observable events
$events = $observer->getObservableEvents();
foreach ($events as $event)
{
$event = strtolower($event);
if (!isset($this->events[$event]))
{
$this->events[$event] = array($className);
}
else
{
$this->events[$event][] = $className;
}
}
return $this;
}
/**
* Detaches an observer from the object
*
* @param Observer $observer The observer to detach
*
* @return Dispatcher Ourselves, for chaining
*/
public function detach(Observer $observer)
{
$className = get_class($observer);
// Make sure this observer is already registered
if (!isset($this->observers[$className]))
{
return $this;
}
// Unregister the observable events
$events = $observer->getObservableEvents();
foreach ($events as $event)
{
$event = strtolower($event);
if (isset($this->events[$event]))
{
$key = array_search($className, $this->events[$event]);
if ($key !== false)
{
unset($this->events[$event][$key]);
if (empty($this->events[$event]))
{
unset ($this->events[$event]);
}
}
}
}
// Detach observer
unset($this->observers[$className]);
return $this;
}
/**
* Is an observer object already registered with this dispatcher?
*
* @param Observer $observer The observer to check if it's attached
*
* @return boolean
*/
public function hasObserver(Observer $observer)
{
$className = get_class($observer);
return $this->hasObserverClass($className);
}
/**
* Is there an observer of the specified class already registered with this dispatcher?
*
* @param string $className The observer class name to check if it's attached
*
* @return boolean
*/
public function hasObserverClass($className)
{
return isset($this->observers[$className]);
}
/**
* Returns an observer attached to this behaviours dispatcher by its class name
*
* @param string $className The class name of the observer object to return
*
* @return null|Observer
*/
public function getObserverByClass($className)
{
if (!$this->hasObserverClass($className))
{
return null;
}
return $this->observers[$className];
}
/**
* Triggers an event in the attached observers
*
* @param string $event The event to attach
* @param array $args Arguments to the event handler
*
* @return array
*/
public function trigger($event, array $args = array())
{
$event = strtolower($event);
$result = array();
// Make sure the event is known to us, otherwise return an empty array
if (!isset($this->events[$event]) || empty($this->events[$event]))
{
return $result;
}
foreach ($this->events[$event] as $className)
{
// Make sure the observer exists.
if (!isset($this->observers[$className]))
{
continue;
}
// Get the observer
$observer = $this->observers[$className];
// Make sure the method exists
if (!method_exists($observer, $event))
{
continue;
}
// Call the event handler and add its output to the return value. The switch allows for execution up to 2x
// faster than using call_user_func_array
switch (count($args))
{
case 0:
$result[] = $observer->{$event}();
break;
case 1:
$result[] = $observer->{$event}($args[0]);
break;
case 2:
$result[] = $observer->{$event}($args[0], $args[1]);
break;
case 3:
$result[] = $observer->{$event}($args[0], $args[1], $args[2]);
break;
case 4:
$result[] = $observer->{$event}($args[0], $args[1], $args[2], $args[3]);
break;
case 5:
$result[] = $observer->{$event}($args[0], $args[1], $args[2], $args[3], $args[4]);
break;
default:
$result[] = call_user_func_array(array($observer, $event), $args);
break;
}
}
// Return the observers' result in an array
return $result;
}
/**
* Asks each observer to handle an event based on the provided arguments. The first observer to return a non-null
* result wins. This is a *very* simplistic implementation of the Chain of Command pattern.
*
* @param string $event The event name to handle
* @param array $args The arguments to the event
*
* @return mixed Null if the event can't be handled by any observer
*/
public function chainHandle($event, $args = array())
{
$event = strtolower($event);
$result = null;
// Make sure the event is known to us, otherwise return an empty array
if (!isset($this->events[$event]) || empty($this->events[$event]))
{
return $result;
}
foreach ($this->events[$event] as $className)
{
// Make sure the observer exists.
if (!isset($this->observers[$className]))
{
continue;
}
// Get the observer
$observer = $this->observers[$className];
// Make sure the method exists
if (!method_exists($observer, $event))
{
continue;
}
// Call the event handler and add its output to the return value. The switch allows for execution up to 2x
// faster than using call_user_func_array
switch (count($args))
{
case 0:
$result = $observer->{$event}();
break;
case 1:
$result = $observer->{$event}($args[0]);
break;
case 2:
$result = $observer->{$event}($args[0], $args[1]);
break;
case 3:
$result = $observer->{$event}($args[0], $args[1], $args[2]);
break;
case 4:
$result = $observer->{$event}($args[0], $args[1], $args[2], $args[3]);
break;
case 5:
$result = $observer->{$event}($args[0], $args[1], $args[2], $args[3], $args[4]);
break;
default:
$result = call_user_func_array(array($observer, $event), $args);
break;
}
if (!is_null($result))
{
return $result;
}
}
// Return the observers' result in an array
return $result;
}
}