PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Xinc.Packager/Classes/Cli.php

https://gitlab.com/Tiger66639/xinc
PHP | 262 lines | 161 code | 21 blank | 80 comment | 16 complexity | 4f7b484fb4d51dc33002821d9ff22f5f MD5 | raw file
  1. <?php
  2. /**
  3. * Xinc - Cross integration and continous management.
  4. * This script belongs to the Xinc package "Xinc.Packager".
  5. *
  6. * It is free software; you can redistribute it and/or modify it under the terms of the
  7. * GNU Lesser General Public License, either version 3 of the License, or (at your option) any later version.
  8. *
  9. * @package Xinc.Packager
  10. * @author Alexander Opitz <opitz.alexander@googlemail.com>
  11. * @license http://www.gnu.org/copyleft/lgpl.html GNU LGPL 3+
  12. * @see http://code.google.com/p/xinc/
  13. */
  14. namespace Xinc\Packager;
  15. /**
  16. * This is the entry class for command line interface operations.
  17. */
  18. class Cli
  19. {
  20. /** @type array Holding the merged cli parameters. */
  21. private $options = array();
  22. /**
  23. * Handle command line arguments.
  24. *
  25. * @return void
  26. */
  27. protected function parseOptions()
  28. {
  29. $workingDir = dirname($_SERVER['argv'][0]);
  30. $opts = getopt(
  31. 'p:c:j:i:u:a:d:',
  32. array(
  33. 'package-dir:',
  34. 'composer-bin-dir:',
  35. 'composer-json-dir:',
  36. 'install:',
  37. 'uninstall:',
  38. 'activate:',
  39. 'deactivate:',
  40. 'version',
  41. 'help',
  42. )
  43. );
  44. if (isset($opts['version'])) {
  45. $this->showVersion();
  46. exit();
  47. }
  48. if (isset($opts['help'])) {
  49. $this->showHelp();
  50. exit();
  51. }
  52. $this->options = $this->mergeOpts(
  53. $opts,
  54. array(
  55. 'p' => 'package-dir',
  56. 'c' => 'composer-bin-dir',
  57. 'j' => 'composer-json-dir',
  58. 'i' => 'install',
  59. 'u' => 'uninstall',
  60. 'a' => 'activate',
  61. 'd' => 'deactivate',
  62. ),
  63. array (
  64. 'package-dir' => $this->getPackageDir(),
  65. 'composer-bin-dir' => $this->getComposerBinDir(),
  66. 'composer-json-dir' => $this->getComposerJsonDir(),
  67. )
  68. );
  69. }
  70. /**
  71. * Returns path to the directory where configuration should be (PackageStates.php) but not tested.
  72. *
  73. * @return string Path to configuration.
  74. */
  75. protected function getPackageDir()
  76. {
  77. return __DIR__ . '/../../../../Configuration';
  78. }
  79. /**
  80. * Returns path to the directory where the composer executable should be.
  81. *
  82. * @return string Path to composer executable.
  83. */
  84. protected function getComposerBinDir()
  85. {
  86. return __DIR__ . '/../../../..';
  87. }
  88. /**
  89. * Returns path to the directory where the composer.json configuration should be.
  90. *
  91. * @return string Path to composer json.
  92. */
  93. protected function getComposerJsonDir()
  94. {
  95. return __DIR__ . '/../../../..';
  96. }
  97. /**
  98. * Validates the existence of given directory options (package-dir, composer-dir)
  99. *
  100. * @throws \Xinc\Packager\Exception
  101. */
  102. protected function validateFileDirectoryOptions()
  103. {
  104. $this->options['package-dir'] = $this->checkDirectory($this->options['package-dir']);
  105. $this->options['composer-bin-dir'] = $this->checkDirectory($this->options['composer-bin-dir']);
  106. $this->options['composer-json-dir'] = $this->checkDirectory($this->options['composer-json-dir']);
  107. }
  108. /**
  109. * Validates that given options do not collide
  110. *
  111. * @throws \Xinc\Packager\Exception
  112. */
  113. protected function validateManagementOptions()
  114. {
  115. $managementOptionsSet = 0;
  116. if (isset($this->options['install'])) {
  117. $managementOptionsSet++;
  118. }
  119. if (isset($this->options['uninstall'])) {
  120. $managementOptionsSet++;
  121. }
  122. if (isset($this->options['activate'])) {
  123. $managementOptionsSet++;
  124. }
  125. if (isset($this->options['deactivate'])) {
  126. $managementOptionsSet++;
  127. }
  128. if ($managementOptionsSet === 0) {
  129. throw new \Xinc\Packager\Exception('No management option set.');
  130. }
  131. if ($managementOptionsSet > 1) {
  132. throw new \Xinc\Packager\Exception('Too much management options set.');
  133. }
  134. }
  135. /**
  136. * Merges the default config and the short/long arguments given by mapping together.
  137. * TODO: It doesn't respect options which aren't in the mapping.
  138. *
  139. * @param array $opts The options after php getopt function call.
  140. * @param array $mapping Mapping from short to long argument names.
  141. * @param array $default The default values for some arguments.
  142. *
  143. * @return array Mapping of the long arguments to the given values.
  144. */
  145. protected function mergeOpts($opts, $mapping, $default)
  146. {
  147. $merge = $default;
  148. foreach ($mapping as $keyShort => $keyLong) {
  149. if (isset($opts[$keyShort])) {
  150. $merge[$keyLong] = $opts[$keyShort];
  151. }
  152. if (isset($opts[$keyLong])) {
  153. $merge[$keyLong] = $opts[$keyLong];
  154. }
  155. }
  156. return $merge;
  157. }
  158. /**
  159. * Checks if the directory is available otherwise tries to create it.
  160. * Returns the realpath of the directory afterwards.
  161. *
  162. * @param string $directory Directory to check for.
  163. *
  164. * @return string The realpath of given directory.
  165. * @throws \Xinc\Packager\Exception
  166. */
  167. protected function checkDirectory($directory)
  168. {
  169. if (!is_dir($directory)) {
  170. throw new \Xinc\Packager\Exception('Directory does not exists or no permissions: ' . $directory);
  171. } elseif (!is_readable($directory)) {
  172. throw new \Xinc\Packager\Exception('Can\'t read directory: ' .$directory);
  173. }
  174. $directory = realpath($directory);
  175. if ($directory === false) {
  176. throw new \Xinc\Packager\Exception('No permissions for directory: ' . $directory);
  177. }
  178. return realpath($directory);
  179. }
  180. /**
  181. * Prints help message, describing different parameters to run packager.
  182. *
  183. * @return void
  184. */
  185. protected function showHelp()
  186. {
  187. echo 'Usage: xinc-packager [switches]' . "\n\n";
  188. echo ' -p --package-dir=<dir> The directory to the package configuration.' . "\n"
  189. . ' -c --composer-bin-dir=<dir> The directory to composer executable.' . "\n"
  190. . ' -j --composer-json-dir=<dir> The directory to composer executable.' . "\n"
  191. . ' -i --install=<package> The package to install.' . "\n"
  192. . ' -u --uninstall=<package> The package to remove.' . "\n"
  193. . ' -a --activate=<package> The package to activate.' . "\n"
  194. . ' -d --deactivate=<package> The package to deactivate.' . "\n"
  195. . ' --version Prints the version of Xinc.' . "\n"
  196. . ' -h --help Prints this help message.' . "\n";
  197. }
  198. /**
  199. * Executes the given parameter on the Manager.
  200. *
  201. * @return void
  202. */
  203. protected function executeManagement()
  204. {
  205. $manager = new Manager(
  206. $this->options['composer-bin-dir'],
  207. $this->options['composer-json-dir'],
  208. $this->options['package-dir']
  209. );
  210. if (isset($this->options['install'])) {
  211. $manager->install($this->options['install']);
  212. }
  213. if (isset($this->options['deinstall'])) {
  214. $manager->deinstall($this->options['deinstall']);
  215. }
  216. if (isset($this->options['activate'])) {
  217. $manager->activate($this->options['activate']);
  218. }
  219. if (isset($this->options['deactivate'])) {
  220. $manager->deactivate($this->options['deactivate']);
  221. }
  222. }
  223. /**
  224. * Execution of this cli part. Outputs error message and exits with status code 1.
  225. *
  226. * @return void
  227. */
  228. public static function execute()
  229. {
  230. try {
  231. $cli = new self();
  232. $cli->parseOptions();
  233. $cli->validateFileDirectoryOptions();
  234. $cli->validateManagementOptions();
  235. $cli->executeManagement();
  236. } catch (\Exception $e) {
  237. echo $e->getMessage() . "\n";
  238. exit(1);
  239. }
  240. }
  241. }