Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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