InputValidator.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Console;
  7. use Magento\Setup\Console\Command\DeployStaticContentCommand;
  8. use Magento\Deploy\Console\DeployStaticOptions as Options;
  9. use Magento\Framework\Validator\Locale;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Magento\Framework\App\ObjectManager;
  12. use Magento\Framework\Validator\RegexFactory;
  13. /**
  14. * Command input arguments validator class
  15. */
  16. class InputValidator
  17. {
  18. /**
  19. * @var array
  20. */
  21. public static $fileExtensionOptionMap = [
  22. 'js' => Options::NO_JAVASCRIPT,
  23. 'map' => Options::NO_JAVASCRIPT,
  24. 'css' => Options::NO_CSS,
  25. 'less' => Options::NO_LESS,
  26. 'html' => Options::NO_HTML,
  27. 'htm' => Options::NO_HTML,
  28. 'jpg' => Options::NO_IMAGES,
  29. 'jpeg' => Options::NO_IMAGES,
  30. 'gif' => Options::NO_IMAGES,
  31. 'png' => Options::NO_IMAGES,
  32. 'ico' => Options::NO_IMAGES,
  33. 'svg' => Options::NO_IMAGES,
  34. 'eot' => Options::NO_FONTS,
  35. 'ttf' => Options::NO_FONTS,
  36. 'woff' => Options::NO_FONTS,
  37. 'woff2' => Options::NO_FONTS,
  38. 'md' => Options::NO_MISC,
  39. 'jbf' => Options::NO_MISC,
  40. 'csv' => Options::NO_MISC,
  41. 'json' => Options::NO_MISC,
  42. 'txt' => Options::NO_MISC,
  43. 'htc' => Options::NO_MISC,
  44. 'swf' => Options::NO_MISC,
  45. 'LICENSE' => Options::NO_MISC,
  46. '' => Options::NO_MISC,
  47. ];
  48. /**
  49. * Locale interface
  50. *
  51. * Used to check if specified locale codes are valid
  52. *
  53. * @var Locale
  54. */
  55. private $localeValidator;
  56. /**
  57. * @var RegexFactory
  58. */
  59. private $versionValidatorFactory;
  60. /**
  61. * InputValidator constructor
  62. *
  63. * @param Locale $localeValidator
  64. * @param RegexFactory $versionValidatorFactory
  65. */
  66. public function __construct(
  67. Locale $localeValidator,
  68. ?RegexFactory $versionValidatorFactory = null
  69. ) {
  70. $this->localeValidator = $localeValidator;
  71. $this->versionValidatorFactory = $versionValidatorFactory ?:
  72. ObjectManager::getInstance()->get(RegexFactory::class);
  73. }
  74. /**
  75. * Validate input options
  76. *
  77. * @param InputInterface $input
  78. * @return void
  79. */
  80. public function validate(InputInterface $input)
  81. {
  82. $this->checkAreasInput(
  83. $input->getOption(Options::AREA),
  84. $input->getOption(Options::EXCLUDE_AREA)
  85. );
  86. $this->checkThemesInput(
  87. $input->getOption(Options::THEME),
  88. $input->getOption(Options::EXCLUDE_THEME)
  89. );
  90. $this->checkLanguagesInput(
  91. $input->getArgument(Options::LANGUAGES_ARGUMENT) ?: ['all'],
  92. $input->getOption(Options::EXCLUDE_LANGUAGE)
  93. );
  94. $this->checkVersionInput(
  95. $input->getOption(Options::CONTENT_VERSION) ?: ''
  96. );
  97. }
  98. /**
  99. * Validate options related to areas
  100. *
  101. * @param array $areasInclude
  102. * @param array $areasExclude
  103. * @return void
  104. * @throws \InvalidArgumentException
  105. */
  106. private function checkAreasInput(array $areasInclude, array $areasExclude)
  107. {
  108. if ($areasInclude[0] != 'all' && $areasExclude[0] != 'none') {
  109. throw new \InvalidArgumentException(
  110. '--area (-a) and --exclude-area cannot be used at the same time'
  111. );
  112. }
  113. }
  114. /**
  115. * Validate options related to themes
  116. *
  117. * @param array $themesInclude
  118. * @param array $themesExclude
  119. * @return void
  120. * @throws \InvalidArgumentException
  121. */
  122. private function checkThemesInput(array $themesInclude, array $themesExclude)
  123. {
  124. if ($themesInclude[0] != 'all' && $themesExclude[0] != 'none') {
  125. throw new \InvalidArgumentException(
  126. '--theme (-t) and --exclude-theme cannot be used at the same time'
  127. );
  128. }
  129. }
  130. /**
  131. * Validate options related to locales
  132. *
  133. * @param array $languagesInclude
  134. * @param array $languagesExclude
  135. * @return void
  136. * @throws \InvalidArgumentException
  137. */
  138. private function checkLanguagesInput(array $languagesInclude, array $languagesExclude)
  139. {
  140. if ($languagesInclude[0] != 'all') {
  141. foreach ($languagesInclude as $lang) {
  142. if (!$this->localeValidator->isValid($lang)) {
  143. throw new \InvalidArgumentException(
  144. $lang .
  145. ' argument has invalid value, please run info:language:list for list of available locales'
  146. );
  147. }
  148. }
  149. if ($languagesExclude[0] != 'none') {
  150. throw new \InvalidArgumentException(
  151. '--language (-l) and --exclude-language cannot be used at the same time'
  152. );
  153. }
  154. }
  155. }
  156. /**
  157. * Version input checks
  158. *
  159. * @param string $contentVersion
  160. * @throws \InvalidArgumentException
  161. */
  162. private function checkVersionInput(string $contentVersion): void
  163. {
  164. if ($contentVersion) {
  165. $versionValidator = $this->versionValidatorFactory->create(
  166. [
  167. 'pattern' => '/^[A-Za-z0-9_.]+$/'
  168. ]
  169. );
  170. if (!$versionValidator->isValid($contentVersion)) {
  171. throw new \InvalidArgumentException(
  172. 'Argument "' .
  173. Options::CONTENT_VERSION
  174. . '" has invalid value, content version should contain only characters, digits and dots'
  175. );
  176. }
  177. }
  178. }
  179. }