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