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