This commit is contained in:
as2252258@163.com
2021-04-08 00:38:29 +08:00
parent c5dd60258d
commit 482581b97b
3 changed files with 330 additions and 235 deletions
+12 -2
View File
@@ -85,9 +85,19 @@ class Annotation extends Component
* @param string $dir * @param string $dir
* @throws Exception * @throws Exception
*/ */
public function instanceDirectoryFiles(string $dir) public function instanceDirectoryFiles(string $dir, ?string $outPath = null)
{ {
$this->_loader->loadByDirectory($dir); $this->_loader->loadByDirectory($dir, $outPath);
}
/**
* @param string $dir
* @throws Exception
*/
public function runtime(string $dir, ?string $outPath = null)
{
$this->_loader->directoryRuntime($dir, $outPath);
} }
+89 -10
View File
@@ -142,12 +142,9 @@ class Loader extends BaseObject
if ($path->getExtension() !== 'php') { if ($path->getExtension() !== 'php') {
continue; continue;
} }
if (!in_array($path->getRealPath(), $this->_directoryMap[$DIRECTORY])) {
$this->_directoryMap[$DIRECTORY][] = $path->getRealPath();
}
try { try {
$this->appendFileToDirectory($path->getRealPath());
$replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); $replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
if (empty($replace) || !$replace->isInstantiable()) { if (empty($replace) || !$replace->isInstantiable()) {
continue; continue;
@@ -208,10 +205,13 @@ class Loader extends BaseObject
* @param string $path * @param string $path
* @throws Exception * @throws Exception
*/ */
public function loadByDirectory(string $path) public function loadByDirectory(string $path, ?string $outPath = null)
{ {
try { try {
foreach ($this->_fileMap as $fileName => $className) { foreach ($this->_fileMap as $fileName => $className) {
if (str_starts_with($fileName, $outPath)) {
continue;
}
if (!str_starts_with($fileName, $path)) { if (!str_starts_with($fileName, $path)) {
continue; continue;
} }
@@ -268,15 +268,46 @@ class Loader extends BaseObject
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $directoryIterator->getRealPath()); $DIRECTORY = explode(DIRECTORY_SEPARATOR, $directoryIterator->getRealPath());
array_pop($DIRECTORY); array_pop($DIRECTORY);
$DIRECTORY = implode(DIRECTORY_SEPARATOR, $DIRECTORY); $path = DIRECTORY_SEPARATOR;
foreach ($DIRECTORY as $value) {
if (!isset($this->_directoryMap[$DIRECTORY])) { $path = $this->makeMoneyDirectoryArray($path, $value);
$this->_directoryMap[$DIRECTORY] = [];
} }
return $DIRECTORY; return $DIRECTORY;
} }
/**
* @param $path
* @param $value
* @return string
*/
private function makeMoneyDirectoryArray($path, $value)
{
$path .= $value . DIRECTORY_SEPARATOR;
if (!isset($this->_directoryMap[$path])) {
$this->_directoryMap[$path] = [];
}
return $path;
}
/**
* @param string $filePath
*/
public function appendFileToDirectory(string $filePath)
{
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath);
array_pop($DIRECTORY);
$path = DIRECTORY_SEPARATOR;
foreach ($DIRECTORY as $value) {
$path = $this->makeMoneyDirectoryArray($path, $value);
$this->_directoryMap[$path][] = $filePath;
}
}
/** /**
* @param string $Directory * @param string $Directory
* @return array * @return array
@@ -290,6 +321,19 @@ class Loader extends BaseObject
} }
/**
* @param string $Directory
* @param string|null $output
*/
public function directoryRuntime(string $Directory, ?string $output)
{
if (!empty($output) && isset($this->_directoryMap[$output])) {
unset($this->_directoryMap[$output]);
}
$this->execute($Directory);
}
/** /**
* @param string $filename * @param string $filename
* @return mixed * @return mixed
@@ -301,4 +345,39 @@ class Loader extends BaseObject
} }
return $this->_classes[$this->_fileMap[$filename]]; return $this->_classes[$this->_fileMap[$filename]];
} }
/**
* @param $directory
*/
private function execute(string $directory)
{
if (!isset($this->_directoryMap[$directory])) {
return;
}
$directories = $this->_directoryMap[$directory];
foreach ($directories as $className) {
if (!isset($this->_classes[$className])) {
continue;
}
$annotations = $this->_classes[$className];
if (isset($annotations['target']) && !empty($annotations['target'])) {
foreach ($annotations['target'] as $value) {
$value->execute([$annotations['handler']]);
}
}
foreach ($annotations['methods'] as $name => $attribute) {
foreach ($attribute as $value) {
if (!($value instanceof \Annotation\Attribute)) {
continue;
}
$value->execute([$annotations['handler'], $name]);
}
}
}
}
} }
+8 -2
View File
@@ -46,11 +46,17 @@ class OnWorkerStart extends Callback
$annotation->setLoader($runtime); $annotation->setLoader($runtime);
if ($worker_id >= $server->setting['worker_num']) { if ($worker_id >= $server->setting['worker_num']) {
$annotation->instanceDirectoryFiles(MODEL_PATH); $annotation->runtime(MODEL_PATH);
$this->onTask($server, $worker_id); $this->onTask($server, $worker_id);
} else { } else {
$annotation->instanceDirectoryFiles(APP_PATH);
$start = microtime(true);
$annotation->runtime(CONTROLLER_PATH);
$annotation->runtime(APP_PATH, CONTROLLER_PATH);
$this->error('use time ' . (microtime(true) - $start));
$this->onWorker($server, $worker_id); $this->onWorker($server, $worker_id);
} }