2005-06-21 23:44:49 +00:00
< ? php
2005-08-21 06:49:02 +00:00
/* SVN FILE: $Id$ */
2005-06-21 23:44:49 +00:00
/**
2005-12-24 07:56:48 +00:00
* Dispatcher takes the URL information , parses it for paramters and
2005-09-17 02:22:07 +00:00
* tells the involved controllers what to do .
2005-12-24 07:56:48 +00:00
*
* This is the heart of Cake ' s operation .
2005-08-21 06:49:02 +00:00
*
* PHP versions 4 and 5
*
2007-02-02 10:39:45 +00:00
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
2008-01-01 22:18:17 +00:00
* Copyright 2005 - 2008 , Cake Software Foundation , Inc .
2006-05-26 05:29:17 +00:00
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
2005-08-21 06:49:02 +00:00
*
2005-12-23 21:57:26 +00:00
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
2005-08-21 06:49:02 +00:00
*
2005-12-24 07:56:48 +00:00
* @ filesource
2008-01-01 22:18:17 +00:00
* @ copyright Copyright 2005 - 2008 , Cake Software Foundation , Inc .
2007-02-02 10:39:45 +00:00
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
2006-05-26 05:29:17 +00:00
* @ package cake
* @ subpackage cake . cake
2007-02-02 10:39:45 +00:00
* @ since CakePHP ( tm ) v 0.2 . 9
2006-05-26 05:29:17 +00:00
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
2005-06-21 23:44:49 +00:00
*/
2005-07-04 04:16:20 +00:00
/**
2005-10-18 22:27:39 +00:00
* List of helpers to include
2005-07-04 04:16:20 +00:00
*/
2007-12-22 01:37:46 +00:00
App :: import ( 'Core' , array ( 'Router' , 'Controller' ));
2005-06-21 23:44:49 +00:00
/**
2005-10-18 22:27:39 +00:00
* Dispatcher translates URLs to controller - action - paramter triads .
2005-12-22 01:07:28 +00:00
*
2005-06-21 23:44:49 +00:00
* Dispatches the request , creating appropriate models and controllers .
*
2006-05-26 05:29:17 +00:00
* @ package cake
* @ subpackage cake . cake
2005-06-21 23:44:49 +00:00
*/
2006-05-26 05:29:17 +00:00
class Dispatcher extends Object {
2005-07-10 05:08:19 +00:00
/**
* Base URL
2007-06-19 13:28:55 +00:00
*
2005-07-10 05:08:19 +00:00
* @ var string
2007-06-19 13:28:55 +00:00
* @ access public
2005-07-10 05:08:19 +00:00
*/
2006-05-26 05:29:17 +00:00
var $base = false ;
2007-08-05 02:03:18 +00:00
/**
* webroot path
*
* @ var string
* @ access public
*/
var $webroot = '/' ;
2006-11-21 22:03:52 +00:00
/**
* Current URL
2007-06-19 13:28:55 +00:00
*
2006-11-21 22:03:52 +00:00
* @ var string
2007-06-19 13:28:55 +00:00
* @ access public
2006-11-21 22:03:52 +00:00
*/
var $here = false ;
2005-10-28 08:25:31 +00:00
/**
2007-06-19 13:28:55 +00:00
* Admin route ( if on it )
*
2005-10-28 08:25:31 +00:00
* @ var string
2007-06-19 13:28:55 +00:00
* @ access public
2005-10-28 08:25:31 +00:00
*/
2006-05-26 05:29:17 +00:00
var $admin = false ;
2006-02-01 13:26:23 +00:00
/**
2007-06-19 13:28:55 +00:00
* Plugin being served ( if any )
*
2006-02-01 13:26:23 +00:00
* @ var string
2007-06-19 13:28:55 +00:00
* @ access public
2006-02-01 13:26:23 +00:00
*/
2006-05-26 05:29:17 +00:00
var $plugin = null ;
2007-07-25 19:42:58 +00:00
/**
* the params for this request
*
* @ var string
* @ access public
*/
var $params = null ;
2005-07-10 05:08:19 +00:00
/**
2005-09-17 02:22:07 +00:00
* Constructor .
2005-07-10 05:08:19 +00:00
*/
2007-08-02 00:16:28 +00:00
function __construct ( $url = null , $base = false ) {
2006-05-26 05:29:17 +00:00
parent :: __construct ();
2007-11-03 16:39:29 +00:00
if ( $base !== false ) {
2007-08-02 00:16:28 +00:00
Configure :: write ( 'App.base' , $base );
}
$this -> base = Configure :: read ( 'App.base' );
2007-07-25 04:38:28 +00:00
if ( $url !== null ) {
return $this -> dispatch ( $url );
}
2006-05-26 05:29:17 +00:00
}
2005-07-10 05:08:19 +00:00
/**
2005-09-17 02:22:07 +00:00
* Dispatches and invokes given URL , handing over control to the involved controllers , and then renders the results ( if autoRender is set ) .
*
2005-12-22 01:07:28 +00:00
* If no controller of given name can be found , invoke () shows error messages in
* the form of Missing Controllers information . It does the same with Actions ( methods of Controllers are called
2005-09-17 02:22:07 +00:00
* Actions ) .
2005-07-10 05:08:19 +00:00
*
2007-10-22 06:17:16 +00:00
* @ param string $url URL information to work on
* @ param array $additionalParams Settings array ( " bare " , " return " ) which is melded with the GET and POST params
2007-10-22 16:09:35 +00:00
* @ return boolean Success
2007-06-19 13:28:55 +00:00
* @ access public
2005-07-10 05:08:19 +00:00
*/
2007-08-05 02:03:18 +00:00
function dispatch ( $url = null , $additionalParams = array ()) {
2008-03-12 21:19:31 +00:00
$parse = true ;
2007-08-05 02:03:18 +00:00
if ( $this -> base === false ) {
$this -> base = $this -> baseUrl ();
}
2008-03-12 21:19:31 +00:00
if ( is_array ( $url )) {
$url = $this -> extractParams ( $url , $additionalParams );
$parse = false ;
}
2007-08-05 02:03:18 +00:00
if ( $url !== null ) {
$_GET [ 'url' ] = $url ;
}
2008-03-12 21:19:31 +00:00
if ( $parse ) {
$url = $this -> getUrl ();
}
2007-08-05 02:03:18 +00:00
$this -> here = $this -> base . '/' . $url ;
2007-12-22 01:37:46 +00:00
2007-12-29 19:05:22 +00:00
if ( $this -> cached ( $url )) {
2007-12-22 01:37:46 +00:00
exit ();
}
2008-03-12 21:19:31 +00:00
if ( $parse ) {
$this -> params = array_merge ( $this -> parseParams ( $url ), $additionalParams );
}
2007-07-25 21:08:12 +00:00
$controller = $this -> __getController ();
2008-02-11 06:18:41 +00:00
2007-11-03 16:39:29 +00:00
if ( ! is_object ( $controller )) {
Router :: setRequestInfo ( array ( $this -> params , array ( 'base' => $this -> base , 'webroot' => $this -> webroot )));
return $this -> cakeError ( 'missingController' , array (
array (
'className' => Inflector :: camelize ( $this -> params [ 'controller' ]) . 'Controller' ,
'webroot' => $this -> webroot ,
'url' => $url ,
'base' => $this -> base
)
));
2006-05-26 05:29:17 +00:00
}
2007-08-05 02:03:18 +00:00
$missingAction = $missingView = $privateAction = false ;
2007-07-25 19:42:58 +00:00
if ( empty ( $this -> params [ 'action' ])) {
$this -> params [ 'action' ] = 'index' ;
2007-04-04 07:25:48 +00:00
}
2007-08-16 05:44:06 +00:00
$prefixes = Router :: prefixes ();
2008-03-15 05:04:36 +00:00
2007-08-16 05:44:06 +00:00
if ( ! empty ( $prefixes )) {
if ( isset ( $this -> params [ 'prefix' ])) {
$this -> params [ 'action' ] = $this -> params [ 'prefix' ] . '_' . $this -> params [ 'action' ];
} elseif ( strpos ( $this -> params [ 'action' ], '_' ) !== false ) {
list ( $prefix , $action ) = explode ( '_' , $this -> params [ 'action' ]);
$privateAction = in_array ( $prefix , $prefixes );
2006-05-26 05:29:17 +00:00
}
}
2007-09-19 14:42:07 +00:00
$protected = array_map ( 'strtolower' , get_class_methods ( 'controller' ));
2007-09-11 19:41:57 +00:00
$classMethods = array_map ( 'strtolower' , get_class_methods ( $controller ));
2007-07-25 04:38:28 +00:00
2008-03-12 21:19:31 +00:00
if ( in_array ( strtolower ( $this -> params [ 'action' ]), $protected ) || strpos ( $this -> params [ 'action' ], '_' , 0 ) === 0 ) {
2007-07-25 04:38:28 +00:00
$privateAction = true ;
2006-05-26 05:29:17 +00:00
}
2007-12-29 19:05:22 +00:00
if ( ! in_array ( strtolower ( $this -> params [ 'action' ]), $classMethods )) {
2006-05-26 05:29:17 +00:00
$missingAction = true ;
}
2007-07-25 19:42:58 +00:00
if ( in_array ( 'return' , array_keys ( $this -> params )) && $this -> params [ 'return' ] == 1 ) {
2006-05-26 05:29:17 +00:00
$controller -> autoRender = false ;
}
$controller -> base = $this -> base ;
2006-11-21 22:03:52 +00:00
$controller -> here = $this -> here ;
2006-05-26 05:29:17 +00:00
$controller -> webroot = $this -> webroot ;
2007-07-25 04:38:28 +00:00
$controller -> plugin = $this -> plugin ;
2007-10-20 23:59:46 +00:00
$controller -> params =& $this -> params ;
$controller -> action =& $this -> params [ 'action' ];
2007-10-31 03:53:41 +00:00
$controller -> passedArgs = array_merge ( $this -> params [ 'pass' ], $this -> params [ 'named' ]);
2007-07-25 04:38:28 +00:00
2007-10-20 23:59:46 +00:00
if ( ! empty ( $this -> params [ 'data' ])) {
$controller -> data =& $this -> params [ 'data' ];
2006-05-26 05:29:17 +00:00
} else {
$controller -> data = null ;
}
2007-07-25 19:42:58 +00:00
if ( ! empty ( $this -> params [ 'bare' ])) {
2007-07-25 04:38:28 +00:00
$controller -> autoLayout = false ;
2006-05-26 05:29:17 +00:00
}
2007-07-25 19:42:58 +00:00
if ( isset ( $this -> params [ 'layout' ])) {
if ( $this -> params [ 'layout' ] === '' ) {
2006-08-25 17:38:53 +00:00
$controller -> autoLayout = false ;
} else {
2007-07-25 19:42:58 +00:00
$controller -> layout = $this -> params [ 'layout' ];
2006-08-25 17:38:53 +00:00
}
}
2007-07-08 21:01:31 +00:00
2007-07-25 19:42:58 +00:00
if ( isset ( $this -> params [ 'viewPath' ])) {
$controller -> viewPath = $this -> params [ 'viewPath' ];
2007-07-25 04:38:28 +00:00
}
2007-06-20 06:15:35 +00:00
foreach ( array ( 'components' , 'helpers' ) as $var ) {
2007-07-25 19:42:58 +00:00
if ( isset ( $this -> params [ $var ]) && ! empty ( $this -> params [ $var ]) && is_array ( $controller -> { $var })) {
$diff = array_diff ( $this -> params [ $var ], $controller -> { $var });
2006-08-25 17:38:53 +00:00
$controller -> { $var } = array_merge ( $controller -> { $var }, $diff );
}
}
2006-05-26 05:29:17 +00:00
2007-08-16 17:54:02 +00:00
Router :: setRequestInfo ( array ( $this -> params , array ( 'base' => $this -> base , 'here' => $this -> here , 'webroot' => $this -> webroot )));
2006-05-26 05:29:17 +00:00
$controller -> constructClasses ();
2007-10-20 23:59:46 +00:00
$this -> start ( $controller );
2006-07-23 16:13:05 +00:00
if ( $privateAction ) {
2006-07-15 03:36:53 +00:00
return $this -> cakeError ( 'privateAction' , array (
2006-10-20 17:24:37 +00:00
array (
2007-07-25 19:42:58 +00:00
'className' => Inflector :: camelize ( $this -> params [ 'controller' ] . " Controller " ),
'action' => $this -> params [ 'action' ],
2006-10-20 17:24:37 +00:00
'webroot' => $this -> webroot ,
'url' => $url ,
'base' => $this -> base
)
));
2006-05-26 05:29:17 +00:00
}
2006-09-22 16:39:15 +00:00
2007-07-25 19:42:58 +00:00
return $this -> _invoke ( $controller , $this -> params , $missingAction );
2006-05-26 05:29:17 +00:00
}
2005-09-19 20:07:16 +00:00
/**
2007-06-19 13:28:55 +00:00
* Invokes given controller ' s render action if autoRender option is set . Otherwise the
* contents of the operation are returned as a string .
2005-09-19 20:07:16 +00:00
*
2007-06-19 13:28:55 +00:00
* @ param object $controller Controller to invoke
* @ param array $params Parameters with at least the 'action' to invoke
2007-10-22 16:54:36 +00:00
* @ param boolean $missingAction Set to true if missing action should be rendered , false otherwise
2007-06-19 13:28:55 +00:00
* @ return string Output as sent by controller
* @ access protected
2005-09-19 20:07:16 +00:00
*/
2007-10-22 06:17:16 +00:00
function _invoke ( & $controller , $params , $missingAction = false ) {
2006-05-26 05:29:17 +00:00
$classVars = get_object_vars ( $controller );
if ( $missingAction && in_array ( 'scaffold' , array_keys ( $classVars ))) {
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
App :: import ( 'Core' , 'Scaffold' );
2006-05-26 05:29:17 +00:00
return new Scaffold ( $controller , $params );
2007-07-25 04:38:28 +00:00
} elseif ( $missingAction && ! in_array ( 'scaffold' , array_keys ( $classVars ))) {
2008-01-09 14:04:27 +00:00
return $this -> cakeError ( 'missingAction' , array (
array (
'className' => Inflector :: camelize ( $params [ 'controller' ] . " Controller " ),
'action' => $params [ 'action' ],
'webroot' => $this -> webroot ,
'url' => $this -> here ,
'base' => $this -> base
)
));
2006-05-26 05:29:17 +00:00
} else {
2008-02-11 06:18:41 +00:00
$output = $controller -> dispatchMethod ( $params [ 'action' ], $params [ 'pass' ]);
2006-05-26 05:29:17 +00:00
}
2007-07-25 04:38:28 +00:00
2006-05-26 05:29:17 +00:00
if ( $controller -> autoRender ) {
2008-01-26 08:50:06 +00:00
$controller -> output = $controller -> render ();
2008-02-11 06:18:41 +00:00
} elseif ( empty ( $controller -> output )) {
$controller -> output = $output ;
2006-05-26 05:29:17 +00:00
}
2007-07-25 04:38:28 +00:00
2007-06-20 06:15:35 +00:00
foreach ( $controller -> components as $c ) {
2007-03-23 10:34:21 +00:00
$path = preg_split ( '/\/|\./' , $c );
$c = $path [ count ( $path ) - 1 ];
2007-01-23 15:20:33 +00:00
if ( isset ( $controller -> { $c }) && is_object ( $controller -> { $c }) && is_callable ( array ( $controller -> { $c }, 'shutdown' ))) {
if ( ! array_key_exists ( 'enabled' , get_object_vars ( $controller -> { $c })) || $controller -> { $c } -> enabled == true ) {
$controller -> { $c } -> shutdown ( $controller );
}
}
}
2006-05-26 05:29:17 +00:00
$controller -> afterFilter ();
2008-03-12 21:19:31 +00:00
if ( isset ( $params [ 'return' ])) {
return $controller -> output ;
}
2008-03-10 13:24:36 +00:00
e ( $controller -> output );
2006-05-26 05:29:17 +00:00
}
/**
2007-06-19 13:28:55 +00:00
* Starts up a controller ( by calling its beforeFilter methods and
* starting its components )
2006-05-26 05:29:17 +00:00
*
2007-06-19 13:28:55 +00:00
* @ param object $controller Controller to start
* @ access public
2006-05-26 05:29:17 +00:00
*/
2006-07-23 16:13:05 +00:00
function start ( & $controller ) {
2006-05-26 05:29:17 +00:00
if ( ! empty ( $controller -> beforeFilter )) {
2008-03-12 21:19:31 +00:00
trigger_error ( sprintf ( __ ( 'Dispatcher::start - Controller::$beforeFilter property usage is deprecated and will no longer be supported. Use Controller::beforeFilter().' , true )), E_USER_WARNING );
2006-05-26 05:29:17 +00:00
2007-08-18 23:47:09 +00:00
if ( is_array ( $controller -> beforeFilter )) {
2007-06-20 06:15:35 +00:00
foreach ( $controller -> beforeFilter as $filter ) {
if ( is_callable ( array ( $controller , $filter )) && $filter != 'beforeFilter' ) {
2006-05-26 05:29:17 +00:00
$controller -> $filter ();
}
}
} else {
2007-06-20 06:15:35 +00:00
if ( is_callable ( array ( $controller , $controller -> beforeFilter )) && $controller -> beforeFilter != 'beforeFilter' ) {
2006-05-26 05:29:17 +00:00
$controller -> { $controller -> beforeFilter }();
}
}
}
$controller -> beforeFilter ();
2007-06-20 06:15:35 +00:00
foreach ( $controller -> components as $c ) {
2007-03-23 10:34:21 +00:00
$path = preg_split ( '/\/|\./' , $c );
$c = $path [ count ( $path ) - 1 ];
2006-05-26 05:29:17 +00:00
if ( isset ( $controller -> { $c }) && is_object ( $controller -> { $c }) && is_callable ( array ( $controller -> { $c }, 'startup' ))) {
2007-01-20 15:29:06 +00:00
if ( ! array_key_exists ( 'enabled' , get_object_vars ( $controller -> { $c })) || $controller -> { $c } -> enabled == true ) {
$controller -> { $c } -> startup ( $controller );
}
2006-05-26 05:29:17 +00:00
}
}
}
2008-03-12 21:19:31 +00:00
/**
* Sets the params when $url is passed as an array to Object :: requestAction ();
*
* @ param array $url
* @ param array $additionalParams
* @ return null
* @ access private
* @ todo commented Router :: url () . this improved performance ,
* will work on this more later .
*/
function extractParams ( $url , $additionalParams = array ()) {
$defaults = array ( 'pass' => array (), 'named' => array (), 'form' => array ());
$this -> params = array_merge ( $defaults , $url , $additionalParams );
//$url = Router::url($url);
//return $url;
}
2005-07-10 05:08:19 +00:00
/**
* Returns array of GET and POST parameters . GET parameters are taken from given URL .
*
2007-10-22 06:17:16 +00:00
* @ param string $fromUrl URL to mine for parameter information .
2005-07-10 05:08:19 +00:00
* @ return array Parameters found in POST and GET .
2007-06-19 13:28:55 +00:00
* @ access public
2005-07-10 05:08:19 +00:00
*/
2007-06-19 13:28:55 +00:00
function parseParams ( $fromUrl ) {
2008-01-26 08:50:06 +00:00
$params = array ();
2006-05-26 05:29:17 +00:00
2007-07-25 04:38:28 +00:00
if ( isset ( $_POST )) {
2008-01-26 08:50:06 +00:00
$params [ 'form' ] = $_POST ;
2007-07-25 04:38:28 +00:00
if ( ini_get ( 'magic_quotes_gpc' ) == 1 ) {
2008-01-26 08:50:06 +00:00
$params [ 'form' ] = stripslashes_deep ( $params [ 'form' ]);
}
if ( env ( 'HTTP_X_HTTP_METHOD_OVERRIDE' )) {
$params [ 'form' ][ '_method' ] = env ( 'HTTP_X_HTTP_METHOD_OVERRIDE' );
2006-05-26 05:29:17 +00:00
}
2007-10-28 05:28:45 +00:00
if ( isset ( $params [ 'form' ][ '_method' ])) {
if ( isset ( $_SERVER ) && ! empty ( $_SERVER )) {
$_SERVER [ 'REQUEST_METHOD' ] = $params [ 'form' ][ '_method' ];
} else {
$_ENV [ 'REQUEST_METHOD' ] = $params [ 'form' ][ '_method' ];
}
unset ( $params [ 'form' ][ '_method' ]);
}
2006-05-26 05:29:17 +00:00
}
2008-01-26 08:50:06 +00:00
extract ( Router :: getNamedExpressions ());
include CONFIGS . 'routes.php' ;
$params = array_merge ( Router :: parse ( $fromUrl ), $params );
2006-05-26 05:29:17 +00:00
if ( isset ( $params [ 'form' ][ 'data' ])) {
2007-05-21 03:29:49 +00:00
$params [ 'data' ] = Router :: stripEscape ( $params [ 'form' ][ 'data' ]);
2006-11-12 19:53:42 +00:00
unset ( $params [ 'form' ][ 'data' ]);
2006-05-26 05:29:17 +00:00
}
if ( isset ( $_GET )) {
if ( ini_get ( 'magic_quotes_gpc' ) == 1 ) {
2006-08-12 17:03:40 +00:00
$url = stripslashes_deep ( $_GET );
2006-05-26 05:29:17 +00:00
} else {
2006-08-12 17:03:40 +00:00
$url = $_GET ;
}
if ( isset ( $params [ 'url' ])) {
2007-12-08 06:08:03 +00:00
$params [ 'url' ] = array_merge ( $params [ 'url' ], $url );
2006-08-12 17:03:40 +00:00
} else {
$params [ 'url' ] = $url ;
2006-05-26 05:29:17 +00:00
}
}
foreach ( $_FILES as $name => $data ) {
if ( $name != 'data' ) {
$params [ 'form' ][ $name ] = $data ;
}
}
if ( isset ( $_FILES [ 'data' ])) {
foreach ( $_FILES [ 'data' ] as $key => $data ) {
foreach ( $data as $model => $fields ) {
foreach ( $fields as $field => $value ) {
2008-01-15 07:37:29 +00:00
if ( is_array ( $value )) {
$params [ 'data' ][ $model ][ $field ][ key ( $value )][ $key ] = current ( $value );
} else {
$params [ 'data' ][ $model ][ $field ][ $key ] = $value ;
}
2006-05-26 05:29:17 +00:00
}
}
}
}
return $params ;
}
2005-07-10 05:08:19 +00:00
/**
2007-07-25 04:38:28 +00:00
* Returns a base URL and sets the proper webroot
2005-07-10 05:08:19 +00:00
*
2007-10-22 06:17:16 +00:00
* @ return string Base URL
2007-06-19 13:28:55 +00:00
* @ access public
2006-05-26 05:29:17 +00:00
*/
function baseUrl () {
2007-07-25 04:38:28 +00:00
2008-01-26 08:50:06 +00:00
$dir = $webroot = null ;
2007-08-05 02:03:18 +00:00
$config = Configure :: read ( 'App' );
2007-08-06 15:09:51 +00:00
extract ( $config );
2006-05-26 05:29:17 +00:00
2007-11-03 16:39:29 +00:00
if ( ! $base ) {
$base = $this -> base ;
2007-07-25 04:38:28 +00:00
}
2006-05-26 05:29:17 +00:00
2007-11-03 16:39:29 +00:00
if ( $base !== false ) {
2007-12-30 22:33:48 +00:00
$this -> webroot = $base . '/' ;
2007-11-03 16:39:29 +00:00
return $base ;
2007-07-25 04:38:28 +00:00
}
2006-05-26 05:29:17 +00:00
2007-11-03 16:39:29 +00:00
if ( ! $baseUrl ) {
$base = dirname ( env ( 'PHP_SELF' ));
if ( $webroot === 'webroot' && $webroot === basename ( $base )) {
2008-03-12 21:19:31 +00:00
$base = dirname ( $base );
2007-08-06 15:09:51 +00:00
}
2007-11-03 16:39:29 +00:00
if ( $dir === 'app' && $dir === basename ( $base )) {
2008-03-12 21:19:31 +00:00
$base = dirname ( $base );
2007-08-06 15:09:51 +00:00
}
2007-11-05 14:56:21 +00:00
if ( in_array ( $base , array ( DS , '.' ))) {
$base = '' ;
}
2007-08-06 15:09:51 +00:00
$this -> webroot = $base . '/' ;
2007-07-25 04:38:28 +00:00
return $base ;
}
2006-05-26 05:29:17 +00:00
2007-11-03 16:39:29 +00:00
$file = null ;
if ( $baseUrl ) {
$file = '/' . basename ( $baseUrl );
$base = dirname ( $baseUrl );
if ( in_array ( $base , array ( DS , '.' ))) {
$base = '' ;
}
$this -> webroot = $base . '/' ;
2007-08-06 15:09:51 +00:00
2007-11-03 16:39:29 +00:00
if ( strpos ( $this -> webroot , $dir ) === false ) {
2008-03-12 21:19:31 +00:00
$this -> webroot .= $dir . '/' ;
2007-11-03 16:39:29 +00:00
}
if ( strpos ( $this -> webroot , $webroot ) === false ) {
$this -> webroot .= $webroot . '/' ;
}
return $base . $file ;
2007-07-25 04:38:28 +00:00
}
2007-11-03 16:39:29 +00:00
return false ;
2006-05-26 05:29:17 +00:00
}
/**
2007-06-19 13:28:55 +00:00
* Restructure params in case we ' re serving a plugin .
2006-05-26 05:29:17 +00:00
*
2007-06-19 13:28:55 +00:00
* @ param array $params Array on where to re - set 'controller' , 'action' , and 'pass' indexes
2007-12-09 07:07:28 +00:00
* @ param boolean $reverse
2007-06-19 13:28:55 +00:00
* @ return array Restructured array
* @ access protected
2005-07-10 05:08:19 +00:00
*/
2007-12-09 07:07:28 +00:00
function _restructureParams ( $params , $reverse = false ) {
if ( $reverse === true ) {
extract ( Router :: getArgs ( $params [ 'action' ]));
$params = array_merge ( $params , array ( 'controller' => $params [ 'plugin' ],
'action' => $params [ 'controller' ],
'pass' => array_merge ( $pass , $params [ 'pass' ]),
'named' => array_merge ( $named , $params [ 'named' ])));
$this -> plugin = $params [ 'plugin' ];
2006-05-26 05:29:17 +00:00
} else {
2007-12-09 07:07:28 +00:00
$params [ 'plugin' ] = $params [ 'controller' ];
$params [ 'controller' ] = $params [ 'action' ];
if ( isset ( $params [ 'pass' ][ 0 ])) {
$params [ 'action' ] = $params [ 'pass' ][ 0 ];
array_shift ( $params [ 'pass' ]);
} else {
$params [ 'action' ] = null ;
}
2006-05-26 05:29:17 +00:00
}
return $params ;
}
2007-07-25 19:42:58 +00:00
/**
* Get controller to use , either plugin controller or application controller
*
2007-10-22 06:17:16 +00:00
* @ param array $params Array of parameters
2007-07-25 19:42:58 +00:00
* @ return mixed name of controller if not loaded , or object if loaded
2007-09-27 10:58:06 +00:00
* @ access private
2007-07-25 19:42:58 +00:00
*/
2007-09-27 10:58:06 +00:00
function __getController ( $params = null ) {
if ( ! is_array ( $params )) {
2007-07-25 19:42:58 +00:00
$params = $this -> params ;
}
2007-09-27 10:58:06 +00:00
$controller = false ;
2007-12-09 07:07:28 +00:00
2007-09-27 10:58:06 +00:00
if ( ! $ctrlClass = $this -> __loadController ( $params )) {
2007-11-03 16:39:29 +00:00
if ( ! isset ( $params [ 'plugin' ])) {
2007-10-04 05:09:26 +00:00
$params = $this -> _restructureParams ( $params );
2007-12-09 07:07:28 +00:00
} else {
$params = $this -> _restructureParams ( $params , true );
2007-10-04 05:09:26 +00:00
}
2007-12-09 07:07:28 +00:00
2007-09-27 10:58:06 +00:00
if ( ! $ctrlClass = $this -> __loadController ( $params )) {
2007-12-09 07:07:28 +00:00
return false ;
2007-09-27 10:58:06 +00:00
}
}
2007-12-09 07:07:28 +00:00
$name = $ctrlClass ;
$ctrlClass = $ctrlClass . 'Controller' ;
2007-07-25 19:42:58 +00:00
2007-09-27 10:58:06 +00:00
if ( class_exists ( $ctrlClass )) {
2007-12-29 19:05:22 +00:00
if ( strtolower ( get_parent_class ( $ctrlClass )) === strtolower ( $name . 'AppController' ) && empty ( $params [ 'plugin' ])) {
$params = $this -> _restructureParams ( $params );
2007-12-09 07:07:28 +00:00
$params = $this -> _restructureParams ( $params , true );
}
2007-09-27 10:58:06 +00:00
$this -> params = $params ;
$controller =& new $ctrlClass ();
2007-07-25 19:42:58 +00:00
}
2007-09-27 10:58:06 +00:00
return $controller ;
}
/**
2007-10-22 06:17:16 +00:00
* Load controller and return controller class
2007-09-27 10:58:06 +00:00
*
2007-10-22 06:17:16 +00:00
* @ param array $params Array of parameters
* @ return string | bool Name of controller class name
2007-09-27 10:58:06 +00:00
* @ access private
*/
function __loadController ( $params ) {
2007-12-09 07:07:28 +00:00
$pluginName = $pluginPath = $controller = null ;
2007-09-27 10:58:06 +00:00
2007-07-25 19:42:58 +00:00
if ( ! empty ( $params [ 'plugin' ])) {
$this -> plugin = $params [ 'plugin' ];
2007-09-27 10:58:06 +00:00
$pluginName = Inflector :: camelize ( $params [ 'plugin' ]);
$pluginPath = $pluginName . '.' ;
2007-10-04 05:09:26 +00:00
$this -> params [ 'controller' ] = $this -> plugin ;
$controller = $pluginName ;
2007-07-25 21:08:12 +00:00
}
2007-08-02 00:16:28 +00:00
2007-09-27 10:58:06 +00:00
if ( ! empty ( $params [ 'controller' ])) {
$controller = Inflector :: camelize ( $params [ 'controller' ]);
2007-07-25 19:42:58 +00:00
}
2007-09-27 10:58:06 +00:00
if ( $pluginPath . $controller ) {
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
if ( App :: import ( 'Controller' , $pluginPath . $controller )) {
2007-12-09 07:07:28 +00:00
return $controller ;
2007-09-27 10:58:06 +00:00
}
2007-07-25 19:42:58 +00:00
}
2007-09-27 10:58:06 +00:00
return false ;
2007-07-25 19:42:58 +00:00
}
2007-08-05 02:03:18 +00:00
/**
* Returns the REQUEST_URI from the server environment , or , failing that ,
* constructs a new one , using the PHP_SELF constant and other variables .
*
* @ return string URI
2007-10-22 06:17:16 +00:00
* @ access public
2007-08-05 02:03:18 +00:00
*/
function uri () {
2007-12-30 22:33:48 +00:00
foreach ( array ( 'HTTP_X_REWRITE_URL' , 'REQUEST_URI' , 'argv' ) as $var ) {
if ( $uri = env ( $var )) {
if ( $var == 'argv' ) {
$uri = $uri [ 0 ];
2007-08-05 02:03:18 +00:00
}
2007-12-30 22:33:48 +00:00
break ;
2007-08-05 02:03:18 +00:00
}
}
2007-12-30 22:33:48 +00:00
$base = preg_replace ( '/^\//' , '' , '' . Configure :: read ( 'App.baseUrl' ));
if ( $base ) {
$uri = preg_replace ( '/^(?:\/)?(?:' . preg_quote ( $base , '/' ) . ')?(?:url=)?/' , '' , $uri );
}
2008-02-06 17:22:39 +00:00
if ( Configure :: read ( 'App.server' ) == 'IIS' ) {
$uri = preg_replace ( '/^(?:\/)?(?:\/)?(?:\?)?(?:url=)?/' , '' , $uri );
}
if ( ! empty ( $uri )) {
2007-12-30 22:33:48 +00:00
if ( key ( $_GET ) && strpos ( key ( $_GET ), '?' ) !== false ) {
unset ( $_GET [ key ( $_GET )]);
}
$uri = preg_split ( '/\?/' , $uri , 2 );
if ( isset ( $uri [ 1 ])) {
parse_str ( $uri [ 1 ], $_GET );
}
$uri = $uri [ 0 ];
} elseif ( empty ( $uri ) && is_string ( env ( 'QUERY_STRING' ))) {
$uri = env ( 'QUERY_STRING' );
}
if ( strpos ( $uri , 'index.php' ) !== false ) {
list (, $uri ) = explode ( 'index.php' , $uri , 2 );
}
if ( empty ( $uri ) || $uri == '/' || $uri == '//' ) {
return '' ;
}
return str_replace ( '//' , '/' , '/' . $uri );
2007-08-05 02:03:18 +00:00
}
/**
* Returns and sets the $_GET [ url ] derived from the REQUEST_URI
*
2007-10-22 06:17:16 +00:00
* @ param string $uri Request URI
* @ param string $base Base path
2007-08-05 02:03:18 +00:00
* @ return string URL
2007-10-22 06:17:16 +00:00
* @ access public
2007-08-05 02:03:18 +00:00
*/
function getUrl ( $uri = null , $base = null ) {
if ( empty ( $_GET [ 'url' ])) {
if ( $uri == null ) {
$uri = $this -> uri ();
}
if ( $base == null ) {
$base = $this -> base ;
}
$url = null ;
2007-12-30 22:33:48 +00:00
$tmpUri = preg_replace ( '/^(?:\?)?(?:\/)?/' , '' , $uri );
$baseDir = preg_replace ( '/^\//' , '' , dirname ( $base )) . '/' ;
if ( $tmpUri === '/' || $tmpUri == $baseDir || $tmpUri == $base ) {
2007-08-05 02:03:18 +00:00
$url = $_GET [ 'url' ] = '/' ;
} else {
2008-01-24 21:19:13 +00:00
if ( $base && strpos ( $uri , $base ) !== false ) {
2007-08-05 02:03:18 +00:00
$elements = explode ( $base , $uri );
} elseif ( preg_match ( '/^[\/\?\/|\/\?|\?\/]/' , $uri )) {
$elements = array ( 1 => preg_replace ( '/^[\/\?\/|\/\?|\?\/]/' , '' , $uri ));
} else {
$elements = array ();
}
if ( ! empty ( $elements [ 1 ])) {
$_GET [ 'url' ] = $elements [ 1 ];
$url = $elements [ 1 ];
} else {
$url = $_GET [ 'url' ] = '/' ;
}
if ( strpos ( $url , '/' ) === 0 && $url != '/' ) {
$url = $_GET [ 'url' ] = substr ( $url , 1 );
}
}
} else {
$url = $_GET [ 'url' ];
}
2007-11-03 16:39:29 +00:00
if ( $url { 0 } == '/' ) {
2007-08-05 02:03:18 +00:00
$url = substr ( $url , 1 );
}
return $url ;
}
/**
* Outputs cached dispatch for js , css , view cache
*
2007-10-22 06:17:16 +00:00
* @ param string $url Requested URL
* @ access public
2007-08-05 02:03:18 +00:00
*/
2007-08-06 15:09:51 +00:00
function cached ( $url ) {
if ( strpos ( $url , 'ccss/' ) === 0 ) {
2007-12-31 05:19:00 +00:00
include WWW_ROOT . DS . Configure :: read ( 'Asset.filter.css' );
exit ();
} elseif ( strpos ( $url , 'cjs/' ) === 0 ) {
include WWW_ROOT . DS . Configure :: read ( 'Asset.filter.js' );
2007-08-05 02:03:18 +00:00
exit ();
}
2007-12-22 01:37:46 +00:00
$assets = array ( 'js' => 'text/javascript' , 'css' => 'text/css' );
$isAsset = false ;
foreach ( $assets as $type => $contentType ) {
2007-12-29 19:05:22 +00:00
$pos = strpos ( $url , $type . '/' );
2008-03-12 21:19:31 +00:00
if ( $pos !== false ) {
2007-12-22 01:37:46 +00:00
$isAsset = true ;
break ;
}
}
if ( $isAsset === true ) {
2008-01-19 21:29:11 +00:00
$ob = @ ini_get ( " zlib.output_compression " ) !== true && extension_loaded ( " zlib " ) && ( strpos ( env ( 'HTTP_ACCEPT_ENCODING' ), 'gzip' ) !== false );
2007-12-22 01:37:46 +00:00
if ( $ob && Configure :: read ( 'Asset.compress' )) {
ob_start ();
ob_start ( 'ob_gzhandler' );
}
$assetFile = null ;
$paths = array ();
2007-12-29 19:05:22 +00:00
if ( $pos > 0 ) {
$plugin = substr ( $url , 0 , $pos - 1 );
$url = str_replace ( $plugin . '/' , '' , $url );
2007-12-22 01:37:46 +00:00
$pluginPaths = Configure :: read ( 'pluginPaths' );
$count = count ( $pluginPaths );
for ( $i = 0 ; $i < $count ; $i ++ ) {
2007-12-29 19:05:22 +00:00
$paths [] = $pluginPaths [ $i ] . $plugin . DS . 'vendors' . DS ;
2007-12-22 01:37:46 +00:00
}
}
2007-12-29 19:05:22 +00:00
$paths = array_merge ( $paths , Configure :: read ( 'vendorPaths' ));
2007-12-22 01:37:46 +00:00
foreach ( $paths as $path ) {
if ( is_file ( $path . $url ) && file_exists ( $path . $url )) {
$assetFile = $path . $url ;
break ;
}
}
if ( $assetFile !== null ) {
$fileModified = filemtime ( $assetFile );
2007-10-19 08:57:53 +00:00
header ( " Date: " . date ( " D, j M Y G:i:s " , $fileModified ) . 'GMT' );
2007-12-22 01:37:46 +00:00
header ( 'Content-type: ' . $assets [ $type ]);
2007-10-19 08:57:53 +00:00
header ( " Expires: " . gmdate ( " D, j M Y H:i:s " , time () + DAY ) . " GMT " );
header ( " Cache-Control: cache " );
header ( " Pragma: cache " );
2007-12-22 01:37:46 +00:00
include ( $assetFile );
if ( Configure :: read ( 'Asset.compress' )) {
header ( " Content-length: " . ob_get_length ());
ob_end_flush ();
}
return true ;
}
2007-08-05 02:03:18 +00:00
}
2007-10-01 16:49:37 +00:00
if ( Configure :: read ( 'Cache.check' ) === true ) {
2007-12-22 01:37:46 +00:00
$filename = CACHE . 'views' . DS . Inflector :: slug ( $this -> here ) . '.php' ;
2007-08-05 02:03:18 +00:00
if ( ! file_exists ( $filename )) {
2007-12-22 01:37:46 +00:00
$filename = CACHE . 'views' . DS . Inflector :: slug ( $this -> here ) . '_index.php' ;
2007-08-05 02:03:18 +00:00
}
if ( file_exists ( $filename )) {
2007-11-05 14:56:21 +00:00
if ( ! class_exists ( 'View' )) {
2007-12-29 19:05:22 +00:00
App :: import ( 'Core' , 'View' );
2007-11-05 14:56:21 +00:00
}
$controller = null ;
$view = new View ( $controller );
2007-12-29 19:05:22 +00:00
return $view -> renderCache ( $filename , getMicrotime ());
2007-08-05 02:03:18 +00:00
}
}
2007-12-29 19:05:22 +00:00
return false ;
2007-08-05 02:03:18 +00:00
}
2005-07-21 04:02:32 +00:00
}
2007-08-05 02:03:18 +00:00
?>