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:
22: /**
23: * CakeObject class provides a few generic methods used in several subclasses.
24: *
25: * Also includes methods for logging and the special method RequestAction,
26: * to call other Controllers' Actions from anywhere.
27: *
28: * @package Cake.Core
29: */
30: class CakeObject {
31:
32: /**
33: * Constructor, no-op
34: */
35: public function __construct() {
36: }
37:
38: /**
39: * CakeObject-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('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1);
86: $data = isset($extra['data']) ? $extra['data'] : null;
87: unset($extra['data']);
88:
89: if (is_string($url) && strpos($url, Router::fullBaseUrl()) === 0) {
90: $url = Router::normalize(str_replace(Router::fullBaseUrl(), '', $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 = $extra + $params;
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 int|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 int $type Error type constant. Defined in app/Config/core.php.
153: * @param null|string|array $scope The scope(s) a log message is being created in.
154: * See CakeLog::config() for more information on logging scopes.
155: * @return bool Success of log write
156: */
157: public function log($msg, $type = LOG_ERR, $scope = null) {
158: if (!is_string($msg)) {
159: $msg = print_r($msg, true);
160: }
161:
162: return CakeLog::write($type, $msg, $scope);
163: }
164:
165: /**
166: * Allows setting of multiple properties of the object in a single line of code. Will only set
167: * properties that are part of a class declaration.
168: *
169: * @param array $properties An associative array containing properties and corresponding values.
170: * @return void
171: */
172: protected function _set($properties = array()) {
173: if (is_array($properties) && !empty($properties)) {
174: $vars = get_object_vars($this);
175: foreach ($properties as $key => $val) {
176: if (array_key_exists($key, $vars)) {
177: $this->{$key} = $val;
178: }
179: }
180: }
181: }
182:
183: /**
184: * Merges this objects $property with the property in $class' definition.
185: * This classes value for the property will be merged on top of $class'
186: *
187: * This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
188: * this method as an empty function.
189: *
190: * @param array $properties The name of the properties to merge.
191: * @param string $class The class to merge the property with.
192: * @param bool $normalize Set to true to run the properties through Hash::normalize() before merging.
193: * @return void
194: */
195: protected function _mergeVars($properties, $class, $normalize = true) {
196: $classProperties = get_class_vars($class);
197: foreach ($properties as $var) {
198: if (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: