ConfigurationResolver.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS;
  11. use Symfony\CS\ConfigurationException\InvalidConfigurationException;
  12. /**
  13. * The resolver that resolves configuration to use by command line options and config.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. * @author Katsuhiro Ogawa <ko.fivestar@gmail.com>
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. */
  19. class ConfigurationResolver
  20. {
  21. /**
  22. * @var FixerInterface[]
  23. */
  24. protected $allFixers;
  25. /**
  26. * @var ConfigInterface
  27. */
  28. protected $config;
  29. /**
  30. * @var FixerInterface[]
  31. */
  32. protected $fixers = array();
  33. /**
  34. * @var array
  35. */
  36. protected $options = array(
  37. 'fixers' => null,
  38. 'level' => null,
  39. 'progress' => null,
  40. );
  41. /**
  42. * @var string
  43. */
  44. private $format;
  45. public function setAllFixers(array $allFixers)
  46. {
  47. $this->allFixers = $allFixers;
  48. return $this;
  49. }
  50. public function setConfig(ConfigInterface $config)
  51. {
  52. $this->config = $config;
  53. return $this;
  54. }
  55. public function setOption($name, $value)
  56. {
  57. $this->options[$name] = $value;
  58. return $this;
  59. }
  60. public function setOptions(array $options)
  61. {
  62. foreach ($options as $name => $value) {
  63. $this->setOption($name, $value);
  64. }
  65. return $this;
  66. }
  67. /**
  68. * Resolves fixers.
  69. *
  70. * @return ConfigurationResolver
  71. */
  72. public function resolve()
  73. {
  74. $this->resolveByLevel();
  75. $this->resolveByNames();
  76. $this->resolveFormat();
  77. return $this;
  78. }
  79. /**
  80. * Returns fixers.
  81. *
  82. * @return FixerInterface[] An array of FixerInterface
  83. */
  84. public function getFixers()
  85. {
  86. return $this->fixers;
  87. }
  88. /**
  89. * Returns output format.
  90. *
  91. * @return string
  92. */
  93. public function getFormat()
  94. {
  95. return $this->format;
  96. }
  97. public function getProgress()
  98. {
  99. // TODO: following condition should be removed on 2.0 line
  100. // and method should be added to ConfigInterface
  101. if (!method_exists($this->config, 'getHideProgress')) {
  102. return $this->options['progress'];
  103. }
  104. return $this->options['progress'] && !$this->config->getHideProgress();
  105. }
  106. protected function resolveByLevel()
  107. {
  108. $level = $this->parseLevel();
  109. if (null === $level) {
  110. return;
  111. }
  112. $fixers = array();
  113. foreach ($this->allFixers as $fixer) {
  114. if ($fixer->getLevel() === ($fixer->getLevel() & $level)) {
  115. $fixers[] = $fixer;
  116. }
  117. }
  118. $this->fixers = $fixers;
  119. }
  120. protected function resolveByNames()
  121. {
  122. $names = $this->parseFixers();
  123. if (null === $names) {
  124. return;
  125. }
  126. $addNames = array();
  127. $removeNames = array();
  128. foreach ($names as $name) {
  129. if (0 === strpos($name, '-')) {
  130. $removeNames[ltrim($name, '-')] = true;
  131. } else {
  132. $addNames[$name] = true;
  133. }
  134. }
  135. foreach ($this->fixers as $key => $fixer) {
  136. if (isset($removeNames[$fixer->getName()])) {
  137. unset($this->fixers[$key]);
  138. }
  139. }
  140. foreach ($this->allFixers as $fixer) {
  141. if (isset($addNames[$fixer->getName()]) && !in_array($fixer, $this->fixers, true)) {
  142. $this->fixers[] = $fixer;
  143. }
  144. }
  145. }
  146. protected function resolveFormat()
  147. {
  148. if (array_key_exists('format', $this->options)) {
  149. $format = $this->options['format'];
  150. } elseif (method_exists($this->config, 'getFormat')) {
  151. $format = $this->config->getFormat();
  152. } else {
  153. $format = 'txt'; // default
  154. }
  155. static $formats = array('txt', 'xml', 'json');
  156. if (!in_array($format, $formats, true)) {
  157. throw new InvalidConfigurationException(sprintf('The format "%s" is not defined, supported are %s.', $format, implode(', ', $formats)));
  158. }
  159. $this->format = $format;
  160. }
  161. protected function parseLevel()
  162. {
  163. static $levelMap = array(
  164. 'none' => FixerInterface::NONE_LEVEL,
  165. 'psr0' => FixerInterface::PSR0_LEVEL,
  166. 'psr1' => FixerInterface::PSR1_LEVEL,
  167. 'psr2' => FixerInterface::PSR2_LEVEL,
  168. 'symfony' => FixerInterface::SYMFONY_LEVEL,
  169. );
  170. $levelOption = $this->options['level'];
  171. if (null !== $levelOption) {
  172. if (!isset($levelMap[$levelOption])) {
  173. throw new InvalidConfigurationException(sprintf('The level "%s" is not defined.', $levelOption));
  174. }
  175. return $levelMap[$levelOption];
  176. }
  177. if (null === $this->options['fixers']) {
  178. return $this->config->getLevel();
  179. }
  180. foreach ($this->parseFixers() as $fixer) {
  181. if (0 === strpos($fixer, '-')) {
  182. return $this->config->getLevel();
  183. }
  184. }
  185. }
  186. protected function parseFixers()
  187. {
  188. if (null !== $this->options['fixers']) {
  189. return array_map('trim', explode(',', $this->options['fixers']));
  190. }
  191. if (null === $this->options['level']) {
  192. return $this->config->getFixers();
  193. }
  194. }
  195. }