PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/classes/XLite/Controller/Console/AConsole.php

https://github.com/ckdimka/core
PHP | 332 lines | 128 code | 40 blank | 164 comment | 15 complexity | 7ad8ad7ea7b9da83052a0dbbba941d8b MD5 | raw file
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * PHP version 5.3.0
  17. *
  18. * @category LiteCommerce
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.litecommerce.com/
  23. * @see ____file_see____
  24. * @since 1.0.0
  25. */
  26. namespace XLite\Controller\Console;
  27. /**
  28. * Abstarct console-zone controller
  29. *
  30. * @see ____class_see____
  31. * @since 1.0.0
  32. */
  33. abstract class AConsole extends \XLite\Controller\AController
  34. {
  35. /**
  36. * Action time
  37. *
  38. * @var float
  39. * @see ____var_see____
  40. * @since 1.0.0
  41. */
  42. protected $actionTime;
  43. /**
  44. * Pure output flag
  45. *
  46. * @var boolean
  47. * @see ____var_see____
  48. * @since 1.0.0
  49. */
  50. protected $pureOutput = false;
  51. /**
  52. * Handles the request.
  53. * Parses the request variables if necessary. Attempts to call the specified action function
  54. *
  55. * @return void
  56. * @see ____func_see____
  57. * @since 1.0.0
  58. */
  59. public function handleRequest()
  60. {
  61. if ($this->checkAccess() && \XLite\Core\Request::getInstance()->help) {
  62. print ($this->getHelp());
  63. } else {
  64. set_time_limit(0);
  65. $this->actionTime = microtime(true);
  66. parent::handleRequest();
  67. if (!$this->pureOutput) {
  68. $duration = microtime(true) - $this->actionTime;
  69. $micro = $duration - floor($duration);
  70. $this->printContent(
  71. PHP_EOL . 'Execution time: '
  72. . date('H:i:s', floor($duration) - intval(date('Z')))
  73. . '.' . sprintf('%04d', $micro * 10000) . ' sec.'
  74. . PHP_EOL
  75. );
  76. }
  77. }
  78. }
  79. /**
  80. * isRedirectNeeded
  81. *
  82. * @return boolean
  83. * @see ____func_see____
  84. * @since 1.0.0
  85. */
  86. public function isRedirectNeeded()
  87. {
  88. return false;
  89. }
  90. /**
  91. * Check if current page is accessible
  92. *
  93. * @return boolean
  94. * @see ____func_see____
  95. * @since 1.0.0
  96. */
  97. public function checkAccess()
  98. {
  99. return $this->checkCLIKey();
  100. }
  101. /**
  102. * Return Viewer object
  103. *
  104. * @return \XLite\View\Controller
  105. * @see ____func_see____
  106. * @since 1.0.0
  107. */
  108. public function getViewer()
  109. {
  110. return new \XLite\View\Console(array(), $this->getViewerTemplate());
  111. }
  112. /**
  113. * Get allowed actions
  114. *
  115. * @return array
  116. * @see ____func_see____
  117. * @since 1.0.0
  118. */
  119. public function getAllowedActions()
  120. {
  121. $r = new \ReflectionCLass(get_called_class());
  122. $actions = array();
  123. foreach ($r->getMethods() as $method) {
  124. if (preg_match('/^doAction(.+)$/Ss', $method->getName(), $m)) {
  125. $actions[] = lcfirst($m[1]);
  126. }
  127. }
  128. return $actions;
  129. }
  130. /**
  131. * Check CLI key
  132. *
  133. * @return boolean
  134. * @see ____func_see____
  135. * @since 1.0.0
  136. */
  137. protected function checkCLIKey()
  138. {
  139. $cliKey = \XLite\Core\Config::getInstance()->Security->cli_key;
  140. return !$cliKey || \XLite\Core\Request::getInstance()->key == $cliKey;
  141. }
  142. /**
  143. * Get help
  144. *
  145. * @return string
  146. * @see ____func_see____
  147. * @since 1.0.0
  148. */
  149. protected function getHelp()
  150. {
  151. $help = null;
  152. $action = \XLite\Core\Request::getInstance()->action;
  153. if ($action) {
  154. $method = 'getHelp' . \XLite\Core\Converter::convertToCamelCase($action);
  155. $help = method_exists($this, $method)
  156. // Call an action-specific method
  157. ? $this->$method()
  158. : 'Action \'' . $action . '\' has not help note';
  159. } else {
  160. $help = $this->getControllerHelp();
  161. }
  162. return $help;
  163. }
  164. /**
  165. * Get controller help
  166. *
  167. * @return void
  168. * @see ____func_see____
  169. * @since 1.0.0
  170. */
  171. protected function getControllerHelp()
  172. {
  173. return 'Allowed actions: ' . PHP_EOL
  174. . implode(PHP_EOL, $this->getAllowedActions());
  175. }
  176. /**
  177. * Print content
  178. *
  179. * @param string $str Content
  180. *
  181. * @return void
  182. * @see ____func_see____
  183. * @since 1.0.0
  184. */
  185. protected function printContent($str)
  186. {
  187. print ($str);
  188. }
  189. /**
  190. * Print error
  191. *
  192. * @param string $error Error message
  193. *
  194. * @return void
  195. * @see ____func_see____
  196. * @since 1.0.0
  197. */
  198. protected function printError($error)
  199. {
  200. $this->printContent('[ERROR] ' . $error . PHP_EOL);
  201. if (!defined('CLI_RESULT_CODE')) {
  202. define('CLI_RESULT_CODE', 1);
  203. }
  204. }
  205. /**
  206. * Perform redirect
  207. *
  208. * @param string $url Redirect URL OPTIONAL
  209. *
  210. * @return void
  211. * @see ____var_see____
  212. * @since 1.0.0
  213. */
  214. protected function redirect($url = null)
  215. {
  216. }
  217. /**
  218. * Mark controller run thread as access denied
  219. *
  220. * @return void
  221. * @see ____func_see____
  222. * @since 1.0.0
  223. */
  224. protected function markAsAccessDenied()
  225. {
  226. $this->printError('Access denied');
  227. }
  228. /**
  229. * Check - script run with input stream or not
  230. *
  231. * @return boolean
  232. * @see ____func_see____
  233. * @since 1.0.0
  234. */
  235. protected function isInputStream()
  236. {
  237. $result = false;
  238. $stdin = @fopen('php://stdin', 'r');
  239. if ($stdin) {
  240. $stat = fstat($stdin);
  241. $result = 0 < $stat['size'];
  242. fclose($stdin);
  243. }
  244. return $result;
  245. }
  246. /**
  247. * Open input stream
  248. *
  249. * @return boolean
  250. * @see ____func_see____
  251. * @since 1.0.0
  252. */
  253. protected function openInputStream()
  254. {
  255. if (!isset($this->stdin)) {
  256. $this->stdin = @fopen($path, 'r');
  257. if (!$this->stdin) {
  258. $this->stdin = null;
  259. }
  260. }
  261. return isset($this->stdin);
  262. }
  263. /**
  264. * Read line form input stream
  265. *
  266. * @return string|boolean
  267. * @see ____func_see____
  268. * @since 1.0.0
  269. */
  270. protected function readInputStream()
  271. {
  272. $this->openInputStream();
  273. if ($this->openInputStream() && feof($this->stdin)) {
  274. fclose($this->stdin);
  275. $this->stdin = false;
  276. }
  277. return $this->stdin ? fgets($this->stdin) : false;
  278. }
  279. /**
  280. * Save input stream to temporary file
  281. *
  282. * @return string|void
  283. * @see ____func_see____
  284. * @since 1.0.0
  285. */
  286. protected function saveInputStream()
  287. {
  288. $path = tempnam(sys_get_temp_dir() . LC_DS, 'input');
  289. file_put_contents($path, file_get_contents('php://stdin'));
  290. return $path;
  291. }
  292. }