PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Mage.php

https://github.com/FiveDigital/magento2
PHP | 972 lines | 492 code | 89 blank | 391 comment | 74 complexity | bcc345dce703242a177fc77a8a74c145 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Main Mage hub class
  28. */
  29. final class Mage
  30. {
  31. /**
  32. * Registry collection
  33. *
  34. * @var array
  35. */
  36. static private $_registry = array();
  37. /**
  38. * Application root absolute path
  39. *
  40. * @var string
  41. */
  42. static private $_appRoot;
  43. /**
  44. * Application model
  45. *
  46. * @var Mage_Core_Model_App
  47. */
  48. static private $_app;
  49. /**
  50. * Config Model
  51. *
  52. * @var Mage_Core_Model_Config
  53. */
  54. static private $_config;
  55. /**
  56. * Event Collection Object
  57. *
  58. * @var Varien_Event_Collection
  59. */
  60. static private $_events;
  61. /**
  62. * Object cache instance
  63. *
  64. * @var Varien_Object_Cache
  65. */
  66. static private $_objects;
  67. /**
  68. * Is downloader flag
  69. *
  70. * @var bool
  71. */
  72. static private $_isDownloader = false;
  73. /**
  74. * Is developer mode flag
  75. *
  76. * @var bool
  77. */
  78. static private $_isDeveloperMode = false;
  79. /**
  80. * Is allow throw Exception about headers already sent
  81. *
  82. * @var bool
  83. */
  84. public static $headersSentThrowsException = true;
  85. /**
  86. * Is installed flag
  87. *
  88. * @var bool
  89. */
  90. static private $_isInstalled;
  91. /**
  92. * Logger entities
  93. *
  94. * @var array
  95. */
  96. static private $_loggers = array();
  97. /**
  98. * Magento edition constants
  99. */
  100. const EDITION_COMMUNITY = 'Community';
  101. const EDITION_ENTERPRISE = 'Enterprise';
  102. const EDITION_PROFESSIONAL = 'Professional';
  103. const EDITION_GO = 'Go';
  104. /**
  105. * Current Magento edition.
  106. *
  107. * @var string
  108. * @static
  109. */
  110. static private $_currentEdition = self::EDITION_COMMUNITY;
  111. /**
  112. * Gets the current Magento version string
  113. * @link http://www.magentocommerce.com/blog/new-community-edition-release-process/
  114. *
  115. * @return string
  116. */
  117. public static function getVersion()
  118. {
  119. $i = self::getVersionInfo();
  120. return trim("{$i['major']}.{$i['minor']}.{$i['revision']}" . ($i['patch'] != '' ? ".{$i['patch']}" : "")
  121. . "-{$i['stability']}{$i['number']}", '.-');
  122. }
  123. /**
  124. * Gets the detailed Magento version information
  125. * @link http://www.magentocommerce.com/blog/new-community-edition-release-process/
  126. *
  127. * @return array
  128. */
  129. public static function getVersionInfo()
  130. {
  131. return array(
  132. 'major' => '2',
  133. 'minor' => '0',
  134. 'revision' => '0',
  135. 'patch' => '0',
  136. 'stability' => 'dev',
  137. 'number' => '29',
  138. );
  139. }
  140. /**
  141. * Get current Magento edition
  142. *
  143. * @static
  144. * @return string
  145. */
  146. public static function getEdition()
  147. {
  148. return self::$_currentEdition;
  149. }
  150. /**
  151. * Set all my static data to defaults
  152. *
  153. */
  154. public static function reset()
  155. {
  156. self::$_registry = array();
  157. self::$_appRoot = null;
  158. self::$_app = null;
  159. self::$_config = null;
  160. self::$_events = null;
  161. self::$_objects = null;
  162. self::$_isDownloader = false;
  163. self::$_isDeveloperMode = false;
  164. self::$_isInstalled = null;
  165. self::$_loggers = array();
  166. // do not reset $headersSentThrowsException
  167. }
  168. /**
  169. * Register a new variable
  170. *
  171. * @param string $key
  172. * @param mixed $value
  173. * @param bool $graceful
  174. * @throws Mage_Core_Exception
  175. */
  176. public static function register($key, $value, $graceful = false)
  177. {
  178. if (isset(self::$_registry[$key])) {
  179. if ($graceful) {
  180. return;
  181. }
  182. self::throwException('Mage registry key "'.$key.'" already exists');
  183. }
  184. self::$_registry[$key] = $value;
  185. }
  186. /**
  187. * Unregister a variable from register by key
  188. *
  189. * @param string $key
  190. */
  191. public static function unregister($key)
  192. {
  193. if (isset(self::$_registry[$key])) {
  194. if (is_object(self::$_registry[$key]) && (method_exists(self::$_registry[$key], '__destruct'))) {
  195. self::$_registry[$key]->__destruct();
  196. }
  197. unset(self::$_registry[$key]);
  198. }
  199. }
  200. /**
  201. * Retrieve a value from registry by a key
  202. *
  203. * @param string $key
  204. * @return mixed
  205. */
  206. public static function registry($key)
  207. {
  208. if (isset(self::$_registry[$key])) {
  209. return self::$_registry[$key];
  210. }
  211. return null;
  212. }
  213. /**
  214. * Set application root absolute path
  215. *
  216. * @param string $appRoot
  217. * @throws Mage_Core_Exception
  218. */
  219. public static function setRoot($appRoot = '')
  220. {
  221. if (self::$_appRoot) {
  222. return ;
  223. }
  224. if ('' === $appRoot) {
  225. // automagically find application root by dirname of Mage.php
  226. $appRoot = dirname(__FILE__);
  227. }
  228. $appRoot = realpath($appRoot);
  229. if (is_dir($appRoot) and is_readable($appRoot)) {
  230. self::$_appRoot = $appRoot;
  231. } else {
  232. self::throwException($appRoot . ' is not a directory or not readable by this user');
  233. }
  234. }
  235. /**
  236. * Retrieve application root absolute path
  237. *
  238. * @return string
  239. */
  240. public static function getRoot()
  241. {
  242. return self::$_appRoot;
  243. }
  244. /**
  245. * Retrieve Events Collection
  246. *
  247. * @return Varien_Event_Collection $collection
  248. */
  249. public static function getEvents()
  250. {
  251. return self::$_events;
  252. }
  253. /**
  254. * Varien Objects Cache
  255. *
  256. * @param string $key optional, if specified will load this key
  257. * @return Varien_Object_Cache
  258. */
  259. public static function objects($key = null)
  260. {
  261. if (!self::$_objects) {
  262. self::$_objects = new Varien_Object_Cache;
  263. }
  264. if (is_null($key)) {
  265. return self::$_objects;
  266. } else {
  267. return self::$_objects->load($key);
  268. }
  269. }
  270. /**
  271. * Retrieve application root absolute path
  272. *
  273. * @param string $type
  274. * @return string
  275. */
  276. public static function getBaseDir($type = 'base')
  277. {
  278. return self::getConfig()->getOptions()->getDir($type);
  279. }
  280. /**
  281. * Retrieve module absolute path by directory type
  282. *
  283. * @param string $type
  284. * @param string $moduleName
  285. * @return string
  286. */
  287. public static function getModuleDir($type, $moduleName)
  288. {
  289. return self::getConfig()->getModuleDir($type, $moduleName);
  290. }
  291. /**
  292. * Retrieve config value for store by path
  293. *
  294. * @param string $path
  295. * @param mixed $store
  296. * @return mixed
  297. */
  298. public static function getStoreConfig($path, $store = null)
  299. {
  300. return self::app()->getStore($store)->getConfig($path);
  301. }
  302. /**
  303. * Retrieve config flag for store by path
  304. *
  305. * @param string $path
  306. * @param mixed $store
  307. * @return bool
  308. */
  309. public static function getStoreConfigFlag($path, $store = null)
  310. {
  311. $flag = strtolower(self::getStoreConfig($path, $store));
  312. if (!empty($flag) && 'false' !== $flag) {
  313. return true;
  314. } else {
  315. return false;
  316. }
  317. }
  318. /**
  319. * Get base URL path by type
  320. *
  321. * @param string $type
  322. * @param null|bool $secure
  323. * @return string
  324. */
  325. public static function getBaseUrl($type = Mage_Core_Model_Store::URL_TYPE_LINK, $secure = null)
  326. {
  327. return self::app()->getStore()->getBaseUrl($type, $secure);
  328. }
  329. /**
  330. * Generate url by route and parameters
  331. *
  332. * @param string $route
  333. * @param array $params
  334. * @return string
  335. */
  336. public static function getUrl($route = '', $params = array())
  337. {
  338. return self::getModel('Mage_Core_Model_Url')->getUrl($route, $params);
  339. }
  340. /**
  341. * Get design package singleton
  342. *
  343. * @return Mage_Core_Model_Design_Package
  344. */
  345. public static function getDesign()
  346. {
  347. return self::getSingleton('Mage_Core_Model_Design_Package');
  348. }
  349. /**
  350. * Retrieve a config instance
  351. *
  352. * @return Mage_Core_Model_Config
  353. */
  354. public static function getConfig()
  355. {
  356. return self::$_config;
  357. }
  358. /**
  359. * Add observer to even object
  360. *
  361. * @param string $eventName
  362. * @param callback $callback
  363. * @param array $arguments
  364. * @param string $observerName
  365. */
  366. public static function addObserver($eventName, $callback, $data = array(), $observerName = '', $observerClass = '')
  367. {
  368. if ($observerClass == '') {
  369. $observerClass = 'Varien_Event_Observer';
  370. }
  371. $observer = new $observerClass();
  372. $observer->setName($observerName)->addData($data)->setEventName($eventName)->setCallback($callback);
  373. return self::getEvents()->addObserver($observer);
  374. }
  375. /**
  376. * Dispatch event
  377. *
  378. * Calls all observer callbacks registered for this event
  379. * and multiple observers matching event name pattern
  380. *
  381. * @param string $name
  382. * @param array $data
  383. * @return Mage_Core_Model_App
  384. */
  385. public static function dispatchEvent($name, array $data = array())
  386. {
  387. Magento_Profiler::start('EVENT:' . $name);
  388. $result = self::app()->dispatchEvent($name, $data);
  389. Magento_Profiler::stop('EVENT:'.$name);
  390. return $result;
  391. }
  392. /**
  393. * Retrieve model object
  394. *
  395. * @link Mage_Core_Model_Config::getModelInstance
  396. * @param string $modelClass
  397. * @param array|object $arguments
  398. * @return Mage_Core_Model_Abstract|false
  399. */
  400. public static function getModel($modelClass = '', $arguments = array())
  401. {
  402. return self::getConfig()->getModelInstance($modelClass, $arguments);
  403. }
  404. /**
  405. * Retrieve model object singleton
  406. *
  407. * @param string $modelClass
  408. * @param array $arguments
  409. * @return Mage_Core_Model_Abstract
  410. */
  411. public static function getSingleton($modelClass='', array $arguments=array())
  412. {
  413. $registryKey = '_singleton/'.$modelClass;
  414. if (!self::registry($registryKey)) {
  415. self::register($registryKey, self::getModel($modelClass, $arguments));
  416. }
  417. return self::registry($registryKey);
  418. }
  419. /**
  420. * Retrieve object of resource model
  421. *
  422. * @param string $modelClass
  423. * @param array $arguments
  424. * @return Object
  425. */
  426. public static function getResourceModel($modelClass, $arguments = array())
  427. {
  428. return self::getConfig()->getResourceModelInstance($modelClass, $arguments);
  429. }
  430. /**
  431. * Retrieve Controller instance by ClassName
  432. *
  433. * @param string $class
  434. * @param Mage_Core_Controller_Request_Http $request
  435. * @param Mage_Core_Controller_Response_Http $response
  436. * @param array $invokeArgs
  437. * @return Mage_Core_Controller_Front_Action
  438. */
  439. public static function getControllerInstance($class, $request, $response, array $invokeArgs = array())
  440. {
  441. return new $class($request, $response, $invokeArgs);
  442. }
  443. /**
  444. * Retrieve resource vodel object singleton
  445. *
  446. * @param string $modelClass
  447. * @param array $arguments
  448. * @return object
  449. */
  450. public static function getResourceSingleton($modelClass = '', array $arguments = array())
  451. {
  452. $registryKey = '_resource_singleton/'.$modelClass;
  453. if (!self::registry($registryKey)) {
  454. self::register($registryKey, self::getResourceModel($modelClass, $arguments));
  455. }
  456. return self::registry($registryKey);
  457. }
  458. /**
  459. * Returns block singleton instance, if current action exists. Otherwise returns FALSE.
  460. *
  461. * @param string $className
  462. * @return mixed
  463. */
  464. public static function getBlockSingleton($className)
  465. {
  466. $action = self::app()->getFrontController()->getAction();
  467. return $action ? $action->getLayout()->getBlockSingleton($className) : false;
  468. }
  469. /**
  470. * Retrieve helper object
  471. *
  472. * @param string $name the helper name
  473. * @return Mage_Core_Helper_Abstract
  474. */
  475. public static function helper($name)
  476. {
  477. /* Default helper class for a module */
  478. if (strpos($name, '_Helper_') === false) {
  479. $name .= '_Helper_Data';
  480. }
  481. $registryKey = '_helper/' . $name;
  482. if (!self::registry($registryKey)) {
  483. $helperClass = self::getConfig()->getHelperClassName($name);
  484. self::register($registryKey, new $helperClass);
  485. }
  486. return self::registry($registryKey);
  487. }
  488. /**
  489. * Retrieve resource helper object
  490. *
  491. * @param string $moduleName
  492. * @return Mage_Core_Model_Resource_Helper_Abstract
  493. */
  494. public static function getResourceHelper($moduleName)
  495. {
  496. $registryKey = '_resource_helper/' . $moduleName;
  497. if (!self::registry($registryKey)) {
  498. $helperClass = self::getConfig()->getResourceHelper($moduleName);
  499. self::register($registryKey, $helperClass);
  500. }
  501. return self::registry($registryKey);
  502. }
  503. /**
  504. * Return new exception by module to be thrown
  505. *
  506. * @param string $module
  507. * @param string $message
  508. * @param integer $code
  509. * @return Mage_Core_Exception
  510. */
  511. public static function exception($module = 'Mage_Core', $message = '', $code = 0)
  512. {
  513. $className = $module . '_Exception';
  514. return new $className($message, $code);
  515. }
  516. /**
  517. * Throw Exception
  518. *
  519. * @param string $message
  520. * @param string $messageStorage
  521. * @throws Mage_Core_Exception
  522. */
  523. public static function throwException($message, $messageStorage = null)
  524. {
  525. if ($messageStorage && ($storage = self::getSingleton($messageStorage))) {
  526. $storage->addError($message);
  527. }
  528. throw new Mage_Core_Exception($message);
  529. }
  530. /**
  531. * Get initialized application object.
  532. *
  533. * @param string $code
  534. * @param string $type
  535. * @param string|array $options
  536. * @return Mage_Core_Model_App
  537. */
  538. public static function app($code = '', $type = 'store', $options = array())
  539. {
  540. if (null === self::$_app) {
  541. self::$_app = new Mage_Core_Model_App();
  542. self::setRoot();
  543. self::$_events = new Varien_Event_Collection();
  544. self::_setIsInstalled($options);
  545. self::_setConfigModel($options);
  546. Magento_Profiler::start('self::app::init');
  547. self::$_app->init($code, $type, $options);
  548. Magento_Profiler::stop('self::app::init');
  549. self::$_app->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);
  550. }
  551. return self::$_app;
  552. }
  553. /**
  554. * @static
  555. * @param string $code
  556. * @param string $type
  557. * @param array $options
  558. * @param string|array $modules
  559. */
  560. public static function init($code = '', $type = 'store', $options = array(), $modules = array())
  561. {
  562. try {
  563. self::setRoot();
  564. self::$_app = new Mage_Core_Model_App();
  565. self::_setIsInstalled($options);
  566. self::_setConfigModel($options);
  567. if (!empty($modules)) {
  568. self::$_app->initSpecified($code, $type, $options, $modules);
  569. } else {
  570. self::$_app->init($code, $type, $options);
  571. }
  572. } catch (Mage_Core_Model_Session_Exception $e) {
  573. header('Location: ' . self::getBaseUrl());
  574. die;
  575. } catch (Mage_Core_Model_Store_Exception $e) {
  576. require_once(self::getBaseDir() . DS . 'pub' . DS . 'errors' . DS . '404.php');
  577. die;
  578. } catch (Exception $e) {
  579. self::printException($e);
  580. die;
  581. }
  582. }
  583. /**
  584. * Front end main entry point
  585. *
  586. * @param string $code
  587. * @param string $type
  588. * @param string|array $options
  589. */
  590. public static function run($code = '', $type = 'store', $options = array())
  591. {
  592. try {
  593. Magento_Profiler::start('mage');
  594. self::setRoot();
  595. if (isset($options['edition'])) {
  596. self::$_currentEdition = $options['edition'];
  597. }
  598. self::$_app = new Mage_Core_Model_App();
  599. if (isset($options['request'])) {
  600. self::$_app->setRequest($options['request']);
  601. }
  602. if (isset($options['response'])) {
  603. self::$_app->setResponse($options['response']);
  604. }
  605. self::$_events = new Varien_Event_Collection();
  606. self::_setIsInstalled($options);
  607. self::_setConfigModel($options);
  608. self::$_app->run(array(
  609. 'scope_code' => $code,
  610. 'scope_type' => $type,
  611. 'options' => $options,
  612. ));
  613. Magento_Profiler::stop('mage');
  614. } catch (Mage_Core_Model_Session_Exception $e) {
  615. header('Location: ' . self::getBaseUrl());
  616. } catch (Mage_Core_Model_Store_Exception $e) {
  617. require_once(self::getBaseDir() . '/pub/errors/404.php');
  618. } catch (Exception $e) {
  619. self::printException($e);
  620. }
  621. }
  622. /**
  623. * Set application isInstalled flag based on given options
  624. *
  625. * @param array $options
  626. */
  627. protected static function _setIsInstalled($options = array())
  628. {
  629. if (isset($options['is_installed'])) {
  630. self::$_isInstalled = (bool) $options['is_installed'];
  631. }
  632. }
  633. /**
  634. * Set application Config model
  635. *
  636. * @param array $options
  637. */
  638. protected static function _setConfigModel($options = array())
  639. {
  640. if (isset($options['config_model']) && Magento_Autoload::getInstance()->classExists($options['config_model'])) {
  641. $alternativeConfigModelName = $options['config_model'];
  642. unset($options['config_model']);
  643. $alternativeConfigModel = new $alternativeConfigModelName($options);
  644. } else {
  645. $alternativeConfigModel = null;
  646. }
  647. if (!is_null($alternativeConfigModel) && ($alternativeConfigModel instanceof Mage_Core_Model_Config)) {
  648. self::$_config = $alternativeConfigModel;
  649. } else {
  650. self::$_config = new Mage_Core_Model_Config($options);
  651. }
  652. }
  653. /**
  654. * Retrieve application installation flag
  655. *
  656. * @param string|array $options
  657. * @return bool
  658. */
  659. public static function isInstalled($options = array())
  660. {
  661. if (self::$_isInstalled === null) {
  662. self::setRoot();
  663. if (is_string($options)) {
  664. $options = array('etc_dir' => $options);
  665. }
  666. $etcDir = self::getRoot() . DS . 'etc';
  667. if (!empty($options['etc_dir'])) {
  668. $etcDir = $options['etc_dir'];
  669. }
  670. $localConfigFile = $etcDir . DS . 'local.xml';
  671. self::$_isInstalled = false;
  672. if (is_readable($localConfigFile)) {
  673. $localConfig = simplexml_load_file($localConfigFile);
  674. date_default_timezone_set('UTC');
  675. if (($date = $localConfig->global->install->date) && strtotime($date)) {
  676. self::$_isInstalled = true;
  677. }
  678. }
  679. }
  680. return self::$_isInstalled;
  681. }
  682. /**
  683. * log facility (??)
  684. *
  685. * @param string $message
  686. * @param integer $level
  687. * @param string $file
  688. * @param bool $forceLog
  689. */
  690. public static function log($message, $level = null, $file = '', $forceLog = false)
  691. {
  692. if (!self::getConfig()) {
  693. return;
  694. }
  695. try {
  696. $logActive = self::getStoreConfig('dev/log/active');
  697. if (empty($file)) {
  698. $file = self::getStoreConfig('dev/log/file');
  699. }
  700. }
  701. catch (Exception $e) {
  702. $logActive = true;
  703. }
  704. if (!self::$_isDeveloperMode && !$logActive && !$forceLog) {
  705. return;
  706. }
  707. $level = is_null($level) ? Zend_Log::DEBUG : $level;
  708. $file = empty($file) ? 'system.log' : $file;
  709. try {
  710. if (!isset(self::$_loggers[$file])) {
  711. $logFile = self::_expandLogFileName($file);
  712. $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
  713. $formatter = new Zend_Log_Formatter_Simple($format);
  714. $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');
  715. if (!self::$_app || !$writerModel || !is_subclass_of($writerModel, 'Zend_Log_Writer_Stream')) {
  716. $writerModel = 'Zend_Log_Writer_Stream';
  717. }
  718. /** @var $writer Zend_Log_Writer_Stream */
  719. $writer = new $writerModel($logFile);
  720. $writer->setFormatter($formatter);
  721. self::$_loggers[$file] = new Zend_Log($writer);
  722. }
  723. if (is_array($message) || is_object($message)) {
  724. $message = print_r($message, true);
  725. }
  726. self::$_loggers[$file]->log($message, $level);
  727. }
  728. catch (Exception $e) {
  729. }
  730. }
  731. /**
  732. * Expand log file name to absolute path, if necessary
  733. *
  734. * @param string $file
  735. * @return string
  736. */
  737. protected static function _expandLogFileName($file)
  738. {
  739. /*
  740. * Check whether a file is a wrapper
  741. * @link http://www.php.net/manual/en/wrappers.php
  742. */
  743. if (preg_match('#^[a-z][a-z0-9+.-]*\://#i', $file)) {
  744. return $file;
  745. }
  746. $dir = self::getBaseDir('var') . DIRECTORY_SEPARATOR . 'log';
  747. $file = $dir . DIRECTORY_SEPARATOR . $file;
  748. if (!is_dir($dir)) {
  749. mkdir($dir);
  750. chmod($dir, 0777);
  751. }
  752. if (!file_exists($file)) {
  753. file_put_contents($file, '');
  754. chmod($file, 0777);
  755. }
  756. return $file;
  757. }
  758. /**
  759. * Write exception to log
  760. *
  761. * @param Exception $e
  762. */
  763. public static function logException(Exception $e)
  764. {
  765. if (!self::getConfig()) {
  766. return;
  767. }
  768. $file = self::getStoreConfig('dev/log/exception_file');
  769. self::log("\n" . $e->__toString(), Zend_Log::ERR, $file);
  770. }
  771. /**
  772. * Set enabled developer mode
  773. *
  774. * @param bool $mode
  775. * @return bool
  776. */
  777. public static function setIsDeveloperMode($mode)
  778. {
  779. self::$_isDeveloperMode = (bool)$mode;
  780. return self::$_isDeveloperMode;
  781. }
  782. /**
  783. * Retrieve enabled developer mode
  784. *
  785. * @return bool
  786. */
  787. public static function getIsDeveloperMode()
  788. {
  789. return self::$_isDeveloperMode;
  790. }
  791. /**
  792. * Display exception
  793. *
  794. * @param Exception $e
  795. */
  796. public static function printException(Exception $e, $extra = '')
  797. {
  798. if (self::$_isDeveloperMode) {
  799. print '<pre>';
  800. if (!empty($extra)) {
  801. print $extra . "\n\n";
  802. }
  803. print $e->getMessage() . "\n\n";
  804. print $e->getTraceAsString();
  805. print '</pre>';
  806. } else {
  807. $reportData = array(
  808. !empty($extra) ? $extra . "\n\n" : '' . $e->getMessage(),
  809. $e->getTraceAsString()
  810. );
  811. // retrieve server data
  812. if (isset($_SERVER)) {
  813. if (isset($_SERVER['REQUEST_URI'])) {
  814. $reportData['url'] = $_SERVER['REQUEST_URI'];
  815. }
  816. if (isset($_SERVER['SCRIPT_NAME'])) {
  817. $reportData['script_name'] = $_SERVER['SCRIPT_NAME'];
  818. }
  819. }
  820. // attempt to specify store as a skin
  821. try {
  822. $storeCode = self::app()->getStore()->getCode();
  823. $reportData['skin'] = $storeCode;
  824. }
  825. catch (Exception $e) {}
  826. require_once(self::getBaseDir() . DS . 'pub' . DS . 'errors' . DS . 'report.php');
  827. }
  828. die();
  829. }
  830. /**
  831. * Define system folder directory url by virtue of running script directory name
  832. * Try to find requested folder by shifting to domain root directory
  833. *
  834. * @param string $folder
  835. * @param boolean $exitIfNot
  836. * @return string
  837. */
  838. public static function getScriptSystemUrl($folder, $exitIfNot = false)
  839. {
  840. $runDirUrl = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
  841. $runDir = rtrim(dirname($_SERVER['SCRIPT_FILENAME']), DS);
  842. $baseUrl = null;
  843. if (is_dir($runDir.'/'.$folder)) {
  844. $baseUrl = str_replace(DS, '/', $runDirUrl);
  845. } else {
  846. $runDirUrlArray = explode('/', $runDirUrl);
  847. $runDirArray = explode('/', $runDir);
  848. $count = count($runDirArray);
  849. for ($i=0; $i < $count; $i++) {
  850. array_pop($runDirUrlArray);
  851. array_pop($runDirArray);
  852. $_runDir = implode('/', $runDirArray);
  853. if (!empty($_runDir)) {
  854. $_runDir .= '/';
  855. }
  856. if (is_dir($_runDir.$folder)) {
  857. $_runDirUrl = implode('/', $runDirUrlArray);
  858. $baseUrl = str_replace(DS, '/', $_runDirUrl);
  859. break;
  860. }
  861. }
  862. }
  863. if (is_null($baseUrl)) {
  864. $errorMessage = "Unable detect system directory: $folder";
  865. if ($exitIfNot) {
  866. // exit because of infinity loop
  867. exit($errorMessage);
  868. } else {
  869. self::printException(new Exception(), $errorMessage);
  870. }
  871. }
  872. return $baseUrl;
  873. }
  874. /**
  875. * Set is downloader flag
  876. *
  877. * @param bool $flag
  878. */
  879. public static function setIsDownloader($flag = true)
  880. {
  881. self::$_isDownloader = $flag;
  882. }
  883. }