2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Core
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-05 02:36:53 +00:00
|
|
|
App::uses('Set', 'Utility');
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-09-04 05:34:45 +00:00
|
|
|
* Object class provides a few generic methods used in several subclasses.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Also includes methods for logging and the special method RequestAction,
|
|
|
|
* to call other Controllers' Actions from anywhere.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Core
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class Object {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-04-18 05:12:44 +00:00
|
|
|
/**
|
|
|
|
* constructor, no-op
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Object-to-string conversion.
|
|
|
|
* Each class can override this method as necessary.
|
|
|
|
*
|
|
|
|
* @return string The name of this class
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function toString() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$class = get_class($this);
|
|
|
|
return $class;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Calls a controller's method from any location. Can be used to connect controllers together
|
|
|
|
* or tie plugins into a main application. requestAction can be used to return rendered views
|
|
|
|
* or fetch the return value from controller actions.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-12-02 05:58:09 +00:00
|
|
|
* Under the hood this method uses Router::reverse() to convert the $url parameter into a string
|
2012-12-22 22:48:15 +00:00
|
|
|
* URL. You should use URL formats that are compatible with Router::reverse()
|
2011-11-13 14:44:48 +00:00
|
|
|
*
|
|
|
|
* #### Passing POST and GET data
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* POST and GET data can be simulated in requestAction. Use `$extra['url']` for
|
|
|
|
* GET data. The `$extra['data']` parameter allows POST data simulation.
|
2011-11-13 14:44:48 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param string|array $url String or array-based url. Unlike other url arrays in CakePHP, this
|
2011-12-02 05:58:09 +00:00
|
|
|
* url will not automatically handle passed and named arguments in the $url parameter.
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param array $extra if array includes the key "return" it sets the AutoRender to true. Can
|
2011-11-13 14:44:48 +00:00
|
|
|
* also be used to submit GET/POST data, and named/passed arguments.
|
2009-08-02 19:12:02 +00:00
|
|
|
* @return mixed Boolean true or false on success/failure, or contents
|
2010-01-25 20:37:55 +00:00
|
|
|
* of rendered action if 'return' is set in $extra.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requestAction($url, $extra = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($url)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-28 06:30:20 +00:00
|
|
|
App::uses('Dispatcher', 'Routing');
|
2011-10-06 00:55:17 +00:00
|
|
|
if (($index = array_search('return', $extra)) !== false) {
|
|
|
|
$extra['return'] = 0;
|
|
|
|
$extra['autoRender'] = 1;
|
|
|
|
unset($extra[$index]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-11-02 20:46:36 +00:00
|
|
|
$arrayUrl = is_array($url);
|
|
|
|
if ($arrayUrl && !isset($extra['url'])) {
|
2008-10-23 02:05:34 +00:00
|
|
|
$extra['url'] = array();
|
2008-10-22 17:12:30 +00:00
|
|
|
}
|
2012-11-02 20:46:36 +00:00
|
|
|
if ($arrayUrl && !isset($extra['data'])) {
|
|
|
|
$extra['data'] = array();
|
|
|
|
}
|
2010-11-14 06:01:11 +00:00
|
|
|
$extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
|
2011-11-13 14:44:48 +00:00
|
|
|
$data = isset($extra['data']) ? $extra['data'] : null;
|
|
|
|
unset($extra['data']);
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2012-03-24 22:10:52 +00:00
|
|
|
if (is_string($url) && strpos($url, FULL_BASE_URL) === 0) {
|
|
|
|
$url = Router::normalize(str_replace(FULL_BASE_URL, '', $url));
|
|
|
|
}
|
2010-11-14 06:01:11 +00:00
|
|
|
if (is_string($url)) {
|
|
|
|
$request = new CakeRequest($url);
|
|
|
|
} elseif (is_array($url)) {
|
|
|
|
$params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
|
|
|
|
$params = array_merge($params, $extra);
|
2012-11-02 20:46:36 +00:00
|
|
|
$request = new CakeRequest(Router::reverse($params));
|
2011-11-13 14:44:48 +00:00
|
|
|
}
|
|
|
|
if (isset($data)) {
|
|
|
|
$request->data = $data;
|
2010-11-14 06:01:11 +00:00
|
|
|
}
|
2012-11-02 20:46:36 +00:00
|
|
|
|
2010-11-14 06:01:11 +00:00
|
|
|
$dispatcher = new Dispatcher();
|
2011-08-13 01:21:05 +00:00
|
|
|
$result = $dispatcher->dispatch($request, new CakeResponse(), $extra);
|
|
|
|
Router::popRequest();
|
|
|
|
return $result;
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Calls a method on this object with the given parameters. Provides an OO wrapper
|
2010-01-25 20:37:55 +00:00
|
|
|
* for `call_user_func_array`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $method Name of the method to call
|
|
|
|
* @param array $params Parameter list to use when calling $method
|
|
|
|
* @return mixed Returns the result of the method call
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function dispatchMethod($method, $params = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
switch (count($params)) {
|
|
|
|
case 0:
|
|
|
|
return $this->{$method}();
|
|
|
|
case 1:
|
|
|
|
return $this->{$method}($params[0]);
|
|
|
|
case 2:
|
|
|
|
return $this->{$method}($params[0], $params[1]);
|
|
|
|
case 3:
|
|
|
|
return $this->{$method}($params[0], $params[1], $params[2]);
|
|
|
|
case 4:
|
|
|
|
return $this->{$method}($params[0], $params[1], $params[2], $params[3]);
|
|
|
|
case 5:
|
|
|
|
return $this->{$method}($params[0], $params[1], $params[2], $params[3], $params[4]);
|
|
|
|
default:
|
|
|
|
return call_user_func_array(array(&$this, $method), $params);
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Stop execution of the current script. Wraps exit() making
|
2010-01-25 20:37:55 +00:00
|
|
|
* testing easier.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param integer|string $status see http://php.net/exit for values
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-09 03:24:23 +00:00
|
|
|
protected function _stop($status = 0) {
|
2008-05-30 11:40:08 +00:00
|
|
|
exit($status);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Convenience method to write a message to CakeLog. See CakeLog::write()
|
2010-01-25 20:37:55 +00:00
|
|
|
* for more information on writing to logs.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $msg Log message
|
2011-06-20 00:28:40 +00:00
|
|
|
* @param integer $type Error type constant. Defined in app/Config/core.php.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return boolean Success of log write
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-05-11 13:42:54 +00:00
|
|
|
public function log($msg, $type = LOG_ERR) {
|
2011-12-07 01:09:05 +00:00
|
|
|
App::uses('CakeLog', 'Log');
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_string($msg)) {
|
|
|
|
$msg = print_r($msg, true);
|
|
|
|
}
|
2009-11-06 04:21:40 +00:00
|
|
|
return CakeLog::write($type, $msg);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Allows setting of multiple properties of the object in a single line of code. Will only set
|
2010-01-25 20:37:55 +00:00
|
|
|
* properties that are part of a class declaration.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $properties An associative array containing properties and corresponding values.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _set($properties = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($properties) && !empty($properties)) {
|
|
|
|
$vars = get_object_vars($this);
|
|
|
|
foreach ($properties as $key => $val) {
|
|
|
|
if (array_key_exists($key, $vars)) {
|
|
|
|
$this->{$key} = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-11-20 18:20:09 +00:00
|
|
|
/**
|
|
|
|
* Merges this objects $property with the property in $class' definition.
|
|
|
|
* This classes value for the property will be merged on top of $class'
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
|
2010-11-20 18:20:09 +00:00
|
|
|
* this method as an empty function.
|
|
|
|
*
|
|
|
|
* @param array $properties The name of the properties to merge.
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param string $class The class to merge the property with.
|
2012-03-11 01:57:18 +00:00
|
|
|
* @param boolean $normalize Set to true to run the properties through Hash::normalize() before merging.
|
2010-11-20 18:20:09 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-11-21 05:47:42 +00:00
|
|
|
protected function _mergeVars($properties, $class, $normalize = true) {
|
2010-11-20 18:20:09 +00:00
|
|
|
$classProperties = get_class_vars($class);
|
|
|
|
foreach ($properties as $var) {
|
|
|
|
if (
|
|
|
|
isset($classProperties[$var]) &&
|
2011-04-17 10:35:21 +00:00
|
|
|
!empty($classProperties[$var]) &&
|
2010-11-20 18:20:09 +00:00
|
|
|
is_array($this->{$var}) &&
|
|
|
|
$this->{$var} != $classProperties[$var]
|
|
|
|
) {
|
2010-11-21 05:47:42 +00:00
|
|
|
if ($normalize) {
|
2012-03-11 01:57:18 +00:00
|
|
|
$classProperties[$var] = Hash::normalize($classProperties[$var]);
|
|
|
|
$this->{$var} = Hash::normalize($this->{$var});
|
2010-11-21 05:47:42 +00:00
|
|
|
}
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->{$var} = Hash::merge($classProperties[$var], $this->{$var});
|
2010-11-20 18:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-04 00:34:07 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|