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