1 | <?php |
||
15 | class Find extends Command |
||
16 | { |
||
17 | /** |
||
18 | * The console command name. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $name = 'find'; |
||
23 | |||
24 | /** |
||
25 | * The console command description. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $description = 'search for files in a directory hierarchy'; |
||
30 | |||
31 | /** |
||
32 | * $finder. |
||
33 | * |
||
34 | * @var Finder |
||
35 | */ |
||
36 | protected $finder; |
||
37 | |||
38 | /** |
||
39 | * $files. |
||
40 | * |
||
41 | * @var Filesystem |
||
42 | */ |
||
43 | protected $files; |
||
44 | |||
45 | /** |
||
46 | * __construct. |
||
47 | * |
||
48 | * @param Finder $finder |
||
49 | * @param Filesystem $files |
||
50 | 8 | */ |
|
51 | public function __construct(Finder $finder, Filesystem $files) |
||
52 | 8 | { |
|
53 | parent::__construct(); |
||
54 | 8 | ||
55 | 8 | $this->finder = $finder; |
|
56 | 8 | $this->files = $files; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * run. |
||
61 | * |
||
62 | * @param InputInterface $input |
||
63 | * @param OutputInterface $output |
||
64 | * @return int |
||
65 | 1 | */ |
|
66 | public function run(InputInterface $input, OutputInterface $output) |
||
67 | 1 | { |
|
68 | 1 | $command = (string) $input; |
|
69 | 1 | $command = strtr($command, [ |
|
70 | ' -name' => ' -N', |
||
71 | ' -type' => ' -T', |
||
72 | ' -maxdepth' => ' -M', |
||
73 | ' -delete' => ' -d true', |
||
74 | ]); |
||
75 | 1 | ||
76 | return parent::run(new StringInput($command), $output); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Handle the command. |
||
81 | * |
||
82 | * @throws InvalidArgumentException |
||
83 | 7 | */ |
|
84 | public function handle() |
||
85 | 7 | { |
|
86 | 7 | $path = $this->argument('path'); |
|
87 | 7 | $name = $this->option('name'); |
|
88 | 7 | $type = $this->option('type'); |
|
89 | 7 | $maxDepth = $this->option('maxdepth'); |
|
90 | $delete = filter_var($this->option('delete'), FILTER_VALIDATE_BOOLEAN); |
||
91 | 7 | ||
92 | 7 | $root = function_exists('base_path') === true ? base_path() : getcwd(); |
|
93 | $path = rtrim($root, '/').'/'.$path; |
||
94 | 7 | ||
95 | $this->finder->in($path); |
||
96 | 7 | ||
97 | 7 | if ($name !== null) { |
|
98 | $this->finder->name($name); |
||
99 | } |
||
100 | 7 | ||
101 | 7 | switch ($type) { |
|
102 | 1 | case 'd': |
|
103 | 1 | $this->finder->directories(); |
|
104 | 6 | break; |
|
105 | 1 | case 'f': |
|
106 | 1 | $this->finder->files(); |
|
107 | break; |
||
108 | } |
||
109 | 7 | ||
110 | 2 | if (($maxDepth) !== null) { |
|
111 | 1 | if ((int) $maxDepth === 0) { |
|
112 | $this->line($path); |
||
113 | 1 | ||
114 | return; |
||
115 | 1 | } |
|
116 | $this->finder->depth('<'.$maxDepth); |
||
117 | } |
||
118 | 6 | ||
119 | 6 | foreach ($this->finder->getIterator() as $file) { |
|
120 | 6 | $pathname = $file->getPathname(); |
|
121 | 2 | if ($delete === true && $this->files->exists($pathname) === true) { |
|
122 | try { |
||
123 | 2 | if ($this->files->isDirectory($pathname) === true) { |
|
124 | 1 | $removed = $this->files->deleteDirectory($pathname); |
|
125 | } else { |
||
126 | 1 | $removed = $this->files->delete($pathname); |
|
127 | } |
||
128 | 1 | } catch (Exception $e) { |
|
129 | 1 | $removed = false; |
|
130 | } |
||
131 | 2 | $removed === true ? $this->info('removed '.$pathname) : $this->error('removed '.$pathname.' fail'); |
|
132 | } else { |
||
133 | 4 | $this->line($pathname); |
|
134 | } |
||
135 | } |
||
136 | 6 | } |
|
137 | |||
138 | /** |
||
139 | * Get the console command arguments. |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | 8 | protected function getArguments() |
|
149 | |||
150 | /** |
||
151 | * Get the console command options. |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | 8 | protected function getOptions() |
|
164 | } |
||
165 |