1: <?php
2:
3: /**
4: * Orchestra: A minimalist object-oriented superset for WordPress using Phalcon.
5: *
6: * This source file is subject to the MIT license that is bundled
7: * with this package in the file LICENSE and is available through
8: * the world-wide-web at the following URI:
9: * http://opensource.org/licenses/MIT
10: *
11: * @copyright Akufen Atelier Creatif
12: * @author Nicholas Charbonneau <nicholas@akufen.ca>
13: * @license http://opensource.org/licenses/MIT
14: * @version 0.1.3
15: * @link https://github.com/akufenstudio/orchestra
16: */
17:
18: namespace Akufen\Orchestra\Services;
19:
20: /**
21: * Akufen\Orchestra\Dispatcher
22: *
23: * A custom dispatcher for WordPress architecture
24: *
25: * @package Services
26: */
27: class Dispatcher extends \Phalcon\Mvc\User\Plugin
28: {
29: /**
30: * Attempts to find a controller & action that matches the request.
31: *
32: * @param \Phalcon\Events\Event $event Contextual information of the event produced.
33: * @param \Phalcon\Mvc\Dispatcher $dispatcher The dispatcher service.
34: * @return void
35: */
36: public function beforeDispatchLoop(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher)
37: {
38: global $post;
39: $router = $this->getDI()->getRouter();
40:
41: // Send to a 404 by default
42: $dispatcher->setControllerName('error');
43: $dispatcher->setActionName('show404');
44:
45: // Get paths if router matched
46: if ($router->wasMatched()) {
47: $paths = $router->getMatchedRoute()->getPaths();
48: }
49:
50: // Attempt to redirect to a controller & action
51: if (isset($paths) && is_string($paths['controller'])) {
52: $dispatcher->setModuleName($paths['module']);
53: $dispatcher->setNamespaceName($paths['namespace']);
54: $dispatcher->setControllerName($paths['controller']);
55: $dispatcher->setActionName($paths['action']);
56: } else if ($post) {
57: // Set the correct controller & action
58: $dispatcher->setControllerName($post->post_type);
59:
60: // Retrieve page slug, taxonomy or single action
61: if (!empty($template = get_page_template_slug($post->ID))) {
62: $dispatcher->setActionName(
63: str_replace('.php', '', get_page_template_slug($post->ID))
64: );
65: } else {
66: $dispatcher->setActionName(
67: is_post_type_archive($post->post_type)? 'index' : 'single'
68: );
69: }
70: }
71: }
72: }
73: