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 \Symfony\Component\Finder\Finder |
||
35 | */ |
||
36 | protected $finder; |
||
37 | |||
38 | /** |
||
39 | * $files. |
||
40 | * |
||
41 | * @var \Illuminate\Filesystem\Filesystem |
||
42 | */ |
||
43 | protected $files; |
||
44 | |||
45 | /** |
||
46 | * __construct. |
||
47 | * |
||
48 | * @param \Symfony\Component\Finder\Finder $finder |
||
49 | * @param \Illuminate\Filesystem\Filesystem $files |
||
50 | */ |
||
51 | 8 | public function __construct(Finder $finder, Filesystem $files) |
|
52 | { |
||
53 | 8 | parent::__construct(); |
|
54 | |||
55 | 8 | $this->finder = $finder; |
|
56 | 8 | $this->files = $files; |
|
57 | 8 | } |
|
58 | |||
59 | /** |
||
60 | * run. |
||
61 | * |
||
62 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
63 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
64 | * @return int |
||
65 | */ |
||
66 | 1 | public function run(InputInterface $input, OutputInterface $output) |
|
67 | { |
||
68 | 1 | $command = (string) $input; |
|
69 | 1 | $command = strtr($command, [ |
|
70 | 1 | ' -name' => ' -N', |
|
71 | 1 | ' -type' => ' -T', |
|
72 | 1 | ' -maxdepth' => ' -M', |
|
73 | 1 | ' -delete' => ' -d true', |
|
74 | 1 | ]); |
|
75 | |||
76 | 1 | return parent::run(new StringInput($command), $output); |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Handle the command. |
||
81 | * |
||
82 | * @throws \InvalidArgumentException |
||
83 | */ |
||
84 | 7 | public function handle() |
|
85 | { |
||
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 | 7 | $delete = filter_var($this->option('delete'), FILTER_VALIDATE_BOOLEAN); |
|
91 | |||
92 | 7 | $root = function_exists('base_path') === true ? base_path() : getcwd(); |
|
93 | 7 | $path = rtrim($root, '/').'/'.$path; |
|
94 | |||
95 | 7 | $this->finder->in($path); |
|
96 | |||
97 | 7 | if ($name !== null) { |
|
98 | 7 | $this->finder->name($name); |
|
99 | 7 | } |
|
100 | |||
101 | switch ($type) { |
||
102 | 7 | case 'd': |
|
103 | 1 | $this->finder->directories(); |
|
104 | 1 | break; |
|
105 | 6 | case 'f': |
|
106 | 1 | $this->finder->files(); |
|
107 | 1 | break; |
|
108 | } |
||
109 | |||
110 | 7 | if (is_null($maxDepth) === false) { |
|
111 | 2 | if ($maxDepth == '0') { |
|
112 | 1 | $this->line($path); |
|
113 | |||
114 | 1 | return; |
|
115 | } |
||
116 | 1 | $this->finder->depth('<'.$maxDepth); |
|
117 | 1 | } |
|
118 | |||
119 | 6 | foreach ($this->finder->getIterator() as $file) { |
|
120 | 6 | $realPath = $file->getRealpath(); |
|
121 | 6 | if ($delete === true && $this->files->exists($realPath) === true) { |
|
122 | 2 | $removed = false; |
|
123 | try { |
||
124 | 2 | if ($this->files->isDirectory($realPath) === true) { |
|
125 | 1 | $removed = $this->files->deleteDirectory($realPath); |
|
126 | 1 | } else { |
|
127 | $removed = $this->files->delete($realPath); |
||
128 | } |
||
129 | 2 | } catch (Exception $e) { |
|
130 | 1 | $removed = false; |
|
131 | } |
||
132 | 2 | $removed === true ? $this->info('removed '.$realPath) : $this->error('removed '.$realPath.' fail'); |
|
133 | 2 | } else { |
|
134 | 4 | $this->line($realPath); |
|
135 | } |
||
136 | 6 | } |
|
137 | 6 | } |
|
138 | |||
139 | /** |
||
140 | * Get the console command arguments. |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | 8 | protected function getArguments() |
|
150 | |||
151 | /** |
||
152 | * Get the console command options. |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | 8 | protected function getOptions() |
|
165 | } |
||
166 |