/lib/Cake/Core/CakeObject.php

https://bitbucket.org/gowthami_gk/inventory_management · PHP · 212 lines · 103 code · 14 blank · 95 comment · 18 complexity · 9f26de87585dce4fb66e375a41e7a74f MD5 · raw file

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