On Tue, Jan 15, 2013 at 8:38 AM, Gavin Staniforth <[email protected]> wrote:
> This was perhaps a type-o on my example. Having thought about it after a day
> of frustration I think I can grasp how I want it to work.
>
> Im wanting my Events to only trigger if the the Module route is matched, say
> my module has a base route of /private/foo I might want a different
> dispatch.error then /app/foo I was under the impression the first parameter
> on ->attach() on the SharedManager would do this as I pass the Module
> Namespace..
>
> Module1 -> Route = /module1/
> Module2 -> Route = /module2/
>
> If dispatch.error happens within the /module1/ then it should trigger the
> Module1 dispatch.error.
> If dispatch.error happens within the /module2/ then it should trigger the
> Module2 dispatch.error.
>
> Can this be achieved? Should I be adding logic into my closure to check if
> the Route is matched? I guess I could extend Exception to create a
> Module1Exception, Module2Exception then instanceof in the closure

What you'll do here is twofold:

* First, you'll register post-route events that inspect the route
match to see what module was matched
* Second, on matching the module from a route match, they will
register dispatch.error events.

So, let's consider the Module class from Module1:

    namespace Module1;

    class Module
    {
        public function onBootstrap($e)
        {
            $app = $e->getTarget();
            $app->getEventManager()->attach('route', array($this,
'onRoutePost'), -100);
        }

        public function onRoutePost($e)
        {
            $matches = $e->getRouteMatch();
            if (!$matches) {
                return;
            }
            $controller = $matches->getParam('controller', false);
            if (!$controller
                || (0 !== strpos($controller, __NAMESPACE__))
            ) {
                return;
            }

            $app = $e->getTarget();
            $app->getEventManager()->attach('dispatch.error',
array($this, 'onDispatchError'), 100);
        }

        public function onDispatchError($e)
        {
            // handle the error
        }
    }

Explanation:

* We create an onBootstrap() listener, which registers a listener on
"route", at low priority, so that it happens after we've routed.

* In the route listener, we check to see if the controller namespace
matches ours. If it does, we register a dispatch.error listener at
high priority (so it executes before the default listener).

You would do this for any module where you want to intercept dispatch.error.

> On Jan 15, 2013, at 02:19 PM, Matthew Weier O'Phinney <[email protected]>
> wrote:
>
> On Fri, Jan 11, 2013 at 5:45 PM, Gavin Staniforth <[email protected]> wrote:
>> Namespaced 'dispatch.error' Events.
>>
>> $sharedEvents =
>> $e->getApplication()->getEventManager()->getSharedManager();
>> $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
>> die('test');
>> }, 100);
>>
>> The above will trigger and only trigger in the module to which is under
>> __NAMESPACE__
>>
>> However .error doesnt seem to trigger at all? Is this expected? whats the
>> logic to having a error trigger for a certain module instead of having it
>> global across the application?
>>
>> $sharedEvents =
>> $e->getApplication()->getEventManager()->getSharedManager();
>> $sharedEvents->attach(__NAMESPACE__, 'dispatch.error', function($e) {
>> die('test');
>> }, 100);
>
> I don't understand what your question is...
>
> In the first case, you attached only to "dispatch", but your listener
> calls "die", which halts program execution; as such, there's no way
> that dispatch.error could be triggered, unless a listener at higher
> priority triggers it.
>
> You later ask "whats the logic to having a error trigger for a certain
> module instead of having it global across the application?" Well, as
> written, it will not trigger.
>
> dispatch.error is an Application event, not a controller event -- in
> other words, a controller will never trigger it, only the Application
> instance will. Furthermore, the Application class only publishes on
> the contexts of "Application", "Zend\Mvc\Application", and
> "Zend\Mvc\ApplicationInterface"; a namespace-specific listener will
> never trigger.
>
>
>
> --
> Matthew Weier O'Phinney
> Project Lead | [email protected]
> Zend Framework | http://framework.zend.com/
> PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
>
> --
> List: [email protected]
> Info: http://framework.zend.com/archives
> Unsubscribe: [email protected]
>
>



--
Matthew Weier O'Phinney
Project Lead            | [email protected]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to