-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookLoader.php
More file actions
146 lines (125 loc) · 3.75 KB
/
Copy pathHookLoader.php
File metadata and controls
146 lines (125 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Copyright (c) Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*/
namespace Dbout\WpHook;
use Dbout\WpHook\Enums\ActionType;
use Dbout\WpHook\Exceptions\HookException;
use Dbout\WpHook\FileLocators\FileLocator;
use Dbout\WpHook\Helpers\SortActions;
use Dbout\WpHook\Loaders\AttributeDirectoryLoader;
use Psr\Cache\CacheItemPoolInterface;
class HookLoader
{
private const string CACHE_KEY = '_app_wp_autoloader_hooks';
/**
* @param string|array<string> $directory
* @param CacheItemPoolInterface|null $cache
*/
public function __construct(
protected string|array $directory,
public ?CacheItemPoolInterface $cache = null,
) {
}
/**
* @throws \Psr\Cache\InvalidArgumentException
* @throws \Exception
* @return Action[]
*/
protected function getHooks(): array
{
if (!$this->cache instanceof CacheItemPoolInterface) {
return $this->findHooks();
}
$cacheRoutes = $this->cache->getItem(self::CACHE_KEY);
if ($cacheRoutes->isHit()) {
try {
$result = unserialize($cacheRoutes->get());
if (is_array($result)) {
return $result;
}
} catch (\Exception) {
}
}
$routes = $this->findHooks();
$cacheRoutes->set(serialize($routes));
$this->cache->save($cacheRoutes);
return $routes;
}
/**
* @throws \Exception
* @return Action[]
*/
protected function findHooks(): array
{
$directories = $this->getDirectories();
$hooks = [];
foreach ($directories as $directory) {
$loader = new AttributeDirectoryLoader(
new FileLocator([$directory])
);
$hooks = array_merge($hooks, $loader->load($directory));
}
return (new SortActions())->sort($hooks);
}
/**
* @throws HookException
* @return array<string>
*/
protected function getDirectories(): array
{
$tmpDirectories = is_array($this->directory) ? $this->directory : [$this->directory];
$directories = [];
foreach ($tmpDirectories as $dir) {
$globalDirs = glob($dir);
if (!is_array($globalDirs)) {
continue;
}
$directories = array_merge($directories, $globalDirs);
}
$dirs = [];
foreach ($directories as $directory) {
$directory = new \SplFileInfo($directory);
if (!$directory->isDir()) {
throw new HookException(sprintf(
'The path %s is not a valid folder.',
$directory
));
}
$path = $directory->getRealPath();
if (!is_string($path)) {
continue;
}
$dirs[] = $path;
}
return $dirs;
}
/**
* @throws \Psr\Cache\InvalidArgumentException
* @return void
*/
public function register(): void
{
$hooks = $this->getHooks();
foreach ($hooks as $hook) {
match ($hook->actionType) {
ActionType::Action => add_action(...$this->getHookArg($hook)),
ActionType::Filter => add_filter(...$this->getHookArg($hook)),
};
}
}
/**
* @param Action $action
* @return array<string, mixed>
*/
protected function getHookArg(Action $action): array
{
return [
'hook_name' => $action->name,
'callback' => [$action->classInstance, $action->methodName],
'priority' => $action->priority,
'accepted_args' => $action->acceptedArgs,
];
}
}