1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\Finder\Adapter;
13:
14: use Symfony\Component\Finder\Shell\Shell;
15: use Symfony\Component\Finder\Shell\Command;
16: use Symfony\Component\Finder\Iterator\SortableIterator;
17: use Symfony\Component\Finder\Expression\Expression;
18:
19: 20: 21: 22: 23:
24: class BsdFindAdapter extends AbstractFindAdapter
25: {
26: 27: 28:
29: public function getName()
30: {
31: return 'bsd_find';
32: }
33:
34: 35: 36:
37: protected function canBeUsed()
38: {
39: return in_array($this->shell->getType(), array(Shell::TYPE_BSD, Shell::TYPE_DARWIN)) && parent::canBeUsed();
40: }
41:
42: 43: 44:
45: protected function buildFormatSorting(Command $command, $sort)
46: {
47: switch ($sort) {
48: case SortableIterator::SORT_BY_NAME:
49: $command->ins('sort')->add('| sort');
50:
51: return;
52: case SortableIterator::SORT_BY_TYPE:
53: $format = '%HT';
54: break;
55: case SortableIterator::SORT_BY_ACCESSED_TIME:
56: $format = '%a';
57: break;
58: case SortableIterator::SORT_BY_CHANGED_TIME:
59: $format = '%c';
60: break;
61: case SortableIterator::SORT_BY_MODIFIED_TIME:
62: $format = '%m';
63: break;
64: default:
65: throw new \InvalidArgumentException('Unknown sort options: '.$sort.'.');
66: }
67:
68: $command
69: ->add('-print0 | xargs -0 stat -f')
70: ->arg($format.'%t%N')
71: ->add('| sort | cut -f 2');
72: }
73:
74: 75: 76:
77: protected function buildFindCommand(Command $command, $dir)
78: {
79: parent::buildFindCommand($command, $dir)->addAtIndex('-E', 1);
80:
81: return $command;
82: }
83:
84: 85: 86:
87: protected function buildContentFiltering(Command $command, array $contains, $not = false)
88: {
89: foreach ($contains as $contain) {
90: $expr = Expression::create($contain);
91:
92:
93: $command
94: ->add('| grep -v \'^$\'')
95: ->add('| xargs -I{} grep -I')
96: ->add($expr->isCaseSensitive() ? null : '-i')
97: ->add($not ? '-L' : '-l')
98: ->add('-Ee')->arg($expr->renderPattern())
99: ->add('{}')
100: ;
101: }
102: }
103: }
104: