GnuFindAdapter.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. @trigger_error('The '.__NAMESPACE__.'\GnuFindAdapter class is deprecated since Symfony 2.8 and will be removed in 3.0. Use directly the Finder class instead.', E_USER_DEPRECATED);
  12. use Symfony\Component\Finder\Expression\Expression;
  13. use Symfony\Component\Finder\Iterator\SortableIterator;
  14. use Symfony\Component\Finder\Shell\Command;
  15. use Symfony\Component\Finder\Shell\Shell;
  16. /**
  17. * Shell engine implementation using GNU find command.
  18. *
  19. * @author Jean-François Simon <contact@jfsimon.fr>
  20. *
  21. * @deprecated since 2.8, to be removed in 3.0. Use Finder instead.
  22. */
  23. class GnuFindAdapter extends AbstractFindAdapter
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getName()
  29. {
  30. return 'gnu_find';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function buildFormatSorting(Command $command, $sort)
  36. {
  37. switch ($sort) {
  38. case SortableIterator::SORT_BY_NAME:
  39. $command->ins('sort')->add('| sort');
  40. return;
  41. case SortableIterator::SORT_BY_TYPE:
  42. $format = '%y';
  43. break;
  44. case SortableIterator::SORT_BY_ACCESSED_TIME:
  45. $format = '%A@';
  46. break;
  47. case SortableIterator::SORT_BY_CHANGED_TIME:
  48. $format = '%C@';
  49. break;
  50. case SortableIterator::SORT_BY_MODIFIED_TIME:
  51. $format = '%T@';
  52. break;
  53. default:
  54. throw new \InvalidArgumentException(sprintf('Unknown sort options: %s.', $sort));
  55. }
  56. $command
  57. ->get('find')
  58. ->add('-printf')
  59. ->arg($format.' %h/%f\\n')
  60. ->add('| sort | cut')
  61. ->arg('-d ')
  62. ->arg('-f2-')
  63. ;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function canBeUsed()
  69. {
  70. return Shell::TYPE_UNIX === $this->shell->getType() && parent::canBeUsed();
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function buildFindCommand(Command $command, $dir)
  76. {
  77. return parent::buildFindCommand($command, $dir)->add('-regextype posix-extended');
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. protected function buildContentFiltering(Command $command, array $contains, $not = false)
  83. {
  84. foreach ($contains as $contain) {
  85. $expr = Expression::create($contain);
  86. // todo: avoid forking process for each $pattern by using multiple -e options
  87. $command
  88. ->add('| xargs -I{} -r grep -I')
  89. ->add($expr->isCaseSensitive() ? null : '-i')
  90. ->add($not ? '-L' : '-l')
  91. ->add('-Ee')->arg($expr->renderPattern())
  92. ->add('{}')
  93. ;
  94. }
  95. }
  96. }