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