PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/symfony/finder/Symfony/Component/Finder/Adapter/AbstractFindAdapter.php

https://gitlab.com/oytunistrator/92five
PHP | 328 lines | 204 code | 48 blank | 76 comment | 34 complexity | 613f29e4a26a79e5a5db3954c748e187 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Finder\Adapter;
  11. use Symfony\Component\Finder\Exception\AccessDeniedException;
  12. use Symfony\Component\Finder\Iterator;
  13. use Symfony\Component\Finder\Shell\Shell;
  14. use Symfony\Component\Finder\Expression\Expression;
  15. use Symfony\Component\Finder\Shell\Command;
  16. use Symfony\Component\Finder\Iterator\SortableIterator;
  17. use Symfony\Component\Finder\Comparator\NumberComparator;
  18. use Symfony\Component\Finder\Comparator\DateComparator;
  19. /**
  20. * Shell engine implementation using GNU find command.
  21. *
  22. * @author Jean-François Simon <contact@jfsimon.fr>
  23. */
  24. abstract class AbstractFindAdapter extends AbstractAdapter
  25. {
  26. /**
  27. * @var Shell
  28. */
  29. protected $shell;
  30. /**
  31. * Constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->shell = new Shell();
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function searchInDirectory($dir)
  41. {
  42. // having "/../" in path make find fail
  43. $dir = realpath($dir);
  44. // searching directories containing or not containing strings leads to no result
  45. if (Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode && ($this->contains || $this->notContains)) {
  46. return new Iterator\FilePathsIterator(array(), $dir);
  47. }
  48. $command = Command::create();
  49. $find = $this->buildFindCommand($command, $dir);
  50. if ($this->followLinks) {
  51. $find->add('-follow');
  52. }
  53. $find->add('-mindepth')->add($this->minDepth + 1);
  54. if (PHP_INT_MAX !== $this->maxDepth) {
  55. $find->add('-maxdepth')->add($this->maxDepth + 1);
  56. }
  57. if (Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode) {
  58. $find->add('-type d');
  59. } elseif (Iterator\FileTypeFilterIterator::ONLY_FILES === $this->mode) {
  60. $find->add('-type f');
  61. }
  62. $this->buildNamesFiltering($find, $this->names);
  63. $this->buildNamesFiltering($find, $this->notNames, true);
  64. $this->buildPathsFiltering($find, $dir, $this->paths);
  65. $this->buildPathsFiltering($find, $dir, $this->notPaths, true);
  66. $this->buildSizesFiltering($find, $this->sizes);
  67. $this->buildDatesFiltering($find, $this->dates);
  68. $useGrep = $this->shell->testCommand('grep') && $this->shell->testCommand('xargs');
  69. $useSort = is_int($this->sort) && $this->shell->testCommand('sort') && $this->shell->testCommand('cut');
  70. if ($useGrep && ($this->contains || $this->notContains)) {
  71. $grep = $command->ins('grep');
  72. $this->buildContentFiltering($grep, $this->contains);
  73. $this->buildContentFiltering($grep, $this->notContains, true);
  74. }
  75. if ($useSort) {
  76. $this->buildSorting($command, $this->sort);
  77. }
  78. $command->setErrorHandler(
  79. $this->ignoreUnreadableDirs
  80. // If directory is unreadable and finder is set to ignore it, `stderr` is ignored.
  81. ? function ($stderr) { return; }
  82. : function ($stderr) { throw new AccessDeniedException($stderr); }
  83. );
  84. $paths = $this->shell->testCommand('uniq') ? $command->add('| uniq')->execute() : array_unique($command->execute());
  85. $iterator = new Iterator\FilePathsIterator($paths, $dir);
  86. if ($this->exclude) {
  87. $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
  88. }
  89. if (!$useGrep && ($this->contains || $this->notContains)) {
  90. $iterator = new Iterator\FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
  91. }
  92. if ($this->filters) {
  93. $iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
  94. }
  95. if (!$useSort && $this->sort) {
  96. $iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort);
  97. $iterator = $iteratorAggregate->getIterator();
  98. }
  99. return $iterator;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. protected function canBeUsed()
  105. {
  106. return $this->shell->testCommand('find');
  107. }
  108. /**
  109. * @param Command $command
  110. * @param string $dir
  111. *
  112. * @return Command
  113. */
  114. protected function buildFindCommand(Command $command, $dir)
  115. {
  116. return $command
  117. ->ins('find')
  118. ->add('find ')
  119. ->arg($dir)
  120. ->add('-noleaf'); // the -noleaf option is required for filesystems that don't follow the '.' and '..' conventions
  121. }
  122. /**
  123. * @param Command $command
  124. * @param string[] $names
  125. * @param bool $not
  126. */
  127. private function buildNamesFiltering(Command $command, array $names, $not = false)
  128. {
  129. if (0 === count($names)) {
  130. return;
  131. }
  132. $command->add($not ? '-not' : null)->cmd('(');
  133. foreach ($names as $i => $name) {
  134. $expr = Expression::create($name);
  135. // Find does not support expandable globs ("*.{a,b}" syntax).
  136. if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
  137. $expr = Expression::create($expr->getGlob()->toRegex(false));
  138. }
  139. // Fixes 'not search' and 'full path matching' regex problems.
  140. // - Jokers '.' are replaced by [^/].
  141. // - We add '[^/]*' before and after regex (if no ^|$ flags are present).
  142. if ($expr->isRegex()) {
  143. $regex = $expr->getRegex();
  144. $regex->prepend($regex->hasStartFlag() ? '/' : '/[^/]*')
  145. ->setStartFlag(false)
  146. ->setStartJoker(true)
  147. ->replaceJokers('[^/]');
  148. if (!$regex->hasEndFlag() || $regex->hasEndJoker()) {
  149. $regex->setEndJoker(false)->append('[^/]*');
  150. }
  151. }
  152. $command
  153. ->add($i > 0 ? '-or' : null)
  154. ->add($expr->isRegex()
  155. ? ($expr->isCaseSensitive() ? '-regex' : '-iregex')
  156. : ($expr->isCaseSensitive() ? '-name' : '-iname')
  157. )
  158. ->arg($expr->renderPattern());
  159. }
  160. $command->cmd(')');
  161. }
  162. /**
  163. * @param Command $command
  164. * @param string $dir
  165. * @param string[] $paths
  166. * @param bool $not
  167. */
  168. private function buildPathsFiltering(Command $command, $dir, array $paths, $not = false)
  169. {
  170. if (0 === count($paths)) {
  171. return;
  172. }
  173. $command->add($not ? '-not' : null)->cmd('(');
  174. foreach ($paths as $i => $path) {
  175. $expr = Expression::create($path);
  176. // Find does not support expandable globs ("*.{a,b}" syntax).
  177. if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
  178. $expr = Expression::create($expr->getGlob()->toRegex(false));
  179. }
  180. // Fixes 'not search' regex problems.
  181. if ($expr->isRegex()) {
  182. $regex = $expr->getRegex();
  183. $regex->prepend($regex->hasStartFlag() ? $dir.DIRECTORY_SEPARATOR : '.*')->setEndJoker(!$regex->hasEndFlag());
  184. } else {
  185. $expr->prepend('*')->append('*');
  186. }
  187. $command
  188. ->add($i > 0 ? '-or' : null)
  189. ->add($expr->isRegex()
  190. ? ($expr->isCaseSensitive() ? '-regex' : '-iregex')
  191. : ($expr->isCaseSensitive() ? '-path' : '-ipath')
  192. )
  193. ->arg($expr->renderPattern());
  194. }
  195. $command->cmd(')');
  196. }
  197. /**
  198. * @param Command $command
  199. * @param NumberComparator[] $sizes
  200. */
  201. private function buildSizesFiltering(Command $command, array $sizes)
  202. {
  203. foreach ($sizes as $i => $size) {
  204. $command->add($i > 0 ? '-and' : null);
  205. switch ($size->getOperator()) {
  206. case '<=':
  207. $command->add('-size -'.($size->getTarget() + 1).'c');
  208. break;
  209. case '>=':
  210. $command->add('-size +'. ($size->getTarget() - 1).'c');
  211. break;
  212. case '>':
  213. $command->add('-size +'.$size->getTarget().'c');
  214. break;
  215. case '!=':
  216. $command->add('-size -'.$size->getTarget().'c');
  217. $command->add('-size +'.$size->getTarget().'c');
  218. break;
  219. case '<':
  220. default:
  221. $command->add('-size -'.$size->getTarget().'c');
  222. }
  223. }
  224. }
  225. /**
  226. * @param Command $command
  227. * @param DateComparator[] $dates
  228. */
  229. private function buildDatesFiltering(Command $command, array $dates)
  230. {
  231. foreach ($dates as $i => $date) {
  232. $command->add($i > 0 ? '-and' : null);
  233. $mins = (int) round((time()-$date->getTarget()) / 60);
  234. if (0 > $mins) {
  235. // mtime is in the future
  236. $command->add(' -mmin -0');
  237. // we will have no result so we don't need to continue
  238. return;
  239. }
  240. switch ($date->getOperator()) {
  241. case '<=':
  242. $command->add('-mmin +'.($mins - 1));
  243. break;
  244. case '>=':
  245. $command->add('-mmin -'.($mins + 1));
  246. break;
  247. case '>':
  248. $command->add('-mmin -'.$mins);
  249. break;
  250. case '!=':
  251. $command->add('-mmin +'.$mins.' -or -mmin -'.$mins);
  252. break;
  253. case '<':
  254. default:
  255. $command->add('-mmin +'.$mins);
  256. }
  257. }
  258. }
  259. /**
  260. * @param Command $command
  261. * @param string $sort
  262. *
  263. * @throws \InvalidArgumentException
  264. */
  265. private function buildSorting(Command $command, $sort)
  266. {
  267. $this->buildFormatSorting($command, $sort);
  268. }
  269. /**
  270. * @param Command $command
  271. * @param string $sort
  272. */
  273. abstract protected function buildFormatSorting(Command $command, $sort);
  274. /**
  275. * @param Command $command
  276. * @param array $contains
  277. * @param bool $not
  278. */
  279. abstract protected function buildContentFiltering(Command $command, array $contains, $not = false);
  280. }