/app/code/core/Mage/Core/Model/ShellAbstract.php

https://bitbucket.org/jokusafet/magento2 · PHP · 206 lines · 97 code · 15 blank · 94 comment · 16 complexity · edf4f30a0d304e3cac49d7e5f77a42b7 MD5 · raw file

  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_Shell
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Shell scripts abstract class
  28. *
  29. * @category Mage
  30. * @package Mage_Shell
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Core_Model_ShellAbstract
  34. {
  35. /**
  36. * Raw arguments, that should be parsed
  37. *
  38. * @var array
  39. */
  40. protected $_rawArgs = array();
  41. /**
  42. * Parsed input arguments
  43. *
  44. * @var array
  45. */
  46. protected $_args = array();
  47. /**
  48. * Entry point - script filename that is executed
  49. *
  50. * @var string
  51. */
  52. protected $_entryPoint = null;
  53. /**
  54. * Initializes application and parses input parameters
  55. *
  56. * @var string $entryPoint
  57. */
  58. public function __construct($entryPoint)
  59. {
  60. if (isset($_SERVER['REQUEST_METHOD'])) {
  61. throw new Exception('This script cannot be run from Browser. This is the shell script.');
  62. }
  63. $this->_entryPoint = $entryPoint;
  64. $this->_rawArgs = $_SERVER['argv'];
  65. $this->_applyPhpVariables();
  66. $this->_parseArgs();
  67. }
  68. /**
  69. * Sets raw arguments to be parsed
  70. *
  71. * @param array $args
  72. * @return Mage_Core_Model_ShellAbstract
  73. */
  74. public function setRawArgs($args)
  75. {
  76. $this->_rawArgs = $args;
  77. $this->_parseArgs();
  78. return $this;
  79. }
  80. /**
  81. * Gets Magento root path (with last directory separator)
  82. *
  83. * @return string
  84. */
  85. protected function _getRootPath()
  86. {
  87. return Mage::getBaseDir() . '/../';
  88. }
  89. /**
  90. * Parses .htaccess file and apply php settings to shell script
  91. *
  92. * @return Mage_Core_Model_ShellAbstract
  93. */
  94. protected function _applyPhpVariables()
  95. {
  96. $htaccess = $this->_getRootPath() . '.htaccess';
  97. if (file_exists($htaccess)) {
  98. // parse htaccess file
  99. $data = file_get_contents($htaccess);
  100. $matches = array();
  101. preg_match_all('#^\s+?php_value\s+([a-z_]+)\s+(.+)$#siUm', $data, $matches, PREG_SET_ORDER);
  102. if ($matches) {
  103. foreach ($matches as $match) {
  104. @ini_set($match[1], str_replace("\r", '', $match[2]));
  105. }
  106. }
  107. preg_match_all('#^\s+?php_flag\s+([a-z_]+)\s+(.+)$#siUm', $data, $matches, PREG_SET_ORDER);
  108. if ($matches) {
  109. foreach ($matches as $match) {
  110. @ini_set($match[1], str_replace("\r", '', $match[2]));
  111. }
  112. }
  113. }
  114. return $this;
  115. }
  116. /**
  117. * Parses input arguments
  118. *
  119. * @return Mage_Core_Model_ShellAbstract
  120. */
  121. protected function _parseArgs()
  122. {
  123. $current = null;
  124. foreach ($this->_rawArgs as $arg) {
  125. $match = array();
  126. if (preg_match('#^--([\w\d_-]{1,})(=(.*))?$#', $arg, $match)
  127. || preg_match('#^-([\w\d_]{1,})$#', $arg, $match) ) {
  128. if (isset($match[3])) {
  129. $this->_args[$match[1]] = $match[3];
  130. $current = null;
  131. } else {
  132. $current = $match[1];
  133. $this->_args[$current] = true;
  134. }
  135. } else {
  136. if ($current) {
  137. $this->_args[$current] = $arg;
  138. $current = null;
  139. } else if (preg_match('#^([\w\d_]{1,})$#', $arg, $match)) {
  140. $this->_args[$match[1]] = true;
  141. }
  142. }
  143. }
  144. return $this;
  145. }
  146. /**
  147. * Runs script
  148. *
  149. * @return Mage_Core_Model_ShellAbstract
  150. */
  151. abstract public function run();
  152. /**
  153. * Shows usage help, if requested
  154. *
  155. * @return bool
  156. */
  157. protected function _showHelp()
  158. {
  159. if (isset($this->_args['h']) || isset($this->_args['help'])) {
  160. echo $this->getUsageHelp();
  161. return true;
  162. }
  163. return false;
  164. }
  165. /**
  166. * Retrieves usage help message
  167. *
  168. * @return string
  169. */
  170. public function getUsageHelp()
  171. {
  172. return <<<USAGE
  173. Usage: php -f {$this->_entryPoint} -- [options]
  174. -h Short alias for help
  175. help This help
  176. USAGE;
  177. }
  178. /**
  179. * Retrieves argument value by name. If argument is not found - returns FALSE.
  180. *
  181. * @param string $name the argument name
  182. * @return mixed
  183. */
  184. public function getArg($name)
  185. {
  186. if (isset($this->_args[$name])) {
  187. return $this->_args[$name];
  188. }
  189. return false;
  190. }
  191. }