BsdFindAdapter.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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__.'\BsdFindAdapter 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 BSD 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 BsdFindAdapter extends AbstractFindAdapter
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getName()
  29. {
  30. return 'bsd_find';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function canBeUsed()
  36. {
  37. return \in_array($this->shell->getType(), array(Shell::TYPE_BSD, Shell::TYPE_DARWIN)) && parent::canBeUsed();
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function buildFormatSorting(Command $command, $sort)
  43. {
  44. switch ($sort) {
  45. case SortableIterator::SORT_BY_NAME:
  46. $command->ins('sort')->add('| sort');
  47. return;
  48. case SortableIterator::SORT_BY_TYPE:
  49. $format = '%HT';
  50. break;
  51. case SortableIterator::SORT_BY_ACCESSED_TIME:
  52. $format = '%a';
  53. break;
  54. case SortableIterator::SORT_BY_CHANGED_TIME:
  55. $format = '%c';
  56. break;
  57. case SortableIterator::SORT_BY_MODIFIED_TIME:
  58. $format = '%m';
  59. break;
  60. default:
  61. throw new \InvalidArgumentException(sprintf('Unknown sort options: %s.', $sort));
  62. }
  63. $command
  64. ->add('-print0 | xargs -0 stat -f')
  65. ->arg($format.'%t%N')
  66. ->add('| sort | cut -f 2');
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function buildFindCommand(Command $command, $dir)
  72. {
  73. parent::buildFindCommand($command, $dir)->addAtIndex('-E', 1);
  74. return $command;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function buildContentFiltering(Command $command, array $contains, $not = false)
  80. {
  81. foreach ($contains as $contain) {
  82. $expr = Expression::create($contain);
  83. // todo: avoid forking process for each $pattern by using multiple -e options
  84. $command
  85. ->add('| grep -v \'^$\'')
  86. ->add('| xargs -I{} grep -I')
  87. ->add($expr->isCaseSensitive() ? null : '-i')
  88. ->add($not ? '-L' : '-l')
  89. ->add('-Ee')->arg($expr->renderPattern())
  90. ->add('{}')
  91. ;
  92. }
  93. }
  94. }