1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11: * @link http://cakephp.org CakePHP(tm) Project
12: * @package Cake.Core
13: * @since CakePHP(tm) v 0.2.9
14: * @license http://www.opensource.org/licenses/mit-license.php MIT License
15: */
16:
17: App::uses('CakeLog', 'Log');
18: App::uses('Dispatcher', 'Routing');
19: App::uses('Set', 'Utility');
20:
21: /**
22: * Object class provides a few generic methods used in several subclasses.
23: *
24: * Also includes methods for logging and the special method RequestAction,
25: * to call other Controllers' Actions from anywhere.
26: *
27: * @package Cake.Core
28: */
29: class Object {
30:
31: /**
32: * constructor, no-op
33: *
34: */
35: public function __construct() {
36: }
37:
38: /**
39: * Object-to-string conversion.
40: * Each class can override this method as necessary.
41: *
42: * @return string The name of this class
43: */
44: public function toString() {
45: $class = get_class($this);
46: return $class;
47: }
48:
49: /**
50: * Calls a controller's method from any location. Can be used to connect controllers together
51: * or tie plugins into a main application. requestAction can be used to return rendered views
52: * or fetch the return value from controller actions.
53: *
54: * Under the hood this method uses Router::reverse() to convert the $url parameter into a string
55: * URL. You should use URL formats that are compatible with Router::reverse()
56: *
57: * #### Passing POST and GET data
58: *
59: * POST and GET data can be simulated in requestAction. Use `$extra['url']` for
60: * GET data. The `$extra['data']` parameter allows POST data simulation.
61: *
62: * @param string|array $url String or array-based URL. Unlike other URL arrays in CakePHP, this
63: * URL will not automatically handle passed and named arguments in the $url parameter.
64: * @param array $extra if array includes the key "return" it sets the AutoRender to true. Can
65: * also be used to submit GET/POST data, and named/passed arguments.
66: * @return mixed Boolean true or false on success/failure, or contents
67: * of rendered action if 'return' is set in $extra.
68: */
69: public function requestAction($url, $extra = array()) {
70: if (empty($url)) {
71: return false;
72: }
73: if (($index = array_search('return', $extra)) !== false) {
74: $extra['return'] = 0;
75: $extra['autoRender'] = 1;
76: unset($extra[$index]);
77: }
78: $arrayUrl = is_array($url);
79: if ($arrayUrl && !isset($extra['url'])) {
80: $extra['url'] = array();
81: }
82: if ($arrayUrl && !isset($extra['data'])) {
83: $extra['data'] = array();
84: }
85: $extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
86: $data = isset($extra['data']) ? $extra['data'] : null;
87: unset($extra['data']);
88:
89: if (is_string($url) && strpos($url, FULL_BASE_URL) === 0) {
90: $url = Router::normalize(str_replace(FULL_BASE_URL, '', $url));
91: }
92: if (is_string($url)) {
93: $request = new CakeRequest($url);
94: } elseif (is_array($url)) {
95: $params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
96: $params = array_merge($params, $extra);
97: $request = new CakeRequest(Router::reverse($params));
98: }
99: if (isset($data)) {
100: $request->data = $data;
101: }
102:
103: $dispatcher = new Dispatcher();
104: $result = $dispatcher->dispatch($request, new CakeResponse(), $extra);
105: Router::popRequest();
106: return $result;
107: }
108:
109: /**
110: * Calls a method on this object with the given parameters. Provides an OO wrapper
111: * for `call_user_func_array`
112: *
113: * @param string $method Name of the method to call
114: * @param array $params Parameter list to use when calling $method
115: * @return mixed Returns the result of the method call
116: */
117: public function dispatchMethod($method, $params = array()) {
118: switch (count($params)) {
119: case 0:
120: return $this->{$method}();
121: case 1:
122: return $this->{$method}($params[0]);
123: case 2:
124: return $this->{$method}($params[0], $params[1]);
125: case 3:
126: return $this->{$method}($params[0], $params[1], $params[2]);
127: case 4:
128: return $this->{$method}($params[0], $params[1], $params[2], $params[3]);
129: case 5:
130: return $this->{$method}($params[0], $params[1], $params[2], $params[3], $params[4]);
131: default:
132: return call_user_func_array(array(&$this, $method), $params);
133: }
134: }
135:
136: /**
137: * Stop execution of the current script. Wraps exit() making
138: * testing easier.
139: *
140: * @param integer|string $status see http://php.net/exit for values
141: * @return void
142: */
143: protected function _stop($status = 0) {
144: exit($status);
145: }
146:
147: /**
148: * Convenience method to write a message to CakeLog. See CakeLog::write()
149: * for more information on writing to logs.
150: *
151: * @param string $msg Log message.
152: * @param integer|string $type Type of message being written. Either a valid
153: * LOG_* constant or a string matching the recognized levels.
154: * @return boolean Success of log write.
155: * @see CakeLog::write()
156: */
157: public function log($msg, $type = LOG_ERR) {
158: if (!is_string($msg)) {
159: $msg = print_r($msg, true);
160: }
161: return CakeLog::write($type, $msg);
162: }
163:
164: /**
165: * Allows setting of multiple properties of the object in a single line of code. Will only set
166: * properties that are part of a class declaration.
167: *
168: * @param array $properties An associative array containing properties and corresponding values.
169: * @return void
170: */
171: protected function _set($properties = array()) {
172: if (is_array($properties) && !empty($properties)) {
173: $vars = get_object_vars($this);
174: foreach ($properties as $key => $val) {
175: if (array_key_exists($key, $vars)) {
176: $this->{$key} = $val;
177: }
178: }
179: }
180: }
181:
182: /**
183: * Merges this objects $property with the property in $class' definition.
184: * This classes value for the property will be merged on top of $class'
185: *
186: * This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
187: * this method as an empty function.
188: *
189: * @param array $properties The name of the properties to merge.
190: * @param string $class The class to merge the property with.
191: * @param boolean $normalize Set to true to run the properties through Hash::normalize() before merging.
192: * @return void
193: */
194: protected function _mergeVars($properties, $class, $normalize = true) {
195: $classProperties = get_class_vars($class);
196: foreach ($properties as $var) {
197: if (
198: isset($classProperties[$var]) &&
199: !empty($classProperties[$var]) &&
200: is_array($this->{$var}) &&
201: $this->{$var} != $classProperties[$var]
202: ) {
203: if ($normalize) {
204: $classProperties[$var] = Hash::normalize($classProperties[$var]);
205: $this->{$var} = Hash::normalize($this->{$var});
206: }
207: $this->{$var} = Hash::merge($classProperties[$var], $this->{$var});
208: }
209: }
210: }
211:
212: }
213: