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 mixed $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: if (is_array($url) && !isset($extra['url'])) {
77: $extra['url'] = array();
78: }
79: $extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
80: $data = isset($extra['data']) ? $extra['data'] : null;
81: unset($extra['data']);
82:
83: if (is_string($url) && strpos($url, FULL_BASE_URL) === 0) {
84: $url = Router::normalize(str_replace(FULL_BASE_URL, '', $url));
85: }
86: if (is_string($url)) {
87: $request = new CakeRequest($url);
88: } elseif (is_array($url)) {
89: $params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
90: $params = array_merge($params, $extra);
91: $request = new CakeRequest(Router::reverse($params), false);
92: }
93: if (isset($data)) {
94: $request->data = $data;
95: }
96: $dispatcher = new Dispatcher();
97: $result = $dispatcher->dispatch($request, new CakeResponse(), $extra);
98: Router::popRequest();
99: return $result;
100: }
101:
102: /**
103: * Calls a method on this object with the given parameters. Provides an OO wrapper
104: * for `call_user_func_array`
105: *
106: * @param string $method Name of the method to call
107: * @param array $params Parameter list to use when calling $method
108: * @return mixed Returns the result of the method call
109: */
110: public function dispatchMethod($method, $params = array()) {
111: switch (count($params)) {
112: case 0:
113: return $this->{$method}();
114: case 1:
115: return $this->{$method}($params[0]);
116: case 2:
117: return $this->{$method}($params[0], $params[1]);
118: case 3:
119: return $this->{$method}($params[0], $params[1], $params[2]);
120: case 4:
121: return $this->{$method}($params[0], $params[1], $params[2], $params[3]);
122: case 5:
123: return $this->{$method}($params[0], $params[1], $params[2], $params[3], $params[4]);
124: default:
125: return call_user_func_array(array(&$this, $method), $params);
126: break;
127: }
128: }
129:
130: /**
131: * Stop execution of the current script. Wraps exit() making
132: * testing easier.
133: *
134: * @param integer|string $status see http://php.net/exit for values
135: * @return void
136: */
137: protected function _stop($status = 0) {
138: exit($status);
139: }
140:
141: /**
142: * Convenience method to write a message to CakeLog. See CakeLog::write()
143: * for more information on writing to logs.
144: *
145: * @param string $msg Log message
146: * @param integer $type Error type constant. Defined in app/Config/core.php.
147: * @return boolean Success of log write
148: */
149: public function log($msg, $type = LOG_ERROR) {
150: App::uses('CakeLog', 'Log');
151: if (!is_string($msg)) {
152: $msg = print_r($msg, true);
153: }
154: return CakeLog::write($type, $msg);
155: }
156:
157: /**
158: * Allows setting of multiple properties of the object in a single line of code. Will only set
159: * properties that are part of a class declaration.
160: *
161: * @param array $properties An associative array containing properties and corresponding values.
162: * @return void
163: */
164: protected function _set($properties = array()) {
165: if (is_array($properties) && !empty($properties)) {
166: $vars = get_object_vars($this);
167: foreach ($properties as $key => $val) {
168: if (array_key_exists($key, $vars)) {
169: $this->{$key} = $val;
170: }
171: }
172: }
173: }
174:
175: /**
176: * Merges this objects $property with the property in $class' definition.
177: * This classes value for the property will be merged on top of $class'
178: *
179: * This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
180: * this method as an empty function.
181: *
182: * @param array $properties The name of the properties to merge.
183: * @param string $class The class to merge the property with.
184: * @param boolean $normalize Set to true to run the properties through Set::normalize() before merging.
185: * @return void
186: */
187: protected function _mergeVars($properties, $class, $normalize = true) {
188: $classProperties = get_class_vars($class);
189: foreach ($properties as $var) {
190: if (
191: isset($classProperties[$var]) &&
192: !empty($classProperties[$var]) &&
193: is_array($this->{$var}) &&
194: $this->{$var} != $classProperties[$var]
195: ) {
196: if ($normalize) {
197: $classProperties[$var] = Set::normalize($classProperties[$var]);
198: $this->{$var} = Set::normalize($this->{$var});
199: }
200: $this->{$var} = Set::merge($classProperties[$var], $this->{$var});
201: }
202: }
203: }
204:
205: }
206: