1 | <?php |
||
14 | class Find extends Command |
||
15 | { |
||
16 | /** |
||
17 | * The console command name. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $name = 'find'; |
||
22 | |||
23 | /** |
||
24 | * The console command description. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $description = 'search for files in a directory hierarchy'; |
||
29 | |||
30 | /** |
||
31 | * $finder. |
||
32 | * |
||
33 | * @var \Symfony\Component\Finder\Finder |
||
34 | */ |
||
35 | protected $finder; |
||
36 | |||
37 | /** |
||
38 | * $files. |
||
39 | * |
||
40 | * @var \Illuminate\Filesystem\Filesystem |
||
41 | */ |
||
42 | protected $files; |
||
43 | |||
44 | /** |
||
45 | * __construct. |
||
46 | * |
||
47 | * @param \Symfony\Component\Finder\Finder $finder |
||
48 | * @param \Illuminate\Filesystem\Filesystem $files |
||
49 | */ |
||
50 | 8 | public function __construct(Finder $finder, Filesystem $files) |
|
57 | |||
58 | /** |
||
59 | * run. |
||
60 | * |
||
61 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
62 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
63 | * @return int |
||
64 | */ |
||
65 | 1 | public function run(InputInterface $input, OutputInterface $output) |
|
77 | |||
78 | /** |
||
79 | * Handle the command. |
||
80 | * |
||
81 | * @throws \InvalidArgumentException |
||
82 | */ |
||
83 | 7 | public function handle() |
|
84 | { |
||
85 | 7 | $path = $this->argument('path'); |
|
86 | 7 | $name = $this->option('name'); |
|
87 | 7 | $type = $this->option('type'); |
|
88 | 7 | $maxDepth = $this->option('maxdepth'); |
|
89 | 7 | $delete = filter_var($this->option('delete'), FILTER_VALIDATE_BOOLEAN); |
|
90 | |||
91 | 7 | $root = function_exists('base_path') === true ? base_path() : getcwd(); |
|
92 | 7 | $path = rtrim($root, '/').'/'.$path; |
|
93 | |||
94 | 7 | $this->finder->in($path); |
|
95 | |||
96 | 7 | if (is_null($name) === false) { |
|
97 | 7 | $this->finder->name($name); |
|
98 | } |
||
99 | |||
100 | 7 | switch ($type) { |
|
101 | 7 | case 'd': |
|
102 | 1 | $this->finder->directories(); |
|
103 | 1 | break; |
|
104 | 6 | case 'f': |
|
105 | 1 | $this->finder->files(); |
|
106 | 1 | break; |
|
107 | } |
||
108 | |||
109 | 7 | if (is_null($maxDepth) === false) { |
|
110 | 2 | if ($maxDepth == '0') { |
|
111 | 1 | $this->line($path); |
|
112 | |||
113 | 1 | return; |
|
114 | } |
||
115 | 1 | $this->finder->depth('<'.$maxDepth); |
|
116 | } |
||
117 | |||
118 | 6 | foreach ($this->finder->getIterator() as $file) { |
|
119 | 6 | $realPath = $file->getRealpath(); |
|
120 | 6 | if ($delete === true && $this->files->exists($realPath) === true) { |
|
121 | 2 | $removed = false; |
|
122 | try { |
||
123 | 2 | if ($this->files->isDirectory($realPath) === true) { |
|
124 | 1 | $removed = $this->files->deleteDirectory($realPath); |
|
125 | } else { |
||
126 | 1 | $removed = $this->files->delete($realPath); |
|
127 | } |
||
128 | 1 | } catch (Exception $e) { |
|
129 | 1 | $removed = false; |
|
130 | } |
||
131 | 2 | $removed === true ? $this->info('removed '.$realPath) : $this->error('removed '.$realPath.' fail'); |
|
132 | } else { |
||
133 | 4 | $this->line($realPath); |
|
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 |