PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/classes/XLite/Core/Operator.php

https://github.com/ckdimka/core
PHP | 427 lines | 364 code | 6 blank | 57 comment | 3 complexity | b490215e4a6f0b6fe8085f2b2d629da6 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\Core;
  27. /**
  28. * Common operations repository
  29. *
  30. * @see ____class_see____
  31. * @since 1.0.0
  32. */
  33. class Operator extends \XLite\Base\Singleton
  34. {
  35. /**
  36. * Files repositories paths
  37. *
  38. * @var array
  39. * @see ____var_see____
  40. * @since 1.0.0
  41. */
  42. protected $filesRepositories = array(
  43. LC_DIR_COMPILE => 'compiled classes repository',
  44. LC_DIR_ROOT => 'lc root',
  45. );
  46. /**
  47. * Redirect
  48. *
  49. * @param string $location URL
  50. * @param boolean $force Check or not redirect conditions OPTIONAL
  51. * @param integer $code Operation code OPTIONAL
  52. *
  53. * @return void
  54. * @see ____func_see____
  55. * @since 1.0.0
  56. */
  57. public static function redirect($location, $force = false, $code = 302)
  58. {
  59. if (static::checkRedirectStatus() || $force) {
  60. static::setHeaderLocation($location, $code);
  61. static::finish();
  62. }
  63. }
  64. /**
  65. * Check if class exists
  66. *
  67. * @param string $name Name of class to check
  68. *
  69. * @return void
  70. * @see ____func_see____
  71. * @since 1.0.0
  72. */
  73. public static function isClassExists($name)
  74. {
  75. return class_exists($name, false)
  76. || file_exists(LC_DIR_CACHE_CLASSES . str_replace('\\', LC_DS, $name) . '.php');
  77. }
  78. /**
  79. * Get URL content
  80. *
  81. * @param string $url URL
  82. *
  83. * @return string|void
  84. * @see ____func_see____
  85. * @since 1.0.0
  86. */
  87. public static function getURLContent($url)
  88. {
  89. $result = null;
  90. if (ini_get('allow_url_fopen')) {
  91. $result = file_get_contents($url);
  92. } else {
  93. $request = new \XLite\Core\HTTP\Request($url);
  94. $response = $bouncer->sendRequest();
  95. if (200 == $response->code) {
  96. $result = $response->body;
  97. }
  98. }
  99. return $result;
  100. }
  101. /**
  102. * Calculate pagination info
  103. *
  104. * @param integer $count Items count
  105. * @param integer $page Current page index OPTIONAL
  106. * @param integer $limit Page length limit OPTIONAL
  107. *
  108. * @return array (pages count + current page number)
  109. * @see ____func_see____
  110. * @since 1.0.0
  111. */
  112. public static function calculatePagination($count, $page = 1, $limit = 20)
  113. {
  114. $count = max(0, intval($count));
  115. $limit = max(0, intval($limit));
  116. if (0 == $limit && $count) {
  117. $pages = 1;
  118. } else {
  119. $pages = 0 == $count ? 0 : ceil($count / $limit);
  120. }
  121. $page = min($pages, max(1, intval($page)));
  122. return array($pages, $page);
  123. }
  124. /**
  125. * Check if we need to perform a redirect or not
  126. *
  127. * @return boolean
  128. * @see ____var_see____
  129. * @since 1.0.0
  130. */
  131. protected static function checkRedirectStatus()
  132. {
  133. return !\XLite\Core\CMSConnector::isCMSStarted()
  134. || !\XLite\Core\Request::getInstance()->__get(\XLite\Core\CMSConnector::NO_REDIRECT);
  135. }
  136. /**
  137. * setHeaderLocation
  138. *
  139. * @param string $location URL
  140. * @param integer $code Operation code OPTIONAL
  141. *
  142. * @return void
  143. * @see ____var_see____
  144. * @since 1.0.0
  145. */
  146. protected static function setHeaderLocation($location, $code = 302)
  147. {
  148. $location = \Includes\Utils\Converter::removeCRLF($location);
  149. if (headers_sent()) {
  150. // HTML meta tags-based redirect
  151. echo (
  152. '<script type="text/javascript">' . "\n"
  153. . '<!--' . "\n"
  154. . 'self.location=\'' . $location . '\';' . "\n"
  155. . '-->' . "\n"
  156. . '</script>' . "\n"
  157. . '<noscript><a href="' . $location . '">Click here to redirect</a></noscript><br /><br />'
  158. );
  159. } elseif (\XLite\Core\Request::getInstance()->isAJAX() && 200 == $code) {
  160. // AJAX-based redirct
  161. header('AJAX-Location: ' . $location, true, $code);
  162. } else {
  163. // HTTP-based redirect
  164. header('Location: ' . $location, true, $code);
  165. }
  166. }
  167. /**
  168. * finish
  169. *
  170. * @return void
  171. * @see ____var_see____
  172. * @since 1.0.0
  173. */
  174. protected static function finish()
  175. {
  176. exit (0);
  177. }
  178. /**
  179. * Display 404 page
  180. *
  181. * @return void
  182. * @see ____func_see____
  183. * @since 1.0.0
  184. */
  185. public function display404()
  186. {
  187. if (!headers_sent()) {
  188. header('HTTP/1.0 404 Not Found');
  189. header('Status: 404 Not Found');
  190. }
  191. echo ('404 Page not found');
  192. exit (1);
  193. }
  194. /**
  195. * Get back trace list
  196. * FIXME: to revise
  197. *
  198. * @param integer $slice Trace slice count OPTIONAL
  199. *
  200. * @return array
  201. * @see ____func_see____
  202. * @since 1.0.0
  203. */
  204. public function getBackTrace($slice = 0)
  205. {
  206. return $this->prepareBackTrace(debug_backtrace(false), $slice);
  207. }
  208. /**
  209. * Prepare back trace raw data
  210. *
  211. * @param array $backTrace Back trace raw data
  212. * @param integer $slice Trace slice count OPTIONAL
  213. *
  214. * @return array
  215. * @see ____func_see____
  216. * @since 1.0.0
  217. */
  218. public function prepareBackTrace(array $backTrace, $slice = 0)
  219. {
  220. $patterns = array_keys($this->filesRepositories);
  221. $placeholders = preg_replace('/^(.+)$/Ss', '<\1>/', array_values($this->filesRepositories));
  222. $slice = max(0, $slice) + 1;
  223. $trace = array();
  224. foreach ($backTrace as $l) {
  225. if (0 < $slice) {
  226. $slice--;
  227. } else {
  228. $parts = array();
  229. if (isset($l['file'])) {
  230. $parts[] = 'file ' . str_replace($patterns, $placeholders, $l['file']);
  231. } elseif (isset($l['class']) && isset($l['function'])) {
  232. $parts[] = 'method ' . $l['class'] . '::' . $l['function'] . $this->getBackTraceArgs($l);
  233. } elseif (isset($l['function'])) {
  234. $parts[] = 'function ' . $l['function'] . $this->getBackTraceArgs($l);
  235. }
  236. if (isset($l['line'])) {
  237. $parts[] = $l['line'];
  238. }
  239. if ($parts) {
  240. $trace[] = implode(' : ', $parts);
  241. }
  242. }
  243. }
  244. return $trace;
  245. }
  246. /**
  247. * Save service YAML
  248. *
  249. * @param string $path File path
  250. * @param array $data Data
  251. *
  252. * @return integer
  253. * @see ____func_see____
  254. * @since 1.0.0
  255. */
  256. public function saveServiceYAML($path, array $data)
  257. {
  258. return file_put_contents(
  259. $path,
  260. $this->getServiceHeader() . \Symfony\Component\Yaml\Yaml::dump($data)
  261. );
  262. }
  263. /**
  264. * Load service YAML
  265. *
  266. * @param string $path File path
  267. *
  268. * @return void
  269. * @see ____func_see____
  270. * @since 1.0.0
  271. */
  272. public function loadServiceYAML($path)
  273. {
  274. $data = null;
  275. if (file_exists($path)) {
  276. $data = \Symfony\Component\Yaml\Yaml::load($path);
  277. }
  278. return $data;
  279. }
  280. /**
  281. * Get back trace function or method arguments
  282. * FIXME: to revise
  283. *
  284. * @param array $l Back trace record
  285. *
  286. * @return string
  287. * @see ____func_see____
  288. * @since 1.0.0
  289. */
  290. protected function getBackTraceArgs(array $l)
  291. {
  292. $args = array();
  293. if (!isset($l['args'])) {
  294. $l['args'] = array();
  295. }
  296. foreach ($l['args'] as $arg) {
  297. if (is_bool($arg)) {
  298. $args[] = $arg ? 'true' : 'false';
  299. } elseif (is_int($arg) || is_float($arg)) {
  300. $args[] = $arg;
  301. } elseif (is_string($arg)) {
  302. if (is_callable($arg)) {
  303. $args[] = 'lambda function';
  304. } else {
  305. $args[] = '\'' . $arg . '\'';
  306. }
  307. } elseif (is_resource($arg)) {
  308. $args[] = strval($arg);
  309. } elseif (is_array($arg)) {
  310. if (is_callable($arg)) {
  311. $args[] = 'callback ' . $this->detectClassName($arg[0]) . '::' . $arg[1];
  312. } else {
  313. $args[] = 'array(' . count($arg) . ')';
  314. }
  315. } elseif (is_object($arg)) {
  316. if (
  317. is_callable($arg)
  318. && class_exists('Closure')
  319. && $arg instanceof Closure
  320. ) {
  321. $args[] = 'anonymous function';
  322. } else {
  323. $args[] = 'object of ' . $this->detectClassName($arg);
  324. }
  325. } elseif (!isset($arg)) {
  326. $args[] = 'null';
  327. } else {
  328. $args[] = 'variable of ' . gettype($arg);
  329. }
  330. }
  331. return '(' . implode(', ', $args) . ')';
  332. }
  333. /**
  334. * detectClassName
  335. * FIXME: unknown functionality
  336. *
  337. * @param mixed $class ____param_comment____
  338. *
  339. * @return void
  340. * @see ____func_see____
  341. * @since 1.0.0
  342. */
  343. protected function detectClassName($class)
  344. {
  345. return get_class($class);
  346. }
  347. /**
  348. * Get data storage service header
  349. *
  350. * @return string
  351. * @see ____func_see____
  352. * @since 1.0.0
  353. */
  354. protected function getServiceHeader()
  355. {
  356. return '# <' . '?php if (!defined(\'LC_DS\')) { die(); } ?' . '>' . PHP_EOL . PHP_EOL;
  357. }
  358. }