Documentation is available at Controller.php
- <?php
- /**
- * MVCnPHP - Controller.class.php
- *
- * This source file is subject to version 2.02 of the PHP license,
- * that is bundled with this package in the file LICENSE, and is
- * available at through the world-wide-web at
- * http://www.php.net/license/2_02.txt.
- * If you did not receive a copy of the PHP license and are unable to
- * obtain it through the world-wide-web, please send a note to
- * license@php.net so we can mail you a copy immediately.
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @copyright Tony Bibbs 2003
- * @package net.geeklog.mvc
- * @version $Id: Controller.class.php,v 1.6 2004/03/23 02:39:20 tony Exp $
- *
- */
- /**
- * Constants used within this MVC framework
- */
- require_once 'Constants.php';
- /**
- * Loader factory will create a configuration loader.
- * Right now we only support PHP arrays and XML files
- */
- require_once 'LoaderFactory.php';
- /**
- * The controller part of the MVC
- *
- * This class controls processing for the program
- * indiscriminately. The focus is always on the underlying
- * views or commands. This class allows you to store application
- * messages such as status messages (e.g. something was saved
- * successfully) or error messages from failed validation or
- * security reasons. By default these are stored in the $_SESSION
- * superglobal in $_SESSION['MVC_MESSAGE'] and $_SESSION['MVC_ERRORS']
- * respectively (note: the later is an array). If your application is
- * not using PHP4 sessions you will want to follow the directions given
- * in the documentation for Controller::_clearMessages
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @package net.geeklog.mvc
- *
- */
- class MVCnPHP_Controller {
- /**
- * @access private
- * @var int
- */
- private $allowedFormMethod = null;
- /**
- * @access private
- * @var array
- */
- private $request = null;
- /**
- * @access private
- * @var string
- */
- private $object = null;
- /**
- * @access private
- * @var object
- */
- private $mapLoader = null;
- /**
- * @access private
- * @var object
- */
- private $mapping = null;
- /**
- * @access private
- * @var string
- */
- private $mvcBase = null;
- /**
- * @access private
- * @var string
- */
- private $viewDir = null;
- /**
- * @access private
- * @var string
- */
- private $commandDir = null;
- /**
- * @access private
- * @var string
- */
- private $baseURL = null;
- /**
- * @access private
- * @var int
- */
- private $configType = null;
- /**
- * @access private
- * @var array
- */
- private $configData = null;
- /**
- * Constructor
- *
- * Loads MVC configuration data and sets up data needed for
- * further processing
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access public
- * @param variant $configData Configuration data
- * @param string $configType Denotes type of config data structure, MVC_ARRAY or MVC_XML
- * @param string $allowedFormMethod Controller can accept MVC_GET, MVC_POST or MVC_BOTH
- *
- */
- public function __construct($configData, $configType = MVC_XML, $allowedFormMethod = MVC_BOTH)
- {
- $this->clearMessages();
- switch ($allowedFormMethod) {
- case MVC_GET:
- $this->request = &$_GET;
- break;
- case MVC_POST:
- $this->request = &$_POST;
- break;
- default:
- $this->request = &$_REQUEST;
- }
- if (!empty($this->request['cmd'])) {
- $this->object = $this->request['cmd'];
- } else {
- $this->object = 'default';
- }
- $this->configType = $configType;
- $this->configData = $configData;
- $this->allowedFormMethod = $allowedFormMethod;
- }
- /**
- * Sets location of the base MVC package so other MVC classes can be
- * loaded at a later time.
- *
- * @author Tony Bibbs
- * @access public
- * @param string $path Path to MVC base dir, must end with trailing slash
- * @return boolean True if view path is set, otherwise false
- *
- */
- public function setMVCBase($path)
- {
- if (!is_dir($path)) {
- throw new Exception('Bad path in Controller::setMVCBase');
- }
- $this->mvcBase = $path;
- return true;
- }
- /**
- * Sets location of views for this controller
- *
- * @author Tony Bibbs
- * @access public
- * @param string $path Path to view dir, must end with trailing slash
- * @return boolean True if view path is set, otherwise false
- *
- */
- public function setViewDir($path)
- {
- if (!is_dir($path)) {
- return false;
- }
- $this->viewDir = $path;
- return true;
- }
- /**
- * Sets location of commands for this controller
- *
- * @author Tony Bibbs
- * @access public
- * @param string $path Path to command dir, must end with trailing slash
- * @return boolean True if view path is set, otherwise false
- *
- */
- public function setCommandDir($path)
- {
- if (!is_dir($path)) {
- return false;
- }
- $this->commandDir = $path;
- return true;
- }
- /**
- * Sets base URL for this controller
- *
- * The base url allows all subsequent references to URL's to be relative
- * to the URL specified here
- *
- * @author Tony Bibbs
- * @access public
- * @param string $url Base URL
- *
- */
- public function setBaseURL($url)
- {
- $this->baseURL = $url;
- }
- /**
- * Process a request (or forward)
- *
- * NOTE this now supports the concept of a global forward for
- * descendants of the validator class. A global forward is a
- * slick way for validators to return control to the calling
- * command or view without having define a bunch of them in
- * the configuration file.
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access public
- * @return string Optional, may return HTML string of view
- *
- */
- public function processRequest()
- {
- // Ensure user isn't submitted data via an illegal method
- $this->checkMethod();
- if (empty($this->mapping)) {
- $this->loadMapping($this->configType, $this->configData);
- }
- // We should have latest here a valid mapping object
- if (is_null($this->mapping->getName())) {
- throw new Exception('Unexpected return from MVC command or MVC view. Got no valid mapping object');
- }
- if ($this->mapping->getType() == 'view') {
- require_once 'ViewFactory.php';
- try {
- $view = &MVCnPHP_ViewFactory::getView($this->mapping->getPath(), $this->mapping->getName());
- } catch (Exception $e) {
- $msg = $e->getMessage();
- // It is possible that the constructor for a view may want to do a forward
- if ($this->isForward($msg)) {
- $this->forwardControl($msg);
- unset($msg);
- }
- $tmpArray = explode(':', $msg);
- if ($tmpArray[0] == 'doForward') {
- $this->mapping = &$this->mapLoader->getMapping($tmpArray[1], $this->viewDir, $this->commandDir, $this->baseURL);
- $this->processRequest();
- exit;
- }
- throw $e;
- }
- // Got view object fine, now try rendering it
- $retval = $view->getView();
- // Check that we didn't receive any forwards at this point.
- if ($this->isForward($retval)) {
- $this->forwardControl($retval);
- unset($retval);
- }
- $tmpArray = explode(':', $retval);
- if ($tmpArray[0] == 'doForward') {
- $this->mapping = &$this->mapLoader->getMapping($tmpArray[1], $this->viewDir, $this->commandDir, $this->baseURL);
- $this->processRequest();
- exit;
- }
- if (!$view->printView()) {
- return $retval;
- }
- } else {
- require_once 'CommandFactory.php';
- try {
- $tmp = &MVCnPHP_CommandFactory::getCommand($this->mapping->getPath(), $this->mapping->getName());
- } catch (Exception $e) {
- $msg = $e->getMessage();
- if ($this->isForward($msg)) {
- $this->forwardControl($msg);
- unset($msg);
- }
- $tmpArray = explode(':', $msg);
- if ($tmpArray[0] == 'doForward') {
- $this->mapping = &$this->mapLoader->getMapping($tmpArray[1], $this->viewDir, $this->commandDir, $this->baseURL);
- $this->processRequest();
- exit;
- }
- throw $e;
- }
- // Must've got an actual command object, try running it
- $strForward = $tmp->execute();
- // Now check for a forward.
- if ($this->isForward($strForward)) {
- $this->forwardControl($strForward);
- unset($retval);
- }
- $tmpArray = explode(':', $strForward);
- if ($tmpArray[0] == 'doForward') {
- $this->mapping = &$this->mapLoader->getMapping($tmpArray[1], $this->viewDir, $this->commandDir, $this->baseURL);
- $this->processRequest();
- exit;
- }
- // OK if we got here then we got an undefined forward of some kind
- //if (!empty($strForward)) {
- //print_r($tmp); print_r($_SESSION['forwards']); $_SESSION['forwards'] = ''; exit;
- //throw new Exception('Unexpected return from MVC command or MVC view. Got ' . $strForward);
- //}
- }
- }
- /**
- * Clears any existing errors or messages
- *
- * This method assumes the application is using PHP4 sessions
- * to store MVC errors and messages. If that is not the case,
- * the application should create a new class that extends this
- * controller and override this function to clear the messages
- * in the way desired.
- *
- * @author Tony Bibs <tony@geeklog.net>
- * @access private
- *
- */
- public function clearMessages()
- {
- unset($_REQUEST[MVC_MESSAGES]);
- unset($_REQUEST[MVC_ERRORS]);
- }
- /**
- * Ensures only supported methods are used
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access private
- *
- */
- public function checkMethod()
- {
- switch ($this->allowedFormMethod) {
- case MVC_GET:
- if (count($_POST) > 0) {
- throw new Exception('POST method is not supported by this controller');
- }
- case MVC_POST:
- if (count($_GET) > 0) {
- throw new Exception('GET method is not supported by this controller');
- }
- }
- }
- /**
- * Loads the mapping data for a model or view
- *
- * This will load only the mapping data needed by the
- * current object.
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access private
- * @param string $configType Dontes the type of config data structure
- * @param variant $configData this is the data or file pointer for config data
- *
- */
- public function loadMapping($configType, $configData)
- {
- if (empty($this->mapLoader)) {
- $this->mapLoader = &MVCnPHP_LoaderFactory::getLoader($configType);
- }
- $this->mapping = $this->mapLoader->getMapping($this->object, $this->viewDir, $this->commandDir, $this->baseURL, $configData);
- // Maybe, we've gotten an invalid mapping object, so we try to
- // load the default view
- if (is_null($this->mapping->getName())) {
- $this->object = 'default';
- // We can also call $this->loadMapping after we've set the object to 'default'
- // but this may end in an endless loop, so we try to load the default view only once
- $this->mapping = &$this->mapLoader->getMapping($this->object, $this->viewDir, $this->commandDir, $this->baseURL, $configData);
- }
- if (is_null($this->mapping)) {
- throw new Exception('Unabled to load a mapping in MVCnPHP_Controller::loadMapping');
- }
- }
- /**
- * Determines if string is a name of an existing forward.
- *
- * Because views can return the HTML string it won't be obvious
- * if the call to the view's getView() is a forward or actually
- * HTML. This function makes that determintation.
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access private
- * @param string $name Name of forward to look for
- * @return boolean True if forward is found, otherwise false
- *
- */
- public function isForward($name)
- {
- if ($this->mapping->getForward($name)) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * Forwards control onto another command or view
- *
- * A model will typically try to forward control to a view
- * (less likely, but possible, another command) after processing.
- * This object handles that forwarding and can even handle forwarding
- * to a different URL if configured to do so.
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access private
- * @param string $name Name of next model or view to go to
- *
- */
- public function forwardControl($name)
- {
- $forward = $this->mapping->getForward($name);
- if ($forward[MVC_TYPE] == 'redirect') {
- $url = $this->mapping->getBaseURL() . $forward[MVC_TARGET];
- header('Location: ' . $url);
- } else {
- $this->object = $forward[MVC_TARGET];
- $this->mapping = &$this->mapLoader->getMapping($this->object, $this->viewDir, $this->commandDir, $this->baseURL);
- $this->processRequest();
- }
- }
- }
- ?>
Documentation generated on Mon, 7 Mar 2005 22:36:21 -0600 by phpDocumentor 1.3.0RC3