/** * Set parameters * * Overwrites parameters * * @param array|ArrayAccess|Traversable|object $params * @return ModulesEvent */ public function setParams($params) { if ($params instanceof Traversable) { $params = iterator_to_array($params); } else { if (is_object($params) && !$params instanceof ArrayAccess) { $params = (array) $params; } } return parent::setParams($params); }
/** * Create Annotation * * @param array $annotationData * @return false|\stdClass */ public function createAnnotation(array $annotationData) { $event = new Event(); $event->setName(self::EVENT_CREATE_ANNOTATION); $event->setTarget($this); $event->setParams(array('class' => $annotationData[0], 'content' => $annotationData[1], 'raw' => $annotationData[2])); $results = $this->getEventManager()->trigger($event, function ($r) { return is_object($r); }); $annotation = $results->last(); return is_object($annotation) ? $annotation : false; }
/** * Configure an element from annotations * * @param AnnotationCollection $annotations * @param PropertyReflection $reflection * @param ArrayObject $formSpec * @param ArrayObject $filterSpec * @return void * * @triggers checkForExclude * @triggers discoverName * @triggers configureElement */ protected function configureElement($annotations, $reflection, $formSpec, $filterSpec) { // If the element is marked as exclude, return early if ($this->checkForExclude($annotations)) { return; } $events = $this->getEventManager(); $name = $this->discoverName($annotations, $reflection); $elementSpec = new ArrayObject(['flags' => [], 'spec' => ['name' => $name]]); $inputSpec = new ArrayObject(['name' => $name]); $event = new Event(); $event->setParams(['name' => $name, 'elementSpec' => $elementSpec, 'inputSpec' => $inputSpec, 'formSpec' => $formSpec, 'filterSpec' => $filterSpec]); foreach ($annotations as $annotation) { $event->setParam('annotation', $annotation); $events->trigger(__FUNCTION__, $this, $event); } // Since "filters", "type", "validators" is a reserved names in the filter specification, // we need to add the specification without the name as the key. // In all other cases, though, the name is fine. if ($event->getParam('inputSpec')->count() > 1 || $annotations->hasAnnotation(Input::class)) { if ($name === 'type' || $name === 'filters' || $name === 'validators') { $filterSpec[] = $event->getParam('inputSpec'); } else { $filterSpec[$name] = $event->getParam('inputSpec'); } } $elementSpec = $event->getParam('elementSpec'); $type = isset($elementSpec['spec']['type']) ? $elementSpec['spec']['type'] : Element::class; // Compose as a fieldset or an element, based on specification type. // If preserve defined order is true, all elements are composed as elements to keep their ordering if (!$this->preserveDefinedOrder() && is_subclass_of($type, FieldsetInterface::class)) { if (!isset($formSpec['fieldsets'])) { $formSpec['fieldsets'] = []; } if (isset($formSpec['fieldsets'][$name])) { $formSpec['fieldsets'][] = $elementSpec; } else { $formSpec['fieldsets'][$name] = $elementSpec; } } else { if (!isset($formSpec['elements'])) { $formSpec['elements'] = []; } if (isset($formSpec['elements'][$name])) { $formSpec['elements'][] = $elementSpec; } else { $formSpec['elements'][$name] = $elementSpec; } } }
/** * Overload setParams to inject request object, if passed via params * * @param array|ArrayAccess|object $params * @return self */ public function setParams($params) { if (!is_array($params) && !is_object($params)) { throw new InvalidArgumentException(sprintf('Event parameters must be an array or object; received "%s"', gettype($params))); } if (is_array($params) || $params instanceof ArrayAccess) { if (isset($params['request'])) { $this->setRequest($params['request']); unset($params['request']); } } parent::setParams($params); return $this; }
public function template($template) { $controller = get_class($this->controller); $services = $this->getController()->getServiceLocator(); $event = new Event(); $eventManager = $services->get('EventManager'); $eventManager->setIdentifiers('Mail'); $p = new Parameters(array('mail' => $this, 'template' => $template)); $event->setParams($p); $eventManager->trigger('template.pre', $event); // get all loaded modules $moduleManager = $services->get('ModuleManager'); $loadedModules = $moduleManager->getModules(); //get_called_class $controllerIdentifier = strtolower(substr($controller, 0, strpos($controller, '\\'))); $viewResolver = $this->getController()->getServiceLocator()->get('ViewResolver'); $templateHalf = 'mail/' . $template; $resource = $viewResolver->resolve($templateHalf); if (empty($resource)) { $templateFull = $controllerIdentifier . '/mail/' . $template; $resource = $viewResolver->resolve($templateFull); } $__vars = $this->param; if (array_key_exists('this', $__vars)) { unset($__vars['this']); } extract($__vars); unset($__vars); // remove $__vars from local scope if ($resource) { try { ob_start(); include $resource; $content = ob_get_clean(); $this->setBody($content); } catch (\Exception $ex) { ob_end_clean(); throw $ex; } $__vars = get_defined_vars(); foreach ($this->param as $key => $value) { if (isset($__vars[$key])) { unset($__vars[$key]); } } unset($__vars['content'], $__vars['controllerIdentifier'], $__vars['controller'], $__vars['resource'], $__vars['template'], $__vars['viewResolver']); $this->config = $__vars; } }
/** * Triggers the specified event on the defined context and return a concateneted string with the results * @param string $eventName * @param mixed $target * @param array $argv * @return string */ public function triggerEvent($eventName, $target, $argv) { //init the event with the target, params and name $event = new Event(); $event->setTarget($target); $event->setParams($argv); $event->setName($eventName); $content = ""; //trigger the event listeners $responses = $this->events()->trigger($eventName, $event); //merge all results and return the response foreach ($responses as $response) { $content .= $response; } return $content; }
/** * Magic methods for install, uninstall, update, etc. * * @param string $method * @param array $args * @return bool * @throws \InvalidArgumentException */ public function __call($method, $args) { if (!in_array($method, array('install', 'uninstall', 'update'))) { throw new \InvalidArgumentException(sprintf('Invalid action "%s".', $method)); } $name = array_shift($args); $options = empty($args) ? array() : array_shift($args); $version = isset($options['version']) ? $options['version'] : null; $event = new Event(); $event->setParams(array('name' => $name, 'version' => $version, 'action' => $method, 'config' => array())); $this->event = $event; $this->attachDefaultListeners(); $this->getEventManager()->trigger('start', null, $event); // Define callback used to determine whether or not to short-circuit $shortCircuit = function ($r) { if (false === $r) { return true; } return false; }; $result = $this->getEventManager()->trigger(sprintf('%s.pre', $method), null, $event, $shortCircuit); if ($result->stopped()) { return false; } $actionMethod = $method . 'Action'; $result = $this->{$actionMethod}(); if (!$result['status']) { $ret = array($method => $result); $this->event->setParam('result', $ret); return false; } $result = $this->getEventManager()->trigger('process', null, $event, $shortCircuit); if ($result->stopped()) { return false; } $this->getEventManager()->trigger(sprintf('%s.post', $method), null, $event); $this->getEventManager()->trigger('finish', null, $event); $status = true; $result = $event->getParam('result'); //foreach ($result as $action => $state) { if ($result['status'] === false) { $status = false; //break; } //} return $status; }
/** * Triggers the specified event on the defined context and return a concateneted string with the results * * @param string $eventName * @param mixed $target * @param array $argv * @return string */ public function __invoke($eventName, $target, $argv) { $alias = 'zfc-twig'; if (strpos($eventName, ':') !== false) { $aux = explode(':', $eventName); $alias = $aux[0]; $eventName = $aux[1]; } //init the event with the target, params and name $event = new Event(); $event->setTarget($target); $event->setParams($argv); $event->setName($eventName); $content = ""; //trigger the event listeners $responses = $this->events($alias)->trigger($eventName, $event); //merge all results and return the response foreach ($responses as $response) { $content .= $response; } return $content; }
public function setParams($params) { if (is_array($params)) { if (isset($params['paginatorParams'])) { $this->setPaginatorParams($params['paginatorParams']); unset($params['paginatorParams']); } if (isset($params['paginators'])) { $this->setPaginators($params['paginators']); unset($params['paginators']); } if (isset($params['paginatorName'])) { $this->setPaginatorName($params['paginatorName']); unset($params['paginatorName']); } } return parent::setParams($params); }
/** * Configure an element from annotations * * @param AnnotationCollection $annotations * @param \Zend\Code\Reflection\PropertyReflection $reflection * @param ArrayObject $formSpec * @param ArrayObject $filterSpec * @return void * @triggers checkForExclude * @triggers discoverName * @triggers configureElement */ protected function configureElement($annotations, $reflection, $formSpec, $filterSpec) { // If the element is marked as exclude, return early if ($this->checkForExclude($annotations)) { return; } $events = $this->getEventManager(); $name = $this->discoverName($annotations, $reflection); $elementSpec = new ArrayObject(array('flags' => array(), 'spec' => array('name' => $name))); $inputSpec = new ArrayObject(array('name' => $name)); $event = new Event(); $event->setParams(array('name' => $name, 'elementSpec' => $elementSpec, 'inputSpec' => $inputSpec, 'formSpec' => $formSpec, 'filterSpec' => $filterSpec)); foreach ($annotations as $annotation) { $event->setParam('annotation', $annotation); $events->trigger(__FUNCTION__, $this, $event); } // Since "type" is a reserved name in the filter specification, // we need to add the specification without the name as the key. // In all other cases, though, the name is fine. if ($event->getParam('inputSpec')->count() > 1) { if ($name === 'type') { $filterSpec[] = $event->getParam('inputSpec'); } else { $filterSpec[$name] = $event->getParam('inputSpec'); } } $elementSpec = $event->getParam('elementSpec'); $type = isset($elementSpec['spec']['type']) ? $elementSpec['spec']['type'] : 'Zend\\Form\\Element'; // Compose as a fieldset or an element, based on specification type if (static::isSubclassOf($type, 'Zend\\Form\\FieldsetInterface')) { if (!isset($formSpec['fieldsets'])) { $formSpec['fieldsets'] = array(); } $formSpec['fieldsets'][] = $elementSpec; } else { if (!isset($formSpec['elements'])) { $formSpec['elements'] = array(); } $formSpec['elements'][] = $elementSpec; } }
/** * Set event parameters * * @param array|object|ArrayAccess $params * @return ViewEvent */ public function setParams($params) { parent::setParams($params); if (!is_array($params) && !$params instanceof ArrayAccess) { return $this; } foreach (['model', 'renderer', 'request', 'response', 'result'] as $param) { if (isset($params[$param])) { $method = 'set' . $param; $this->{$method}($params[$param]); } } return $this; }
public function testUpdateSubscriptionsReturnsAffectedRowWhenSubscribing() { $event = new Event(); $subscriberModel = new SubscriberModel(); $subscriberModel->setSubscriberId(1)->setEmail('*****@*****.**')->setName('Joe Bloggs')->setSubscribe([1]); $form = $this->getMock('UthandoNewsletter\\Form\\Subscriber'); $form->expects($this->once())->method('getData')->willReturn($subscriberModel); $event->setParams(['form' => $form, 'saved' => 1]); $subscriptionServiceMock = $this->getMock('UthandoNewsletter\\Service\\Subscription'); $subscriptionServiceMock->expects($this->any())->method('getSubscriptionsBySubscriberId')->willReturn([]); $subscriptionServiceMock->expects($this->any())->method('save')->willReturn(1); $this->serviceManager->get('UthandoServiceManager')->setService('UthandoNewsletterSubscription', $subscriptionServiceMock); $service = $this->getService(); $service->updateSubscriptions($event); $this->assertEquals(1, $event->getParam('result')); }
/** * Sets parameters. * * @internal Sets the job entity when passed in $params under the key/property named "job". * This is because in the JobEventManager, the default event class is set to this, * * @since 0.19 */ public function setParams($params) { if (is_array($params) && isset($params['job'])) { $this->setJobEntity($params['job']); unset($params['job']); } elseif (is_object($params) && isset($params->job)) { $this->setJobEntity($params->job); } return parent::setParams($params); }
/** * Sets parameters. * * @internal Sets the application entity when passed in $params under the key/property named "application". * This is because in the ApplicationEventManager, the default event class is set to this, * * @since 0.25 */ public function setParams($params) { if (is_array($params) && isset($params['application'])) { $this->setApplicationEntity($params['application']); unset($params['application']); } elseif (is_object($params) && isset($params->application)) { $this->setApplicationEntity($params->application); } if (is_array($params) && isset($params['user'])) { $this->setUser($params['user']); unset($params['user']); } if (is_array($params) && isset($params['status'])) { $this->setStatus($params['status']); unset($params['status']); } return parent::setParams($params); }
/** * Parse a single page into a navigation configuration * * @param Page $page * @return array * @throws Exception\RuntimeException */ public function parsePage(PageInterface $page) { $meta = $page->getMetaData(); $navPage = Page::factory(array('type' => 'mvc', 'route' => (string) $page->getId(), 'label' => $meta->getNavigationTitle(), 'visible' => $page->isVisible())); $event = new Event(); $event->setName(__FUNCTION__ . '.' . $page->getModule()); $event->setTarget($this); $event->setParams(array('page' => $page, 'navigation' => $navPage)); $this->events->trigger($event); return $navPage; }
/** * Determine if an element is marked to exclude from the definitions * * @param AnnotationCollection $annotations * @return true|false */ protected function checkForExclude($annotations) { $event = new Event(); $event->setName(__FUNCTION__); $event->setTarget($this); $event->setParams(['annotations' => $annotations]); $results = $this->getEventManager()->triggerEventUntil(function ($r) { return true === $r; }, $event); return (bool) $results->last(); }
/** * Is this session valid? * * Notifies the Validator Chain until either all validators have returned * true or one has failed. * * @return bool */ public function isValid() { $validator = $this->getValidatorChain(); $event = new Event(); $event->setName('session.validate'); $event->setTarget($this); $event->setParams($this); $falseResult = function ($test) { return false === $test; }; $responses = $validator->triggerEventUntil($falseResult, $event); if ($responses->stopped()) { // If execution was halted, validation failed return false; } // Otherwise, we're good to go return true; }
/** * Magic method for install, uninstall, update, activate, deactivate, etc. * * @param string $method * @param array $args * @return bool * @throws \InvalidArgumentException */ public function __call($method, $args) { if (!in_array($method, array('install', 'uninstall', 'update', 'activate', 'deactivate'))) { throw new \InvalidArgumentException(sprintf('Invalid action "%s".', $method)); } $model = null; $module = array_shift($args); $options = empty($args) ? array() : array_shift($args); $moduleVersion = isset($options['version']) ? $options['version'] : null; $moduleTitle = isset($options['title']) ? $options['title'] : ''; if ($module instanceof ModuleRow) { $model = $module; $moduleName = $model->name; $moduleDirectory = $model->directory; $moduleTitle = $moduleTitle ?: $model->title; $moduleVersion = $moduleVersion ?: $model->version; } else { $moduleName = $module; $moduleDirectory = isset($options['directory']) ? $options['directory'] : $module; } $event = new Event(); $event->setParams(array('model' => $model, 'module' => $moduleName, 'directory' => $moduleDirectory, 'title' => $moduleTitle, 'version' => $moduleVersion, 'action' => $method, 'config' => array(), 'result' => array())); $this->event = $event; $this->attachDefaultListeners(); $this->getEventManager()->trigger('start', null, $event); $actionClass = sprintf('Module\\%s\\Installer\\Action\\%s', ucfirst($moduleDirectory), ucfirst($method)); if (!class_exists($actionClass)) { $actionClass = sprintf('%s\\Action\\%s', __NAMESPACE__, ucfirst($method)); } $action = new $actionClass($event); $action->setEvents($this->getEventManager()); // Define callback used to determine whether or not to short-circuit $shortCircuit = function ($r) { if (false === $r) { return true; } return false; }; $result = $this->getEventManager()->trigger(sprintf('%s.pre', $method), null, $event, $shortCircuit); if ($result->stopped()) { return false; } $status = $action->process(); if (!$status) { return false; } //$resourceHandler = new Resource($event); //$resourceHandler->attach($this->getEventManager()); $this->attachResource(); $result = $this->getEventManager()->trigger('process', null, $event, $shortCircuit); if ($result->stopped()) { $action->rollback(); return false; } $this->getEventManager()->trigger(sprintf('%s.post', $method), null, $event); $this->getEventManager()->trigger('finish', null, $event); $status = true; $result = $event->getParam('result'); foreach ($result as $action => $state) { if ($state['status'] === false) { $status = false; break; } } return $status; }
public function testCanPassEventObjectAsArgvToTriggerUntil() { $event = new Event(); $event->setParams(array('foo' => 'bar')); $this->events->attach(__FUNCTION__, function ($e) { return $e; }); $responses = $this->events->triggerUntil(__FUNCTION__, $this, $event, function ($r) { return $r instanceof EventInterface; }); $this->assertTrue($responses->stopped()); $this->assertSame($event, $responses->last()); $this->assertEquals(__FUNCTION__, $event->getName()); $this->assertSame($this, $event->getTarget()); }
public function setParams($params) { parent::setParams($params); $form = $this->getParam('form'); if ($form instanceof FormInterface || $form instanceof Container) { $this->setForm($form); } return $this; }
/** * {@inheritdoc} */ public function setParams($params) { parent::setParams($params); $isObject = is_object($this->params); foreach ($this->specializedParams as $param) { if ($isObject) { if (isset($params->{$param})) { $method = 'set' . $param; $this->{$method}($params->{$param}); } unset($this->params->{$param}); } else { if (isset($params[$param])) { $method = 'set' . $param; $this->{$method}($params[$param]); } unset($this->params[$param]); } } return $this; }
public function getEvent() { $event = new Event(); $event->setParams(array('class' => __NAMESPACE__ . '\\TestAsset\\DoctrineAnnotation', 'content' => '(foo="bar")', 'raw' => '@' . __NAMESPACE__ . '\\TestAsset\\DoctrineAnnotation(foo="bar")')); return $event; }
public function getFooEvent() { $event = new Event(); $event->setParams(array('class' => __NAMESPACE__ . '\\TestAsset\\Foo', 'content' => '(test content)', 'raw' => '@' . __NAMESPACE__ . '\\TestAsset\\Foo(test content)')); return $event; }
/** * Configure an element from annotations * * @param AnnotationCollection $annotations * @param \Zend\Code\Reflection\PropertyReflection $reflection * @param ArrayObject $formSpec * @param ArrayObject $filterSpec * @return void * @triggers checkForExclude * @triggers discoverName * @triggers configureElement */ protected function configureElement($annotations, $reflection, $formSpec, $filterSpec) { // If the element is marked as exclude, return early if ($this->checkForExclude($annotations)) { return; } $events = $this->getEventManager(); $name = $this->discoverName($annotations, $reflection); $elementSpec = new ArrayObject(array('flags' => array(), 'spec' => array('name' => $name))); $inputSpec = new ArrayObject(array('name' => $name)); $event = new Event(); $event->setParams(array('name' => $name, 'elementSpec' => $elementSpec, 'inputSpec' => $inputSpec, 'formSpec' => $formSpec, 'filterSpec' => $filterSpec)); foreach ($annotations as $annotation) { $event->setParam('annotation', $annotation); $events->trigger(__FUNCTION__, $this, $event); } $filterSpec[$name] = $event->getParam('inputSpec'); $elementSpec = $event->getParam('elementSpec'); $type = isset($elementSpec['spec']['type']) ? $elementSpec['spec']['type'] : 'Zend\\Form\\Element'; // Compose as a fieldset or an element, based on specification type if (self::isSubclassOf($type, 'Zend\\Form\\FieldsetInterface')) { if (!isset($formSpec['fieldsets'])) { $formSpec['fieldsets'] = array(); } $formSpec['fieldsets'][] = $elementSpec; } else { if (!isset($formSpec['elements'])) { $formSpec['elements'] = array(); } $formSpec['elements'][] = $elementSpec; } }