PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Tasks/AbstractPearTask.php

https://github.com/Proudio-Interactive/phpUnderControl
PHP | 241 lines | 114 code | 17 blank | 110 comment | 13 complexity | e7f33b90872020dfcaf0d7c25a75d8b7 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of phpUnderControl.
  4. *
  5. * PHP Version 5.2.0
  6. *
  7. * Copyright (c) 2007-2010, Manuel Pichler <mapi@phpundercontrol.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category QualityAssurance
  40. * @package Tasks
  41. * @author Manuel Pichler <mapi@phpundercontrol.org>
  42. * @copyright 2007-2010 Manuel Pichler. All rights reserved.
  43. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  44. * @version SVN: $Id$
  45. * @link http://www.phpundercontrol.org/
  46. */
  47. /**
  48. * Abstract base class for the PEAR based options.
  49. *
  50. * @category QualityAssurance
  51. * @package Tasks
  52. * @author Manuel Pichler <mapi@phpundercontrol.org>
  53. * @copyright 2007-2010 Manuel Pichler. All rights reserved.
  54. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  55. * @version Release: @package_version@
  56. * @link http://www.phpundercontrol.org/
  57. *
  58. * @property string $cliTool The PEAR cli command line tool.
  59. * @property string $pearBinaryDir An optional PEAR install directory.
  60. * @property-read string $executable The full command file name.
  61. */
  62. abstract class phpucAbstractPearTask
  63. extends phpucAbstractTask
  64. implements phpucConsoleExtensionI
  65. {
  66. /**
  67. * Constructs a new pear task.
  68. */
  69. public function __construct()
  70. {
  71. parent::__construct();
  72. $this->properties['cliTool'] = null;
  73. $this->properties['executable'] = null;
  74. $this->properties['pearBinaryDir'] = null;
  75. $this->cliTool = $this->getCliToolName();
  76. }
  77. /**
  78. * Sets the parsed console arguments.
  79. *
  80. * @param phpucConsoleArgs $args The console arguments.
  81. *
  82. * @return void
  83. */
  84. public function setConsoleArgs( phpucConsoleArgs $args )
  85. {
  86. parent::setConsoleArgs( $args );
  87. if ( $args->hasOption( 'pear-executables-dir' ) )
  88. {
  89. $this->pearBinaryDir = $args->getOption( 'pear-executables-dir' );
  90. }
  91. }
  92. /**
  93. * Does the primary validation that the command line tool exists. If the
  94. * tool exists this method passes the request to the internal template
  95. * method {@link doValidate()}.
  96. *
  97. * @return void
  98. *
  99. * @throws phpucValidateException If the validation fails.
  100. */
  101. public final function validate()
  102. {
  103. // Get possible or configured pear path
  104. if ( $this->pearBinaryDir === null )
  105. {
  106. $paths = explode( PATH_SEPARATOR, getenv( 'PATH' ) );
  107. }
  108. else
  109. {
  110. $paths = array( $this->pearBinaryDir );
  111. }
  112. $paths = array_unique( $paths );
  113. $windows = phpucFileUtil::getOS() == phpucFileUtil::OS_WINDOWS;
  114. foreach ( $paths as $path )
  115. {
  116. $fileName = sprintf(
  117. '%s/%s%s',
  118. $path,
  119. $this->cliTool,
  120. ( $windows === true ? '.bat' : '' )
  121. );
  122. if ( file_exists( $fileName ) === false )
  123. {
  124. continue;
  125. }
  126. if ( is_executable( $fileName ) === false && $windows === false )
  127. {
  128. continue;
  129. }
  130. $this->properties['executable'] = $fileName;
  131. break;
  132. }
  133. if ( $this->executable === null )
  134. {
  135. throw new phpucValidateException(
  136. "Missing cli tool '{$this->cliTool}', check the PATH variable."
  137. );
  138. }
  139. else if ( $this->pearBinaryDir === null )
  140. {
  141. $dir = dirname( $this->executable );
  142. if ( strpos( getenv( 'PATH' ), $dir ) !== false )
  143. {
  144. $this->properties['executable'] = basename( $this->executable );
  145. }
  146. }
  147. $this->doValidate();
  148. }
  149. /**
  150. * Callback method that registers a command extension.
  151. *
  152. * @param phpucConsoleInputDefinition $def
  153. * The input definition container.
  154. * @param phpucConsoleCommandI $command
  155. * The context cli command instance.
  156. *
  157. * @return void
  158. */
  159. public function registerCommandExtension(
  160. phpucConsoleInputDefinition $def,
  161. phpucConsoleCommandI $command
  162. ) {
  163. if ( !$def->hasOption( $command->getCommandId(), 'pear-executables-dir' ) )
  164. {
  165. $def->addOption(
  166. $command->getCommandId(),
  167. 'e',
  168. 'pear-executables-dir',
  169. 'The pear directory with cli scripts.',
  170. true
  171. );
  172. }
  173. }
  174. /**
  175. * Magic property setter method.
  176. *
  177. * @param string $name The property name.
  178. * @param mixed $value The property value.
  179. *
  180. * @return void
  181. * @throws OutOfRangeException If the property doesn't exist or is readonly.
  182. */
  183. public function __set( $name, $value )
  184. {
  185. switch ( $name )
  186. {
  187. case 'cliTool':
  188. $this->properties[$name] = $value;
  189. break;
  190. case 'pearBinaryDir':
  191. if ( trim( $value ) === '' )
  192. {
  193. $this->properties[$name] = null;
  194. }
  195. else
  196. {
  197. $this->properties[$name] = preg_replace(
  198. sprintf( '#%s+$#', DIRECTORY_SEPARATOR ), '', $value
  199. );
  200. }
  201. break;
  202. default:
  203. throw new OutOfRangeException(
  204. sprintf( 'Unknown or readonly property $%s.', $name )
  205. );
  206. break;
  207. }
  208. }
  209. /**
  210. * Template validate method for additional checks.
  211. *
  212. * @return void
  213. */
  214. protected function doValidate()
  215. {
  216. // Nothing todo here
  217. }
  218. /**
  219. * Must return the name of the used cli tool.
  220. *
  221. * @return string
  222. */
  223. protected abstract function getCliToolName();
  224. }