/PHP/Classes/System/CLI/Combines/eGlooCombine.php

https://github.com/GunioRobot/eglooframework · PHP · 367 lines · 171 code · 70 blank · 126 comment · 37 complexity · a99f6a0dd227b1d22fba7f2f61f79c0d MD5 · raw file

  1. <?php
  2. /**
  3. * eGlooCombine Class File
  4. *
  5. * Contains the class definition for the eGlooCombine
  6. *
  7. * Copyright 2011 eGloo LLC
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * @author George Cooper
  22. * @copyright 2011 eGloo LLC
  23. * @license http://www.apache.org/licenses/LICENSE-2.0
  24. * @category System
  25. * @package CLI
  26. * @subpackage Combines
  27. * @version 1.0
  28. */
  29. /**
  30. * eGlooCombine
  31. *
  32. * $short_description
  33. *
  34. * $long_description
  35. *
  36. * @category System
  37. * @package CLI
  38. * @subpackage Combines
  39. */
  40. abstract class eGlooCombine {
  41. /**
  42. * @var string Name of the command to execute
  43. */
  44. protected $_command = null;
  45. /**
  46. * @var array Command arguments for the specified command
  47. */
  48. protected $_command_arguments = null;
  49. /**
  50. * @var bool If this forms processing object is ready to be executed
  51. */
  52. protected $_is_executable = false;
  53. /**
  54. * @var array Parsed options (key/value)
  55. */
  56. protected $_parsed_options = null;
  57. /**
  58. * @var array Raw arguments for specified command
  59. */
  60. protected $_raw_arguments = null;
  61. /**
  62. * Return an instance of this class built from the provided CLI arguments
  63. *
  64. * @return eGlooCombine subclass object
  65. * @author George Cooper
  66. **/
  67. public static function getInstanceFromCLIArgumentArray( $arguments ) {
  68. $retVal = null;
  69. $combineObject = null;
  70. $command = null;
  71. if ( !empty($arguments) ) {
  72. $command = array_shift($arguments);
  73. if ( is_string($command) && trim($command) !== '' && self::supportsCommand($command) ) {
  74. $combineObject = new static();
  75. $combineObject->setCommand( $command );
  76. $combineObject->setRawArguments( $arguments );
  77. $combineObject->parseOptions();
  78. $combineObject->parseCommandArguments();
  79. if ( $combineObject->commandRequirementsSatisfied() ) {
  80. $combineObject->setIsExecutable();
  81. }
  82. } else if ( is_string($command) && trim($command) !== '' && self::supportsEmptyCommand() ) {
  83. $combineObject = new static();
  84. array_unshift( $arguments, $command );
  85. $combineObject->setCommand( '_empty' );
  86. $combineObject->setRawArguments( $arguments );
  87. $combineObject->parseOptions();
  88. $combineObject->parseCommandArguments();
  89. if ( $combineObject->commandRequirementsSatisfied() ) {
  90. $combineObject->setIsExecutable();
  91. }
  92. }
  93. } else if ( self::supportsZeroArgumentCommand() ) {
  94. $combineObject = new static();
  95. $combineObject->setCommand( '_zero_argument' );
  96. $combineObject->setRawArguments( array() );
  97. $combineObject->parseOptions();
  98. $combineObject->parseCommandArguments();
  99. if ( $combineObject->commandRequirementsSatisfied() ) {
  100. $combineObject->setIsExecutable();
  101. }
  102. }
  103. $retVal = $combineObject;
  104. return $retVal;
  105. }
  106. public function parseCommandArguments( $arguments = null ) {
  107. $retVal = false;
  108. $command_arguments = array();
  109. if ( !$arguments ) {
  110. $arguments = $this->_raw_arguments;
  111. }
  112. foreach($arguments as $argument) {
  113. $matches = array();
  114. preg_match('/^([a-zA-Z0-9]+)$/', $argument, $matches);
  115. if ( !empty($matches) && isset($matches[1]) ) {
  116. $command_arguments[] = $matches[1];
  117. }
  118. }
  119. $this->_command_arguments = $command_arguments;
  120. $retVal = true;
  121. return $retVal;
  122. }
  123. public function parseOptions( $arguments = null ) {
  124. $retVal = false;
  125. $parsed_options = array();
  126. if ( !$arguments ) {
  127. $arguments = $this->_raw_arguments;
  128. }
  129. foreach($arguments as $argument) {
  130. $matches = array();
  131. preg_match('/^--([a-zA-Z]+?)=([a-zA-Z0-9\/_.: -]+)$/', $argument, $matches);
  132. if ( !empty($matches) && isset($matches[1]) && isset($matches[2]) ) {
  133. $value = $matches[2];
  134. if ( strtolower($value) === 'true' ) {
  135. $value = true;
  136. } else if ( strtolower($value) === 'false' ) {
  137. $value = false;
  138. } else if ( is_numeric($value) ) {
  139. if ( strpos($value, '.') !== false ) {
  140. $value = floatval( $value );
  141. } else {
  142. $value = intval( $value );
  143. }
  144. }
  145. if ( isset($parsed_options[$matches[1]]) && !is_array($parsed_options[$matches[1]]) ) {
  146. $parsed_options[$matches[1]] = array($parsed_options[$matches[1]]);
  147. $parsed_options[$matches[1]][] = $matches[2];
  148. } else {
  149. $parsed_options[$matches[1]] = $matches[2];
  150. }
  151. continue;
  152. }
  153. $matches = array();
  154. preg_match('/^--([a-zA-Z0-9]+?)$/', $argument, $matches);
  155. if ( !empty($matches) && isset($matches[1]) ) {
  156. $parsed_options[$matches[1]] = true;
  157. continue;
  158. }
  159. $matches = array();
  160. preg_match('/^-([a-zA-Z0-9]{1})$/', $argument, $matches);
  161. if ( !empty($matches) && isset($matches[1]) ) {
  162. $parsed_options[$matches[1]] = true;
  163. continue;
  164. }
  165. }
  166. $this->_parsed_options = $parsed_options;
  167. $retVal = true;
  168. return $retVal;
  169. }
  170. public static function supportsCommand( $command ) {
  171. $retVal = false;
  172. if ( in_array( $command, array_keys(static::$_supported_commands) ) ) {
  173. $retVal = true;
  174. }
  175. return $retVal;
  176. }
  177. public static function supportsEmptyCommand() {
  178. $retVal = false;
  179. if ( in_array( '_empty', array_keys(static::$_supported_commands) ) ) {
  180. $retVal = true;
  181. }
  182. return $retVal;
  183. }
  184. public static function supportsZeroArgumentCommand() {
  185. $retVal = false;
  186. if ( in_array( '_zero_argument', array_keys(static::$_supported_commands) ) ) {
  187. $retVal = true;
  188. }
  189. return $retVal;
  190. }
  191. // Getters/Setters
  192. /**
  193. * Returns protected class member $_command
  194. *
  195. * @return string Name of the command to execute
  196. */
  197. public function getCommand() {
  198. return $this->_command;
  199. }
  200. /**
  201. * Sets protected class member $_command
  202. *
  203. * @param command string Name of the command to execute
  204. */
  205. public function setCommand( $command ) {
  206. $this->_command = $command;
  207. }
  208. /**
  209. * Returns protected class member $_command_arguments
  210. *
  211. * @return array Command arguments for the specified command
  212. */
  213. public function getCommandArguments() {
  214. return $this->_command_arguments;
  215. }
  216. /**
  217. * Sets protected class member $_command_arguments
  218. *
  219. * @param command_arguments array Command arguments for the specified command
  220. */
  221. public function setCommandArguments( $command_arguments ) {
  222. $this->_command_arguments = $command_arguments;
  223. }
  224. /**
  225. * Return the help information for this class as a string
  226. *
  227. * @return string the help information for this class
  228. * @author George Cooper
  229. **/
  230. public static function getHelpString() {
  231. return 'Prepare for unforeseen consequences';
  232. }
  233. /**
  234. * Returns protected class member $_is_executable
  235. *
  236. * @return bool If this forms processing object is ready to be executed
  237. */
  238. public function isExecutable() {
  239. return $this->_is_executable;
  240. }
  241. /**
  242. * Sets protected class member $_is_executable
  243. *
  244. * @param is_executable bool If this forms processing object is ready to be executed
  245. */
  246. public function setIsExecutable( $is_executable = true ) {
  247. $this->_is_executable = $is_executable;
  248. }
  249. /**
  250. * Returns protected class member $_parsed_options
  251. *
  252. * @return array Parsed options (key/value)
  253. */
  254. public function getParsedOptions() {
  255. return $this->_parsed_options;
  256. }
  257. /**
  258. * Sets protected class member $_parsed_options
  259. *
  260. * @param parsed_options array Parsed options
  261. */
  262. public function setParsedOptions( $parsed_options ) {
  263. $this->_parsed_options = $parsed_options;
  264. }
  265. /**
  266. * Returns protected class member $_raw_arguments
  267. *
  268. * @return array Raw arguments for specified command
  269. */
  270. public function getRawArguments() {
  271. return $this->_raw_arguments;
  272. }
  273. /**
  274. * Sets protected class member $_raw_arguments
  275. *
  276. * @param raw_arguments array Raw arguments for specified command
  277. */
  278. public function setRawArguments( $raw_arguments ) {
  279. $this->_raw_arguments = $raw_arguments;
  280. }
  281. /**
  282. * Returns static class member $_supported_commands
  283. *
  284. * @return array List of supported commands and their options/required arguments
  285. */
  286. public static function getSupportedCommands() {
  287. return static::$_supported_commands;
  288. }
  289. /**
  290. * Sets static class member $_supported_commands
  291. *
  292. * @param supported_commands array List of supported commands and their options/required arguments
  293. */
  294. public static function setSupportedCommands( $supported_commands ) {
  295. static::$_supported_commands = $supported_commands;
  296. }
  297. }