PageRenderTime 41ms CodeModel.GetById 30ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/controller/component.php

https://github.com/samanz/cakecart
PHP | 232 lines | 131 code | 9 blank | 92 comment | 34 complexity | 0fca0f770be23a1e77a0420ace97c3b3 MD5 | raw file
  1<?php
  2/* SVN FILE: $Id: component.php 7296 2008-06-27 09:09:03Z gwoo $ */
  3/**
  4 *
  5 * PHP versions 4 and 5
  6 *
  7 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
  8 * Copyright 2005-2008, Cake Software Foundation, Inc.
  9 *								1785 E. Sahara Avenue, Suite 490-204
 10 *								Las Vegas, Nevada 89104
 11 *
 12 * Licensed under The MIT License
 13 * Redistributions of files must retain the above copyright notice.
 14 *
 15 * @filesource
 16 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 17 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 18 * @package			cake
 19 * @subpackage		cake.cake.libs.controller
 20 * @since			CakePHP(tm) v TBD
 21 * @version			$Revision: 7296 $
 22 * @modifiedby		$LastChangedBy: gwoo $
 23 * @lastmodified	$Date: 2008-06-27 02:09:03 -0700 (Fri, 27 Jun 2008) $
 24 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 25 */
 26/**
 27 * Handler for Controller::$components
 28 *
 29 * @package		cake
 30 * @subpackage	cake.cake.libs.controller
 31 */
 32class Component extends Object {
 33/**
 34 * Some vars from controller (plugin, name, base)
 35 *
 36 * @var object
 37 * @access private
 38 */
 39	var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
 40/**
 41 * All loaded components
 42 *
 43 * @var object
 44 * @access private
 45 */
 46	var $__loaded = array();
 47/**
 48 * Settings for loaded components.
 49 *
 50 * @var array
 51 * @access private
 52 **/
 53	var $__settings = array();
 54/**
 55 * Used to initialize the components for current controller
 56 *
 57 * @param object $controller Controller with components to load
 58 * @access public
 59 */
 60	function init(&$controller) {
 61		if ($controller->components !== false && is_array($controller->components)) {
 62			$this->__controllerVars = array(
 63				'plugin' => $controller->plugin, 'name' => $controller->name, 'base' => $controller->base
 64			);
 65
 66			if (!in_array('Session', $controller->components)) {
 67				array_unshift($controller->components, 'Session');
 68			}
 69			$this->_loadComponents($controller);
 70		}
 71	}
 72/**
 73 * Called before the Controller::beforeFilter()
 74 *
 75 * @param object $controller Controller with components to initialize
 76 * @access public
 77 */
 78	function initialize(&$controller) {
 79		foreach (array_keys($this->__loaded) as $name) {
 80			$component =& $this->__loaded[$name];
 81			if (method_exists($component,'initialize') && $component->enabled === true) {
 82				$settings = array();
 83				if (isset($this->__settings[$name])) {
 84					$settings = $this->__settings[$name];
 85				}
 86				$component->initialize($controller, $settings);
 87			}
 88		}
 89	}
 90/**
 91 * Called after the Controller::beforeFilter() and before the controller action
 92 *
 93 * @param object $controller Controller with components to startup
 94 * @access public
 95 */
 96	function startup(&$controller) {
 97		foreach (array_keys($this->__loaded) as $name) {
 98			$component =& $this->__loaded[$name];
 99			if (method_exists($component,'startup') && $component->enabled === true) {
100				$component->startup($controller);
101			}
102		}
103	}
104/**
105 * Called after the Controller::beforeRender(), after the view class is loaded, and before the Controller::render()
106 *
107 * @param object $controller Controller with components to beforeRender
108 * @access public
109 */
110	function beforeRender(&$controller) {
111		foreach (array_keys($this->__loaded) as $name) {
112			$component =& $this->__loaded[$name];
113			if (method_exists($component,'beforeRender') && $component->enabled === true) {
114				$component->beforeRender($controller);
115			}
116		}
117	}
118/**
119 * Called before Controller::redirect();
120 *
121 * @param object $controller Controller with components to beforeRedirect
122 * @access public
123 */
124	function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
125		$response = array();
126		foreach (array_keys($this->__loaded) as $name) {
127			$component =& $this->__loaded[$name];
128			if (method_exists($component,'beforeRedirect') && $component->enabled === true) {
129				$resp = $component->beforeRedirect($controller, $url, $status, $exit);
130				if ($resp === false) {
131					return false;
132				}
133				$response[] = $resp;
134			}
135		}
136		return $response;
137	}
138/**
139 * Called after Controller::render() and before the output is printed to the browser
140 *
141 * @param object $controller Controller with components to shutdown
142 * @access public
143 */
144	function shutdown(&$controller) {
145		foreach (array_keys($this->__loaded) as $name) {
146			$component =& $this->__loaded[$name];
147			if (method_exists($component,'shutdown') && $component->enabled === true) {
148				$component->shutdown($controller);
149			}
150		}
151	}
152/**
153 * Load components used by this component.
154 *
155 * @param object $object Object with a Components array
156 * @param object $parent the parent of the current object
157 * @return void
158 * @access protected
159 */
160	function _loadComponents(&$object, $parent = null) {
161		$components = $object->components;
162		$base = $this->__controllerVars['base'];
163
164		if (is_array($object->components)) {
165			$normal = Set::normalize($object->components);
166			foreach ($normal as $component => $config) {
167				$parts = preg_split('/\/|\./', $component);
168
169				if (count($parts) === 1) {
170					$plugin = $this->__controllerVars['plugin'] . '.';
171				} else {
172					$plugin = Inflector::underscore($parts['0']) . '.';
173					$component = array_pop($parts);
174				}
175				$componentCn = $component . 'Component';
176
177				if (!class_exists($componentCn)) {
178					if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
179						if (!App::import('Component', $component)) {
180							$this->cakeError('missingComponentFile', array(array(
181								'className' => $this->__controllerVars['name'],
182								'component' => $component,
183								'file' => Inflector::underscore($component) . '.php',
184								'base' => $base,
185								'code' => 500
186							)));
187							return false;
188						}
189					}
190
191					if (!class_exists($componentCn)) {
192						$this->cakeError('missingComponentClass', array(array(
193							'className' => $this->__controllerVars['name'],
194							'component' => $component,
195							'file' => Inflector::underscore($component) . '.php',
196							'base' => $base,
197							'code' => 500
198						)));
199						return false;
200					}
201				}
202
203				if (isset($this->__loaded[$component])) {
204					$object->{$component} =& $this->__loaded[$component];
205
206					if (!empty($config) && isset($this->__settings[$component])) {
207						$this->__settings[$component] = array_merge($this->__settings[$component], $config);
208					} elseif (!empty($config)) {
209						$this->__settings[$component] = $config;
210					}
211				} else {
212					if ($componentCn == 'SessionComponent') {
213						$object->{$component} =& new $componentCn($base);
214					} else {
215						$object->{$component} =& new $componentCn();
216					}
217					$object->{$component}->enabled = true;
218					$this->__loaded[$component] =& $object->{$component};
219					if (!empty($config)) {
220						$this->__settings[$component] = $config;
221					}
222				}
223
224				if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
225					$this->_loadComponents($object->{$component}, $component);
226				}
227			}
228		}
229	}
230}
231
232?>