| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Core\Service;
- use ReflectionClass;
- use ReflectionMethod;
- use SixShop\Core\Attribute\Hook;
- use think\Event;
- use think\exception\ClassNotFoundException;
- use function SixShop\Core\extension_name_list;
- readonly class HookAttributeService
- {
- public function __construct(private AutoloadService $autoloadService, private Event $event)
- {
- }
- public function init(): void
- {
- foreach (extension_name_list() as $extensionName) {
- try {
- $extension = $this->autoloadService->getExtension($extensionName);
- } catch (ClassNotFoundException) {
- continue;
- }
- if (!$extension->available()) {
- continue;
- }
- $hookClassList = $extension->getHooks();
- foreach ($hookClassList as $hookClass) {
- $ref = new ReflectionClass($hookClass);
- foreach ($ref->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
- $attributes = $method->getAttributes(Hook::class);
- foreach ($attributes as $attr) {
- $hookNameList = (array)$attr->newInstance()->hook;
- foreach ($hookNameList as $hookName) {
- $this->event->listen($hookName, [$hookClass, $method->getName()]);
- }
- }
- }
- }
- }
- }
- }
|