2018-03-17 13:55:39 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
|
|
|
use Symfony\Component\ClassLoader\ClassMapGenerator;
|
2018-03-18 07:07:13 +08:00
|
|
|
use Log;
|
2018-03-17 13:55:39 +08:00
|
|
|
|
|
|
|
class ClassLoader
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param $path
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getClassesInPath($path)
|
|
|
|
{
|
|
|
|
if (!file_exists($path)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-03-18 07:07:42 +08:00
|
|
|
$classes = [];
|
2018-03-17 13:55:39 +08:00
|
|
|
$all_classes = array_keys(ClassMapGenerator::createMap($path));
|
|
|
|
foreach ($all_classes as $cl) {
|
2018-03-18 07:07:13 +08:00
|
|
|
try {
|
|
|
|
$klass = new $cl;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error('Error loading class: ' . $e->getMessage());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-17 13:55:39 +08:00
|
|
|
$classes[] = $klass;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
}
|