pre move to sub-modules, probably broken
[simplicity.git] / source / lib / simplicity / modules / mvc / mvc.php
blobbda816a5d5ca91f72af1f6813676005b41f8fabb
1 <?php
2 /**
3 * Mvc Module
4 *
5 * The Simplicity Mvc module provides a lightweight and flexible MVC stack.
6 *
7 * @author John Le Drew <jp@antz29.com>
8 * @copyright Copyright (c) 2009, John Le Drew
9 * @license http://www.opensource.org/licenses/mit-license.php MIT License
10 * @link http://www.simplicityphp.com/modules/mvc Mvc Module
11 * @version 2.0.0-alpha
13 * @smp_depends Template >= 2.0.0-alpha
14 * @smp_depends Router >= 2.0.0-alpha
16 class smp_MvcModule extends smp_Module
18 private $_view;
20 protected function init()
22 $this->setDefault('base_controller','smp_MvcController');
23 $this->setDefault('base_controller_prefix','smp_');
24 $this->setDefault('view_backend','Phtml');
25 $this->setDefault('view_backend_options',array(
26 'view_path' => APP.'mvc'.DS.'views'.DS,
27 'layout_path' => APP.'mvc'.DS.'layouts'.DS
28 ));
29 $this->setDefault('modules',false);
32 protected function postInit()
34 $base = $this->getConfig('base_controller');
35 $prefix = $this->getConfig('base_controller_prefix');
37 $this->createExtensionPoint($base);
38 $this->registerPluginPrefix($prefix);
41 public function exec($args=array())
43 $segments = array(
44 'module' =>'default',
45 'controller' =>'index',
46 'action' =>'index'
49 if (!$this->getConfig('modules')) array_shift($segments);
51 $route = smp_Request::getInstance()->parse($segments);
53 $route['view'] = $route['controller'].DS.$route['action'];
54 $route['controller'] = smp_String::camelize($route['controller'],true);
55 $route['action'] = smp_String::camelize($route['action'],true);
57 $this->_view = $this->initView();
58 $this->initLayout($this->_view);
60 return $this->despatch($route);
63 /**
65 * @return smp_Router
67 public function getRouter()
69 return $this->_router;
72 protected function initView()
74 $backend = $this->getConfig("view_backend");
75 $options = $this->getConfig("view_backend_options");
77 return new smp_MvcView($backend,$options);
80 protected function initLayout($view)
82 $path = $view->getLayoutPath();
83 $view->setLayout('default');
86 protected function isStatic($controller)
88 return !class_exists($controller);
91 protected function getController($controller)
93 if (!$class = $this->getModuleClass($controller))
95 return false;
98 $c = new $class($this);
100 if (!($c instanceof smp_MvcController))
102 throw new Exception('Controller '.$controller.' is not an instance of smp_MvcController.');
105 return $c;
108 public function despatch($route)
110 if (!is_array($route))
112 $route = $this->_router->parse($route)->getParsedSegments();
115 $this->_view->setView($route['view']);
117 $output = null;
119 if ($c = $this->getController($route['controller']))
122 $c->setView($this->_view);
123 $action = $route['method'].$route['action'].'Action';
125 $c->preAction();
126 $c->setUri($route['uri']);
128 $output = $c->$action();
130 if (is_array($output) && isset($output['_despatch']))
132 return $output['_despatch'];
136 if ($output === null)
138 $output = $this->_view->render();
141 if ($c)
143 $c->postAction();
146 smp_Response::getInstance()->setContent($output);