mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 11:48:08 +08:00
Add global import annotations mechanism
This commit is contained in:
parent
83fa73ab93
commit
7e800cbacf
@ -28,7 +28,7 @@ class ClassLoader
|
||||
$this->composerLoader = $classLoader;
|
||||
$config = ScanConfig::instance();
|
||||
|
||||
$scanner = new Scanner($this,$config->getIgnoreAnnotations());
|
||||
$scanner = new Scanner($this, $config->getIgnoreAnnotations(), $config->getGlobalImports());
|
||||
$classes = $scanner->scan($config->getPaths());
|
||||
$this->proxies = ProxyManager::init($classes);
|
||||
var_dump($this->proxies);
|
||||
|
@ -18,6 +18,11 @@ final class ScanConfig
|
||||
*/
|
||||
protected $ignoreAnnotations;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $globalImports;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
@ -28,11 +33,12 @@ final class ScanConfig
|
||||
*/
|
||||
protected static $instance;
|
||||
|
||||
public function __construct(array $paths = [], array $ignoreAnnotations = [], array $dependencies = [])
|
||||
public function __construct(array $paths = [], array $dependencies = [], array $ignoreAnnotations = [], array $globalImports = [])
|
||||
{
|
||||
$this->paths = $paths;
|
||||
$this->ignoreAnnotations = $ignoreAnnotations;
|
||||
$this->dependencies = $dependencies;
|
||||
$this->ignoreAnnotations = $ignoreAnnotations;
|
||||
$this->globalImports = $globalImports;
|
||||
}
|
||||
|
||||
public function getPaths(): array
|
||||
@ -45,6 +51,11 @@ final class ScanConfig
|
||||
return $this->ignoreAnnotations;
|
||||
}
|
||||
|
||||
public function getGlobalImports(): array
|
||||
{
|
||||
return $this->globalImports;
|
||||
}
|
||||
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return $this->dependencies;
|
||||
@ -70,12 +81,14 @@ final class ScanConfig
|
||||
|
||||
$scanDirs = $configFromProviders['annotations']['scan']['paths'] ?? [];
|
||||
$ignoreAnnotations = $configFromProviders['annotations']['scan']['ignore_annotations'] ?? [];
|
||||
$globalImports = $configFromProviders['annotations']['scan']['global_imports'] ?? [];
|
||||
|
||||
// Load the config/autoload/annotations.php and merge the config
|
||||
if (file_exists($configDir . '/autoload/annotations.php')) {
|
||||
$annotations = include $configDir . '/autoload/annotations.php';
|
||||
$scanDirs = array_merge($scanDirs, $annotations['scan']['paths'] ?? []);
|
||||
$ignoreAnnotations = array_merge($ignoreAnnotations, $annotations['scan']['ignore_annotations'] ?? []);
|
||||
$globalImports = array_merge($globalImports, $annotations['scan']['global_imports'] ?? []);
|
||||
}
|
||||
|
||||
// Load the config/config.php and merge the config
|
||||
@ -84,9 +97,10 @@ final class ScanConfig
|
||||
if (isset($configContent['annotations'])) {
|
||||
$scanDirs = array_merge($scanDirs, $configContent['annotations']['scan']['paths'] ?? []);
|
||||
$ignoreAnnotations = array_merge($ignoreAnnotations, $configContent['annotations']['scan']['ignore_annotations'] ?? []);
|
||||
$globalImports = array_merge($globalImports, $configContent['annotations']['scan']['global_imports'] ?? []);
|
||||
}
|
||||
}
|
||||
|
||||
return self::$instance = new self($scanDirs, $ignoreAnnotations,$serverDependencies);
|
||||
return self::$instance = new self($scanDirs, $serverDependencies, $ignoreAnnotations, $globalImports);
|
||||
}
|
||||
}
|
||||
|
@ -27,23 +27,28 @@ class Scanner
|
||||
*/
|
||||
protected $classloader;
|
||||
|
||||
public function __construct(\Hyperf\Autoload\ClassLoader $classloader, array $ignoreAnnotations = [])
|
||||
public function __construct(\Hyperf\Autoload\ClassLoader $classloader, array $ignoreAnnotations = [], array $globalImports = [])
|
||||
{
|
||||
$this->classloader = $classloader;
|
||||
// AnnotationRegistry::registerLoader(function ($class) {
|
||||
// return class_exists($class, false);
|
||||
// });
|
||||
AnnotationRegistry::registerLoader(function ($class) {
|
||||
return class_exists($class, false);
|
||||
});
|
||||
|
||||
foreach ($ignoreAnnotations as $annotation) {
|
||||
AnnotationReader::addGlobalIgnoredName($annotation);
|
||||
}
|
||||
foreach ($globalImports as $alias => $annotation) {
|
||||
AnnotationReader::addGlobalImports($alias, $annotation);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function scan(array $paths): array
|
||||
{
|
||||
try {
|
||||
$classes = [];
|
||||
if (! $paths) {
|
||||
return [];
|
||||
return $classes;
|
||||
}
|
||||
$paths = $this->normalizeDir($paths);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user