PluginProbe
JCH Optimize / trunk
JCH Optimize vtrunk
6.0.2 6.0.1 trunk 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.10.0 2.2.0 2.2.1 All 58 releases
jch-optimize / lib / src / Service / Provider / LaminasCacheServices.php

LaminasCacheServices.php in JCH Optimize trunk, at lib/src/Service/Provider/LaminasCacheServices.php

229 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * JCH Optimize - Performs several front-end optimizations for fast downloads
5 *
6 * @package jchoptimize/core
7 * @author Samuel Marshall <samuel@jch-optimize.net>
8 * @copyright Copyright (c) 2023 Samuel Marshall / JCH Optimize
9 * @license GNU/GPLv3, or later. See LICENSE file
10 *
11 * If LICENSE file missing, see <http://www.gnu.org/licenses/>.
12 */
13
14 namespace JchOptimize\Core\Service\Provider;
15
16 use _JchOptimizeVendor\V91\Joomla\DI\Container;
17 use _JchOptimizeVendor\V91\Joomla\DI\ServiceProviderInterface;
18 use _JchOptimizeVendor\V91\Laminas\Cache\Service\StorageAdapterFactory;
19 use _JchOptimizeVendor\V91\Laminas\Cache\Service\StorageAdapterFactoryInterface;
20 use _JchOptimizeVendor\V91\Laminas\Cache\Service\StoragePluginFactory;
21 use _JchOptimizeVendor\V91\Laminas\Cache\Service\StoragePluginFactoryInterface;
22 use _JchOptimizeVendor\V91\Laminas\Cache\Storage\Adapter\Apcu;
23 use _JchOptimizeVendor\V91\Laminas\Cache\Storage\Adapter\BlackHole;
24 use _JchOptimizeVendor\V91\Laminas\Cache\Storage\Adapter\Filesystem;
25 use _JchOptimizeVendor\V91\Laminas\Cache\Storage\Adapter\Memcached;
26 use _JchOptimizeVendor\V91\Laminas\Cache\Storage\Adapter\Redis;
27 use _JchOptimizeVendor\V91\Laminas\Cache\Storage\AdapterPluginManager;
28 use _JchOptimizeVendor\V91\Laminas\Cache\Storage\PluginManager;
29 use _JchOptimizeVendor\V91\Laminas\ServiceManager\Factory\InvokableFactory;
30 use _JchOptimizeVendor\V91\Laminas\ServiceManager\PluginManagerInterface;
31 use _JchOptimizeVendor\V91\Psr\Log\LoggerInterface;
32 use JchOptimize\Core\Platform\PathsInterface;
33 use JchOptimize\Core\Registry;
34
35 use function defined;
36 use function fileperms;
37 use function octdec;
38 use function sprintf;
39 use function substr;
40
41 defined('_JCH_EXEC') or die('Restricted access');
42
43 class LaminasCacheServices implements ServiceProviderInterface
44 {
45 public function register(Container $container): void
46 {
47 $container->alias(StorageAdapterFactoryInterface::class, StorageAdapterFactory::class)
48 ->share(StorageAdapterFactory::class, [$this, 'getStorageAdapterFactoryService'], true);
49
50 $container->alias(PluginManagerInterface::class, AdapterPluginManager::class)
51 ->share(AdapterPluginManager::class, [$this, 'getAdapterPluginManagerService'], true);
52
53 $container->alias(StoragePluginFactoryInterface::class, StoragePluginFactory::class)
54 ->share(StoragePluginFactory::class, [$this, 'getStoragePluginFactoryService'], true);
55
56 $container->share(PluginManager::class, [$this, 'getPluginManagerService'], true);
57
58 $container->share('config', [$this, 'getConfigurationService'], true);
59 }
60
61 public function getStorageAdapterFactoryService(Container $container): StorageAdapterFactoryInterface
62 {
63 return new StorageAdapterFactory(
64 $container->get(PluginManagerInterface::class),
65 $container->get(StoragePluginFactoryInterface::class)
66 );
67 }
68
69 public function getAdapterPluginManagerService(Container $container): PluginManagerInterface
70 {
71 return new AdapterPluginManager(
72 $container,
73 $container->get('config')['dependencies']
74 );
75 }
76
77 public function getStoragePluginFactoryService(Container $container): StoragePluginFactoryInterface
78 {
79 return new StoragePluginFactory($container->get(PluginManager::class));
80 }
81
82 public function getPluginManagerService(Container $container): PluginManagerInterface
83 {
84 return new PluginManager(
85 $container,
86 $container->get('config')['dependencies']
87 );
88 }
89
90 public function getConfigurationService(Container $container): array
91 {
92 $dirPermission = octdec(substr(sprintf('%o', fileperms(__DIR__)), -4)) ?: 0755;
93 $filePermission = octdec(substr(sprintf('%o', fileperms(__FILE__)), -4)) ?: 0644;
94
95 //Ensure owner has permissions to execute, read, and write directory
96 $dirPermission = ($dirPermission | 0700);
97 //Ensure owner has permissions to read and write files
98 $filePermission = ($filePermission | 0600);
99 //Ensure files are not executable
100 $filePermission = ($filePermission & ~0111);
101
102 $params = $container->get(Registry::class);
103 $paths = $container->get(PathsInterface::class);
104 $logger = $container->get(LoggerInterface::class);
105
106 $redisServerHost = (string) $params->get('redis_server_host', '127.0.0.1');
107
108 if (str_ends_with(trim($redisServerHost), '.sock')) {
109 $redisServer = $redisServerHost;
110 } else {
111 $redisServer = [
112 'host' => $redisServerHost,
113 'port' => (int)$params->get('redis_server_port', 6379)
114 ];
115 }
116
117 return [
118 'caches' => [
119 'filesystem' => [
120 'name' => 'filesystem',
121 'options' => [
122 'cache_dir' => $paths->cacheDir(),
123 'dir_level' => 2,
124 'dir_permission' => $dirPermission,
125 'file_permission' => $filePermission
126 ],
127 'plugins' => [
128 [
129 'name' => 'serializer'
130 ],
131 [
132 'name' => 'exception_handler',
133 'options' => [
134 'exception_callback' => [$logger, 'error'],
135 'throw_exceptions' => false
136 ]
137 ]
138 ]
139 ],
140 'memcached' => [
141 'name' => 'memcached',
142 'options' => [
143 'servers' => [
144 [ (string)$params->get(
145 'memcached_server_host',
146 '127.0.0.1'
147 ),
148 (int)$params->get('memcached_server_port', 11211)
149 ]
150 ]
151 ],
152 'plugins' => [
153 [
154 'name' => 'exception_handler',
155 'options' => [
156 'exception_callback' => [$logger, 'error'],
157 'throw_exceptions' => false
158 ]
159 ]
160 ]
161 ],
162 'apcu' => [
163 'name' => 'apcu',
164 'options' => [],
165 'plugins' => [
166 [
167 'name' => 'exception_handler',
168 'options' => [
169 'exception_callback' => [$logger, 'error'],
170 'throw_exceptions' => false
171 ]
172 ]
173 ]
174 ],
175 'redis' => [
176 'name' => 'redis',
177 'options' => [
178 'server' => $redisServer,
179 'password' => (string)$params->get('redis_server_password', ''),
180 'database' => (int)$params->get('redis_server_db', 0)
181 ],
182 'plugins' => [
183 [
184 'name' => 'serializer'
185 ],
186 [
187 'name' => 'exception_handler',
188 'options' => [
189 'exception_callback' => [$logger, 'error'],
190 'throw_exceptions' => false
191 ]
192 ]
193 ]
194 ],
195 'blackhole' => [
196 'name' => 'blackhole',
197 'options' => [],
198 'plugins' => [
199 [
200 'name' => 'exception_handler',
201 'options' => [
202 'exception_callback' => [$logger, 'error'],
203 'throw_exceptions' => false
204 ]
205 ]
206 ]
207 ]
208
209 ],
210 'dependencies' => [
211 'factories' => [
212 Filesystem::class => InvokableFactory::class,
213 Memcached::class => InvokableFactory::class,
214 Apcu::class => InvokableFactory::class,
215 Redis::class => InvokableFactory::class,
216 BlackHole::class => InvokableFactory::class
217 ],
218 'aliases' => [
219 'filesystem' => Filesystem::class,
220 'memcached' => Memcached::class,
221 'apcu' => Apcu::class,
222 'redis' => Redis::class,
223 'blackhole' => BlackHole::class
224 ],
225 ]
226 ];
227 }
228 }
229