* Get all models from the application * Based on the code by @mohammad425 * https://gist.github.com/mohammad425/231242958edb640601108bdea7bcf9ac * @return array */
()
| 273 | * @return array |
| 274 | */ |
| 275 | public function get(): array { |
| 276 | $composerPath = $this->projectPath . DIRECTORY_SEPARATOR . 'composer.json'; |
| 277 | |
| 278 | $composerData = json_decode( |
| 279 | file_get_contents($composerPath), |
| 280 | true |
| 281 | ); |
| 282 | |
| 283 | $models = []; |
| 284 | |
| 285 | foreach ((array) data_get($composerData, 'autoload.psr-4') as $namespace => $path) { |
| 286 | $finalPath = $this->projectPath . DIRECTORY_SEPARATOR . $path; |
| 287 | |
| 288 | $models = array_merge(collect(File::allFiles($finalPath)) |
| 289 | ->map(function ($item) use ($namespace) { |
| 290 | $path = $item->getRelativePathName(); |
| 291 | |
| 292 | $class = sprintf('%s%s', |
| 293 | $namespace, |
| 294 | strtr(substr($path, 0, strrpos($path, '.')), '/', '\\') |
| 295 | ); |
| 296 | |
| 297 | return [ |
| 298 | 'class' => $class, |
| 299 | 'path' => $path, |
| 300 | 'fullPath' => $item->getPathname(), |
| 301 | 'fileName' => $item->getFilename(), |
| 302 | 'fileContent' => file_get_contents($item->getPathname()), |
| 303 | ]; |
| 304 | }) |
| 305 | ->filter(function ($classData) { |
| 306 | $valid = false; |
| 307 | |
| 308 | if (class_exists($classData['class'])) { |
| 309 | $reflection = new \ReflectionClass($classData['class']); |
| 310 | |
| 311 | $isModel = $reflection->isSubclassOf(\Illuminate\Database\Eloquent\Model::class); |
| 312 | $isAbstract = $reflection->isAbstract(); |
| 313 | |
| 314 | $valid = $isModel && !$isAbstract; |
| 315 | } |
| 316 | |
| 317 | return $valid; |
| 318 | }) |
| 319 | ->values() |
| 320 | ->toArray(), $models); |
| 321 | } |
| 322 | |
| 323 | return $models; |
| 324 | } |
| 325 | } |