@@ -30,56 +30,56 @@ |
||
30 | 30 | * Register Autoload |
31 | 31 | */ |
32 | 32 | spl_autoload_register( |
33 | - static function ($entity): void { |
|
34 | - $module = explode('\\', $entity, 2); |
|
35 | - if (!\in_array($module[0], ['Phpfastcache', 'Psr'])) { |
|
36 | - /** |
|
37 | - * Not a part of phpFastCache file |
|
38 | - * then we return here. |
|
39 | - */ |
|
40 | - return; |
|
41 | - } |
|
33 | + static function ($entity): void { |
|
34 | + $module = explode('\\', $entity, 2); |
|
35 | + if (!\in_array($module[0], ['Phpfastcache', 'Psr'])) { |
|
36 | + /** |
|
37 | + * Not a part of phpFastCache file |
|
38 | + * then we return here. |
|
39 | + */ |
|
40 | + return; |
|
41 | + } |
|
42 | 42 | |
43 | - if (\strpos($entity, 'Psr\Cache') === 0) { |
|
44 | - $path = PFC_BIN_DIR . 'dependencies/Psr/Cache/src/' . substr(strrchr($entity, '\\'), 1) . '.' . PFC_PHP_EXT; |
|
43 | + if (\strpos($entity, 'Psr\Cache') === 0) { |
|
44 | + $path = PFC_BIN_DIR . 'dependencies/Psr/Cache/src/' . substr(strrchr($entity, '\\'), 1) . '.' . PFC_PHP_EXT; |
|
45 | 45 | |
46 | - if (\is_readable($path)) { |
|
47 | - require_once $path; |
|
48 | - } else { |
|
49 | - \trigger_error('Cannot locate the Psr/Cache files', E_USER_ERROR); |
|
50 | - } |
|
51 | - return; |
|
52 | - } |
|
46 | + if (\is_readable($path)) { |
|
47 | + require_once $path; |
|
48 | + } else { |
|
49 | + \trigger_error('Cannot locate the Psr/Cache files', E_USER_ERROR); |
|
50 | + } |
|
51 | + return; |
|
52 | + } |
|
53 | 53 | |
54 | - if (\strpos($entity, 'Psr\SimpleCache') === 0) { |
|
55 | - $path = PFC_BIN_DIR . 'dependencies/Psr/SimpleCache/src/' . \substr(\strrchr($entity, '\\'), 1) . '.' . PFC_PHP_EXT; |
|
54 | + if (\strpos($entity, 'Psr\SimpleCache') === 0) { |
|
55 | + $path = PFC_BIN_DIR . 'dependencies/Psr/SimpleCache/src/' . \substr(\strrchr($entity, '\\'), 1) . '.' . PFC_PHP_EXT; |
|
56 | 56 | |
57 | - if (\is_readable($path)) { |
|
58 | - require_once $path; |
|
59 | - } else { |
|
60 | - \trigger_error('Cannot locate the Psr/SimpleCache files', E_USER_ERROR); |
|
61 | - } |
|
62 | - return; |
|
63 | - } |
|
57 | + if (\is_readable($path)) { |
|
58 | + require_once $path; |
|
59 | + } else { |
|
60 | + \trigger_error('Cannot locate the Psr/SimpleCache files', E_USER_ERROR); |
|
61 | + } |
|
62 | + return; |
|
63 | + } |
|
64 | 64 | |
65 | - $entityPath = str_replace('\\', '/', $entity); |
|
65 | + $entityPath = str_replace('\\', '/', $entity); |
|
66 | 66 | |
67 | - if(\strpos($entity, PFC_TESTS_NS) === 0){ |
|
68 | - $path = PFC_TESTS_DIR . \str_replace(str_replace('\\', '/', PFC_TESTS_NS), '', $entityPath) . '.' . PFC_PHP_EXT; |
|
69 | - }else{ |
|
70 | - $path = PFC_LIB_DIR . $entityPath . '.' . PFC_PHP_EXT; |
|
71 | - } |
|
67 | + if(\strpos($entity, PFC_TESTS_NS) === 0){ |
|
68 | + $path = PFC_TESTS_DIR . \str_replace(str_replace('\\', '/', PFC_TESTS_NS), '', $entityPath) . '.' . PFC_PHP_EXT; |
|
69 | + }else{ |
|
70 | + $path = PFC_LIB_DIR . $entityPath . '.' . PFC_PHP_EXT; |
|
71 | + } |
|
72 | 72 | |
73 | - $path = \realpath($path); |
|
74 | - if (\is_readable($path)) { |
|
75 | - require_once $path; |
|
76 | - } |
|
77 | - } |
|
73 | + $path = \realpath($path); |
|
74 | + if (\is_readable($path)) { |
|
75 | + require_once $path; |
|
76 | + } |
|
77 | + } |
|
78 | 78 | ); |
79 | 79 | |
80 | 80 | if ((!\defined('PFC_IGNORE_COMPOSER_WARNING') || !PFC_IGNORE_COMPOSER_WARNING) && \class_exists(ClassLoader::class)) { |
81 | - trigger_error( |
|
82 | - 'Your project already makes use of Composer. You SHOULD use the composer dependency "phpfastcache/phpfastcache" instead of hard-autoloading.', |
|
83 | - E_USER_WARNING |
|
84 | - ); |
|
81 | + trigger_error( |
|
82 | + 'Your project already makes use of Composer. You SHOULD use the composer dependency "phpfastcache/phpfastcache" instead of hard-autoloading.', |
|
83 | + E_USER_WARNING |
|
84 | + ); |
|
85 | 85 | } |
@@ -25,145 +25,145 @@ |
||
25 | 25 | */ |
26 | 26 | class Api |
27 | 27 | { |
28 | - protected static $version = '3.0.0'; |
|
29 | - |
|
30 | - /** |
|
31 | - * Api constructor. |
|
32 | - */ |
|
33 | - final protected function __construct() |
|
34 | - { |
|
35 | - // The Api is not meant to be instantiated |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * This method will returns the current |
|
40 | - * API version, the API version will be |
|
41 | - * updated by following the semantic versioning |
|
42 | - * based on changes of: |
|
43 | - * - ExtendedCacheItemPoolInterface |
|
44 | - * - ExtendedCacheItemInterface |
|
45 | - * - AggregatablePoolInterface |
|
46 | - * - AggregatorInterface |
|
47 | - * - ClusterPoolInterface |
|
48 | - * - EventManagerInterface |
|
49 | - * |
|
50 | - * @see https://semver.org/ |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public static function getVersion(): string |
|
54 | - { |
|
55 | - return self::$version; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param bool $fallbackOnChangelog |
|
60 | - * @param bool $cacheable |
|
61 | - * @return string |
|
62 | - * @throws PhpfastcacheLogicException |
|
63 | - * @throws PhpfastcacheIOException |
|
64 | - */ |
|
65 | - public static function getPhpFastCacheVersion(bool $fallbackOnChangelog = true, bool $cacheable = true): string |
|
66 | - { |
|
67 | - /** |
|
68 | - * Cache the version statically to improve |
|
69 | - * performances on multiple calls |
|
70 | - */ |
|
71 | - static $version; |
|
72 | - |
|
73 | - if ($version && $cacheable) { |
|
74 | - return $version; |
|
75 | - } |
|
76 | - |
|
77 | - if (\function_exists('shell_exec')) { |
|
78 | - $command = 'git -C "' . __DIR__ . '" describe --abbrev=0 --tags'; |
|
79 | - $stdout = \shell_exec($command); |
|
80 | - if (\is_string($stdout)) { |
|
81 | - $version = trim($stdout); |
|
82 | - return $version; |
|
83 | - } |
|
84 | - if (!$fallbackOnChangelog) { |
|
85 | - throw new PhpfastcacheLogicException('The git command used to retrieve the PhpFastCache version has failed.'); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - if (!$fallbackOnChangelog) { |
|
90 | - throw new PhpfastcacheLogicException('shell_exec is disabled therefore the PhpFastCache version cannot be retrieved.'); |
|
91 | - } |
|
92 | - |
|
93 | - $changelogFilename = __DIR__ . '/../../CHANGELOG.md'; |
|
94 | - if (\file_exists($changelogFilename)) { |
|
95 | - $versionPrefix = '## '; |
|
96 | - $changelog = \explode("\n", self::getPhpFastCacheChangelog()); |
|
97 | - foreach ($changelog as $line) { |
|
98 | - if (\strpos($line, $versionPrefix) === 0) { |
|
99 | - $version = \trim(\str_replace($versionPrefix, '', $line)); |
|
100 | - return $version; |
|
101 | - } |
|
102 | - } |
|
103 | - throw new PhpfastcacheLogicException('Unable to retrieve the PhpFastCache version through the CHANGELOG.md as no valid string were found in it.'); |
|
104 | - } |
|
105 | - throw new PhpfastcacheLogicException( |
|
106 | - 'shell_exec being disabled we attempted to retrieve the PhpFastCache version through the CHANGELOG.md file but it is not readable or has been removed.' |
|
107 | - ); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Return the PhpFastCache changelog, as a string. |
|
112 | - * @return string |
|
113 | - * @throws PhpfastcacheLogicException |
|
114 | - * @throws PhpfastcacheIOException |
|
115 | - */ |
|
116 | - public static function getPhpFastCacheChangelog(): string |
|
117 | - { |
|
118 | - $changelogFilename = __DIR__ . '/../../CHANGELOG.md'; |
|
119 | - if (\file_exists($changelogFilename)) { |
|
120 | - $string = \str_replace(["\r\n", "\r"], "\n", \trim(\file_get_contents($changelogFilename))); |
|
121 | - if ($string) { |
|
122 | - return $string; |
|
123 | - } |
|
124 | - throw new PhpfastcacheLogicException('Unable to retrieve the PhpFastCache changelog as it seems to be empty.'); |
|
125 | - } |
|
126 | - throw new PhpfastcacheIOException('The CHANGELOG.md file is not readable or has been removed.'); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @param bool $cacheable |
|
131 | - * @return string |
|
132 | - */ |
|
133 | - public static function getPhpFastCacheGitHeadHash(bool $cacheable = true): string |
|
134 | - { |
|
135 | - static $hash; |
|
136 | - |
|
137 | - if ($hash && $cacheable) { |
|
138 | - return $hash; |
|
139 | - } |
|
140 | - |
|
141 | - if (\function_exists('shell_exec')) { |
|
142 | - $stdout = \shell_exec('git rev-parse --short HEAD'); |
|
143 | - if (\is_string($stdout)) { |
|
144 | - $hash = \trim($stdout); |
|
145 | - return "#{$hash}"; |
|
146 | - } |
|
147 | - } |
|
148 | - return ''; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Return the API changelog, as a string. |
|
153 | - * @return string |
|
154 | - * @throws PhpfastcacheLogicException |
|
155 | - * @throws PhpfastcacheIOException |
|
156 | - */ |
|
157 | - public static function getChangelog(): string |
|
158 | - { |
|
159 | - $changelogFilename = __DIR__ . '/../../CHANGELOG_API.md'; |
|
160 | - if (\file_exists($changelogFilename)) { |
|
161 | - $string = \str_replace(["\r\n", "\r"], "\n", \trim(\file_get_contents($changelogFilename))); |
|
162 | - if ($string) { |
|
163 | - return $string; |
|
164 | - } |
|
165 | - throw new PhpfastcacheLogicException('Unable to retrieve the PhpFastCache API changelog as it seems to be empty.'); |
|
166 | - } |
|
167 | - throw new PhpfastcacheIOException('The CHANGELOG_API.md file is not readable or has been removed.'); |
|
168 | - } |
|
28 | + protected static $version = '3.0.0'; |
|
29 | + |
|
30 | + /** |
|
31 | + * Api constructor. |
|
32 | + */ |
|
33 | + final protected function __construct() |
|
34 | + { |
|
35 | + // The Api is not meant to be instantiated |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * This method will returns the current |
|
40 | + * API version, the API version will be |
|
41 | + * updated by following the semantic versioning |
|
42 | + * based on changes of: |
|
43 | + * - ExtendedCacheItemPoolInterface |
|
44 | + * - ExtendedCacheItemInterface |
|
45 | + * - AggregatablePoolInterface |
|
46 | + * - AggregatorInterface |
|
47 | + * - ClusterPoolInterface |
|
48 | + * - EventManagerInterface |
|
49 | + * |
|
50 | + * @see https://semver.org/ |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public static function getVersion(): string |
|
54 | + { |
|
55 | + return self::$version; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param bool $fallbackOnChangelog |
|
60 | + * @param bool $cacheable |
|
61 | + * @return string |
|
62 | + * @throws PhpfastcacheLogicException |
|
63 | + * @throws PhpfastcacheIOException |
|
64 | + */ |
|
65 | + public static function getPhpFastCacheVersion(bool $fallbackOnChangelog = true, bool $cacheable = true): string |
|
66 | + { |
|
67 | + /** |
|
68 | + * Cache the version statically to improve |
|
69 | + * performances on multiple calls |
|
70 | + */ |
|
71 | + static $version; |
|
72 | + |
|
73 | + if ($version && $cacheable) { |
|
74 | + return $version; |
|
75 | + } |
|
76 | + |
|
77 | + if (\function_exists('shell_exec')) { |
|
78 | + $command = 'git -C "' . __DIR__ . '" describe --abbrev=0 --tags'; |
|
79 | + $stdout = \shell_exec($command); |
|
80 | + if (\is_string($stdout)) { |
|
81 | + $version = trim($stdout); |
|
82 | + return $version; |
|
83 | + } |
|
84 | + if (!$fallbackOnChangelog) { |
|
85 | + throw new PhpfastcacheLogicException('The git command used to retrieve the PhpFastCache version has failed.'); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + if (!$fallbackOnChangelog) { |
|
90 | + throw new PhpfastcacheLogicException('shell_exec is disabled therefore the PhpFastCache version cannot be retrieved.'); |
|
91 | + } |
|
92 | + |
|
93 | + $changelogFilename = __DIR__ . '/../../CHANGELOG.md'; |
|
94 | + if (\file_exists($changelogFilename)) { |
|
95 | + $versionPrefix = '## '; |
|
96 | + $changelog = \explode("\n", self::getPhpFastCacheChangelog()); |
|
97 | + foreach ($changelog as $line) { |
|
98 | + if (\strpos($line, $versionPrefix) === 0) { |
|
99 | + $version = \trim(\str_replace($versionPrefix, '', $line)); |
|
100 | + return $version; |
|
101 | + } |
|
102 | + } |
|
103 | + throw new PhpfastcacheLogicException('Unable to retrieve the PhpFastCache version through the CHANGELOG.md as no valid string were found in it.'); |
|
104 | + } |
|
105 | + throw new PhpfastcacheLogicException( |
|
106 | + 'shell_exec being disabled we attempted to retrieve the PhpFastCache version through the CHANGELOG.md file but it is not readable or has been removed.' |
|
107 | + ); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Return the PhpFastCache changelog, as a string. |
|
112 | + * @return string |
|
113 | + * @throws PhpfastcacheLogicException |
|
114 | + * @throws PhpfastcacheIOException |
|
115 | + */ |
|
116 | + public static function getPhpFastCacheChangelog(): string |
|
117 | + { |
|
118 | + $changelogFilename = __DIR__ . '/../../CHANGELOG.md'; |
|
119 | + if (\file_exists($changelogFilename)) { |
|
120 | + $string = \str_replace(["\r\n", "\r"], "\n", \trim(\file_get_contents($changelogFilename))); |
|
121 | + if ($string) { |
|
122 | + return $string; |
|
123 | + } |
|
124 | + throw new PhpfastcacheLogicException('Unable to retrieve the PhpFastCache changelog as it seems to be empty.'); |
|
125 | + } |
|
126 | + throw new PhpfastcacheIOException('The CHANGELOG.md file is not readable or has been removed.'); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @param bool $cacheable |
|
131 | + * @return string |
|
132 | + */ |
|
133 | + public static function getPhpFastCacheGitHeadHash(bool $cacheable = true): string |
|
134 | + { |
|
135 | + static $hash; |
|
136 | + |
|
137 | + if ($hash && $cacheable) { |
|
138 | + return $hash; |
|
139 | + } |
|
140 | + |
|
141 | + if (\function_exists('shell_exec')) { |
|
142 | + $stdout = \shell_exec('git rev-parse --short HEAD'); |
|
143 | + if (\is_string($stdout)) { |
|
144 | + $hash = \trim($stdout); |
|
145 | + return "#{$hash}"; |
|
146 | + } |
|
147 | + } |
|
148 | + return ''; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Return the API changelog, as a string. |
|
153 | + * @return string |
|
154 | + * @throws PhpfastcacheLogicException |
|
155 | + * @throws PhpfastcacheIOException |
|
156 | + */ |
|
157 | + public static function getChangelog(): string |
|
158 | + { |
|
159 | + $changelogFilename = __DIR__ . '/../../CHANGELOG_API.md'; |
|
160 | + if (\file_exists($changelogFilename)) { |
|
161 | + $string = \str_replace(["\r\n", "\r"], "\n", \trim(\file_get_contents($changelogFilename))); |
|
162 | + if ($string) { |
|
163 | + return $string; |
|
164 | + } |
|
165 | + throw new PhpfastcacheLogicException('Unable to retrieve the PhpFastCache API changelog as it seems to be empty.'); |
|
166 | + } |
|
167 | + throw new PhpfastcacheIOException('The CHANGELOG_API.md file is not readable or has been removed.'); |
|
168 | + } |
|
169 | 169 | } |
@@ -23,38 +23,38 @@ |
||
23 | 23 | */ |
24 | 24 | trait MemcacheDriverCollisionDetectorTrait |
25 | 25 | { |
26 | - /** |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected static $driverUsed; |
|
30 | - |
|
31 | - /** |
|
32 | - * @param $driverName |
|
33 | - * @return bool |
|
34 | - */ |
|
35 | - public static function checkCollision($driverName): bool |
|
36 | - { |
|
37 | - $CONSTANT_NAME = __NAMESPACE__ . '\MEMCACHE_DRIVER_USED'; |
|
38 | - |
|
39 | - if ($driverName && is_string($driverName)) { |
|
40 | - if (!defined($CONSTANT_NAME)) { |
|
41 | - define($CONSTANT_NAME, $driverName); |
|
42 | - |
|
43 | - return true; |
|
44 | - } else { |
|
45 | - if (constant($CONSTANT_NAME) !== $driverName) { |
|
46 | - trigger_error( |
|
47 | - 'Memcache collision detected, you used both Memcache and Memcached driver in your script, this may leads to unexpected behaviours', |
|
48 | - E_USER_WARNING |
|
49 | - ); |
|
50 | - |
|
51 | - return false; |
|
52 | - } |
|
53 | - } |
|
54 | - |
|
55 | - return true; |
|
56 | - } |
|
57 | - |
|
58 | - return false; |
|
59 | - } |
|
26 | + /** |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected static $driverUsed; |
|
30 | + |
|
31 | + /** |
|
32 | + * @param $driverName |
|
33 | + * @return bool |
|
34 | + */ |
|
35 | + public static function checkCollision($driverName): bool |
|
36 | + { |
|
37 | + $CONSTANT_NAME = __NAMESPACE__ . '\MEMCACHE_DRIVER_USED'; |
|
38 | + |
|
39 | + if ($driverName && is_string($driverName)) { |
|
40 | + if (!defined($CONSTANT_NAME)) { |
|
41 | + define($CONSTANT_NAME, $driverName); |
|
42 | + |
|
43 | + return true; |
|
44 | + } else { |
|
45 | + if (constant($CONSTANT_NAME) !== $driverName) { |
|
46 | + trigger_error( |
|
47 | + 'Memcache collision detected, you used both Memcache and Memcached driver in your script, this may leads to unexpected behaviours', |
|
48 | + E_USER_WARNING |
|
49 | + ); |
|
50 | + |
|
51 | + return false; |
|
52 | + } |
|
53 | + } |
|
54 | + |
|
55 | + return true; |
|
56 | + } |
|
57 | + |
|
58 | + return false; |
|
59 | + } |
|
60 | 60 | } |
61 | 61 | \ No newline at end of file |
@@ -26,130 +26,130 @@ |
||
26 | 26 | */ |
27 | 27 | class Directory |
28 | 28 | { |
29 | - /** |
|
30 | - * Get the directory size |
|
31 | - * @param string $directory |
|
32 | - * @param bool $includeDirAllocSize |
|
33 | - * @return integer |
|
34 | - */ |
|
35 | - public static function dirSize(string $directory, bool $includeDirAllocSize = false): int |
|
36 | - { |
|
37 | - $size = 0; |
|
38 | - foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { |
|
39 | - /** |
|
40 | - * @var SplFileInfo $file |
|
41 | - */ |
|
42 | - if ($file->isFile()) { |
|
43 | - $size += filesize($file->getRealPath()); |
|
44 | - } else { |
|
45 | - if ($includeDirAllocSize) { |
|
46 | - $size += $file->getSize(); |
|
47 | - } |
|
48 | - } |
|
49 | - } |
|
29 | + /** |
|
30 | + * Get the directory size |
|
31 | + * @param string $directory |
|
32 | + * @param bool $includeDirAllocSize |
|
33 | + * @return integer |
|
34 | + */ |
|
35 | + public static function dirSize(string $directory, bool $includeDirAllocSize = false): int |
|
36 | + { |
|
37 | + $size = 0; |
|
38 | + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { |
|
39 | + /** |
|
40 | + * @var SplFileInfo $file |
|
41 | + */ |
|
42 | + if ($file->isFile()) { |
|
43 | + $size += filesize($file->getRealPath()); |
|
44 | + } else { |
|
45 | + if ($includeDirAllocSize) { |
|
46 | + $size += $file->getSize(); |
|
47 | + } |
|
48 | + } |
|
49 | + } |
|
50 | 50 | |
51 | - return $size; |
|
52 | - } |
|
51 | + return $size; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @param string $path |
|
56 | - * @return int |
|
57 | - */ |
|
58 | - public static function getFileCount(string $path): int |
|
59 | - { |
|
60 | - $count = 0; |
|
61 | - $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); |
|
62 | - foreach ($objects as $object) { |
|
63 | - /** |
|
64 | - * @var SplFileInfo $object |
|
65 | - */ |
|
66 | - if ($object->isFile()) { |
|
67 | - $count++; |
|
68 | - } |
|
69 | - } |
|
54 | + /** |
|
55 | + * @param string $path |
|
56 | + * @return int |
|
57 | + */ |
|
58 | + public static function getFileCount(string $path): int |
|
59 | + { |
|
60 | + $count = 0; |
|
61 | + $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); |
|
62 | + foreach ($objects as $object) { |
|
63 | + /** |
|
64 | + * @var SplFileInfo $object |
|
65 | + */ |
|
66 | + if ($object->isFile()) { |
|
67 | + $count++; |
|
68 | + } |
|
69 | + } |
|
70 | 70 | |
71 | - return $count; |
|
72 | - } |
|
71 | + return $count; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Recursively delete a directory and all of it's contents - e.g.the equivalent of `rm -r` on the command-line. |
|
76 | - * Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure. |
|
77 | - * |
|
78 | - * @param string $source absolute path to directory or file to delete. |
|
79 | - * @param bool $removeOnlyChildren set to true will only remove content inside directory. |
|
80 | - * |
|
81 | - * @return bool true on success; false on failure |
|
82 | - */ |
|
83 | - public static function rrmdir(string $source, bool $removeOnlyChildren = false): bool |
|
84 | - { |
|
85 | - if (empty($source) || file_exists($source) === false) { |
|
86 | - return false; |
|
87 | - } |
|
74 | + /** |
|
75 | + * Recursively delete a directory and all of it's contents - e.g.the equivalent of `rm -r` on the command-line. |
|
76 | + * Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure. |
|
77 | + * |
|
78 | + * @param string $source absolute path to directory or file to delete. |
|
79 | + * @param bool $removeOnlyChildren set to true will only remove content inside directory. |
|
80 | + * |
|
81 | + * @return bool true on success; false on failure |
|
82 | + */ |
|
83 | + public static function rrmdir(string $source, bool $removeOnlyChildren = false): bool |
|
84 | + { |
|
85 | + if (empty($source) || file_exists($source) === false) { |
|
86 | + return false; |
|
87 | + } |
|
88 | 88 | |
89 | - if (is_file($source) || is_link($source)) { |
|
90 | - clearstatcache(true, $source); |
|
91 | - return unlink($source); |
|
92 | - } |
|
89 | + if (is_file($source) || is_link($source)) { |
|
90 | + clearstatcache(true, $source); |
|
91 | + return unlink($source); |
|
92 | + } |
|
93 | 93 | |
94 | - $files = new RecursiveIteratorIterator |
|
95 | - ( |
|
96 | - new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
|
97 | - RecursiveIteratorIterator::CHILD_FIRST |
|
98 | - ); |
|
94 | + $files = new RecursiveIteratorIterator |
|
95 | + ( |
|
96 | + new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
|
97 | + RecursiveIteratorIterator::CHILD_FIRST |
|
98 | + ); |
|
99 | 99 | |
100 | - foreach ($files as $fileinfo) { |
|
101 | - /** |
|
102 | - * @var SplFileInfo $fileinfo |
|
103 | - */ |
|
104 | - $realpath = $fileinfo->getRealPath(); |
|
105 | - if($realpath){ |
|
106 | - if ($fileinfo->isDir()) { |
|
107 | - if (self::rrmdir($fileinfo->getRealPath()) === false) { |
|
108 | - return false; |
|
109 | - } |
|
110 | - } elseif (unlink($realpath) === false) { |
|
111 | - return false; |
|
112 | - } |
|
113 | - } |
|
114 | - else{ |
|
115 | - return false; |
|
116 | - } |
|
117 | - } |
|
100 | + foreach ($files as $fileinfo) { |
|
101 | + /** |
|
102 | + * @var SplFileInfo $fileinfo |
|
103 | + */ |
|
104 | + $realpath = $fileinfo->getRealPath(); |
|
105 | + if($realpath){ |
|
106 | + if ($fileinfo->isDir()) { |
|
107 | + if (self::rrmdir($fileinfo->getRealPath()) === false) { |
|
108 | + return false; |
|
109 | + } |
|
110 | + } elseif (unlink($realpath) === false) { |
|
111 | + return false; |
|
112 | + } |
|
113 | + } |
|
114 | + else{ |
|
115 | + return false; |
|
116 | + } |
|
117 | + } |
|
118 | 118 | |
119 | - if ($removeOnlyChildren === false) { |
|
120 | - return rmdir($source); |
|
121 | - } |
|
119 | + if ($removeOnlyChildren === false) { |
|
120 | + return rmdir($source); |
|
121 | + } |
|
122 | 122 | |
123 | - return true; |
|
124 | - } |
|
123 | + return true; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * Alias of realpath() but work |
|
128 | - * on non-existing files |
|
129 | - * |
|
130 | - * @param string $path |
|
131 | - * @return string |
|
132 | - */ |
|
133 | - public static function getAbsolutePath(string $path): string |
|
134 | - { |
|
135 | - $parts = preg_split('~[/\\\\]+~', $path, 0, PREG_SPLIT_NO_EMPTY); |
|
136 | - $absolutes = []; |
|
137 | - foreach ($parts as $part) { |
|
138 | - if ('.' === $part) { |
|
139 | - continue; |
|
140 | - } |
|
141 | - if ('..' === $part) { |
|
142 | - array_pop($absolutes); |
|
143 | - } else { |
|
144 | - $absolutes[] = $part; |
|
145 | - } |
|
146 | - } |
|
126 | + /** |
|
127 | + * Alias of realpath() but work |
|
128 | + * on non-existing files |
|
129 | + * |
|
130 | + * @param string $path |
|
131 | + * @return string |
|
132 | + */ |
|
133 | + public static function getAbsolutePath(string $path): string |
|
134 | + { |
|
135 | + $parts = preg_split('~[/\\\\]+~', $path, 0, PREG_SPLIT_NO_EMPTY); |
|
136 | + $absolutes = []; |
|
137 | + foreach ($parts as $part) { |
|
138 | + if ('.' === $part) { |
|
139 | + continue; |
|
140 | + } |
|
141 | + if ('..' === $part) { |
|
142 | + array_pop($absolutes); |
|
143 | + } else { |
|
144 | + $absolutes[] = $part; |
|
145 | + } |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * Allows to dereference char |
|
150 | - */ |
|
151 | - $__FILE__ = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc. |
|
152 | - $prefix = $__FILE__[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : ''; |
|
153 | - return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes); |
|
154 | - } |
|
148 | + /** |
|
149 | + * Allows to dereference char |
|
150 | + */ |
|
151 | + $__FILE__ = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc. |
|
152 | + $prefix = $__FILE__[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : ''; |
|
153 | + return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes); |
|
154 | + } |
|
155 | 155 | } |
@@ -22,13 +22,13 @@ |
||
22 | 22 | */ |
23 | 23 | interface ClassNamespaceResolverInterface |
24 | 24 | { |
25 | - /** |
|
26 | - * @return string |
|
27 | - */ |
|
28 | - public function getClassNamespace(): string; |
|
25 | + /** |
|
26 | + * @return string |
|
27 | + */ |
|
28 | + public function getClassNamespace(): string; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function getClassName(): string; |
|
30 | + /** |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function getClassName(): string; |
|
34 | 34 | } |
@@ -27,141 +27,141 @@ |
||
27 | 27 | class ArrayObject implements ArrayAccess, Iterator, Countable |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - private $array; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - private $position = 0; |
|
39 | - |
|
40 | - /** |
|
41 | - * @param $args |
|
42 | - * ArrayObject constructor. |
|
43 | - */ |
|
44 | - public function __construct(...$args) |
|
45 | - { |
|
46 | - $this->array = (count($args) === 1 && is_array($args[0]) ? $args[0] : $args); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @return mixed |
|
51 | - */ |
|
52 | - #[\ReturnTypeWillChange] // PHP 8.1 compatibility |
|
53 | - public function current() |
|
54 | - { |
|
55 | - return $this->array[$this->position]; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * |
|
60 | - */ |
|
61 | - public function next(): void |
|
62 | - { |
|
63 | - ++$this->position; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @return int |
|
68 | - */ |
|
69 | - public function key(): int |
|
70 | - { |
|
71 | - return $this->position; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @return bool |
|
76 | - */ |
|
77 | - public function valid(): bool |
|
78 | - { |
|
79 | - return $this->offsetExists($this->position); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @param mixed $offset |
|
84 | - * @return bool |
|
85 | - */ |
|
86 | - public function offsetExists($offset): bool |
|
87 | - { |
|
88 | - return array_key_exists($offset, $this->array); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * |
|
93 | - */ |
|
94 | - public function rewind(): void |
|
95 | - { |
|
96 | - $this->position = 0; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @return int |
|
101 | - */ |
|
102 | - public function count(): int |
|
103 | - { |
|
104 | - return count($this->array); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @param mixed $offset |
|
109 | - * @return mixed |
|
110 | - */ |
|
111 | - #[\ReturnTypeWillChange] // PHP 8.1 compatibility |
|
112 | - public function offsetGet($offset) |
|
113 | - { |
|
114 | - return $this->array[$offset] ?? null; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @param mixed $offset |
|
119 | - * @param mixed $value |
|
120 | - */ |
|
121 | - public function offsetSet($offset, $value): void |
|
122 | - { |
|
123 | - // NOTE: THIS IS THE FIX FOR THE ISSUE "Indirect modification of overloaded element of SplFixedArray has no effect" |
|
124 | - // NOTE: WHEN APPENDING AN ARRAY (E.G. myArr[] = 5) THE KEY IS NULL, SO WE TEST FOR THIS CONDITION BELOW, AND VOILA |
|
125 | - |
|
126 | - if ($offset === null) { |
|
127 | - $this->array[] = $value; |
|
128 | - } else { |
|
129 | - $this->array[$offset] = $value; |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @param mixed $offset |
|
135 | - */ |
|
136 | - public function offsetUnset($offset): void |
|
137 | - { |
|
138 | - unset($this->array[$offset]); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @return array |
|
143 | - */ |
|
144 | - public function toArray(): array |
|
145 | - { |
|
146 | - return $this->array; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * @param array $array |
|
151 | - * @return self |
|
152 | - */ |
|
153 | - public function mergeArray(array $array): self |
|
154 | - { |
|
155 | - $this->array = array_merge($this->array, $array); |
|
156 | - |
|
157 | - return $this; |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @return array |
|
162 | - */ |
|
163 | - protected function &getArray(): array |
|
164 | - { |
|
165 | - return $this->array; |
|
166 | - } |
|
30 | + /** |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + private $array; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + private $position = 0; |
|
39 | + |
|
40 | + /** |
|
41 | + * @param $args |
|
42 | + * ArrayObject constructor. |
|
43 | + */ |
|
44 | + public function __construct(...$args) |
|
45 | + { |
|
46 | + $this->array = (count($args) === 1 && is_array($args[0]) ? $args[0] : $args); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @return mixed |
|
51 | + */ |
|
52 | + #[\ReturnTypeWillChange] // PHP 8.1 compatibility |
|
53 | + public function current() |
|
54 | + { |
|
55 | + return $this->array[$this->position]; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * |
|
60 | + */ |
|
61 | + public function next(): void |
|
62 | + { |
|
63 | + ++$this->position; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @return int |
|
68 | + */ |
|
69 | + public function key(): int |
|
70 | + { |
|
71 | + return $this->position; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @return bool |
|
76 | + */ |
|
77 | + public function valid(): bool |
|
78 | + { |
|
79 | + return $this->offsetExists($this->position); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @param mixed $offset |
|
84 | + * @return bool |
|
85 | + */ |
|
86 | + public function offsetExists($offset): bool |
|
87 | + { |
|
88 | + return array_key_exists($offset, $this->array); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * |
|
93 | + */ |
|
94 | + public function rewind(): void |
|
95 | + { |
|
96 | + $this->position = 0; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @return int |
|
101 | + */ |
|
102 | + public function count(): int |
|
103 | + { |
|
104 | + return count($this->array); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @param mixed $offset |
|
109 | + * @return mixed |
|
110 | + */ |
|
111 | + #[\ReturnTypeWillChange] // PHP 8.1 compatibility |
|
112 | + public function offsetGet($offset) |
|
113 | + { |
|
114 | + return $this->array[$offset] ?? null; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @param mixed $offset |
|
119 | + * @param mixed $value |
|
120 | + */ |
|
121 | + public function offsetSet($offset, $value): void |
|
122 | + { |
|
123 | + // NOTE: THIS IS THE FIX FOR THE ISSUE "Indirect modification of overloaded element of SplFixedArray has no effect" |
|
124 | + // NOTE: WHEN APPENDING AN ARRAY (E.G. myArr[] = 5) THE KEY IS NULL, SO WE TEST FOR THIS CONDITION BELOW, AND VOILA |
|
125 | + |
|
126 | + if ($offset === null) { |
|
127 | + $this->array[] = $value; |
|
128 | + } else { |
|
129 | + $this->array[$offset] = $value; |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @param mixed $offset |
|
135 | + */ |
|
136 | + public function offsetUnset($offset): void |
|
137 | + { |
|
138 | + unset($this->array[$offset]); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @return array |
|
143 | + */ |
|
144 | + public function toArray(): array |
|
145 | + { |
|
146 | + return $this->array; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * @param array $array |
|
151 | + * @return self |
|
152 | + */ |
|
153 | + public function mergeArray(array $array): self |
|
154 | + { |
|
155 | + $this->array = array_merge($this->array, $array); |
|
156 | + |
|
157 | + return $this; |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @return array |
|
162 | + */ |
|
163 | + protected function &getArray(): array |
|
164 | + { |
|
165 | + return $this->array; |
|
166 | + } |
|
167 | 167 | } |
@@ -28,148 +28,148 @@ |
||
28 | 28 | */ |
29 | 29 | trait ClassNamespaceResolverTrait |
30 | 30 | { |
31 | - /** |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $namespace; |
|
31 | + /** |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $namespace; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Iterate over all files in the given directory searching for classes. |
|
38 | - * |
|
39 | - * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
40 | - * deprecated the whole component as of SF4. Our thanks to them. |
|
41 | - * |
|
42 | - * @param Iterator|string|array $dir The directory to search in or an iterator |
|
43 | - * |
|
44 | - * @return array A class map array |
|
45 | - */ |
|
46 | - protected static function createClassMap($dir): array |
|
47 | - { |
|
48 | - if (\is_string($dir)) { |
|
49 | - $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); |
|
50 | - } |
|
51 | - $map = []; |
|
36 | + /** |
|
37 | + * Iterate over all files in the given directory searching for classes. |
|
38 | + * |
|
39 | + * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
40 | + * deprecated the whole component as of SF4. Our thanks to them. |
|
41 | + * |
|
42 | + * @param Iterator|string|array $dir The directory to search in or an iterator |
|
43 | + * |
|
44 | + * @return array A class map array |
|
45 | + */ |
|
46 | + protected static function createClassMap($dir): array |
|
47 | + { |
|
48 | + if (\is_string($dir)) { |
|
49 | + $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); |
|
50 | + } |
|
51 | + $map = []; |
|
52 | 52 | |
53 | - if (\is_iterable($dir)) { |
|
54 | - foreach ($dir as $file) { |
|
55 | - if (!$file->isFile()) { |
|
56 | - continue; |
|
57 | - } |
|
58 | - $path = $file->getRealPath() ?: $file->getPathname(); |
|
59 | - if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) { |
|
60 | - continue; |
|
61 | - } |
|
62 | - $classes = self::findClasses($path); |
|
63 | - if (PHP_VERSION_ID >= 70000) { |
|
64 | - // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 |
|
65 | - gc_mem_caches(); |
|
66 | - } |
|
67 | - foreach ($classes as $class) { |
|
68 | - $map[$class] = $path; |
|
69 | - } |
|
70 | - } |
|
71 | - } |
|
53 | + if (\is_iterable($dir)) { |
|
54 | + foreach ($dir as $file) { |
|
55 | + if (!$file->isFile()) { |
|
56 | + continue; |
|
57 | + } |
|
58 | + $path = $file->getRealPath() ?: $file->getPathname(); |
|
59 | + if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) { |
|
60 | + continue; |
|
61 | + } |
|
62 | + $classes = self::findClasses($path); |
|
63 | + if (PHP_VERSION_ID >= 70000) { |
|
64 | + // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 |
|
65 | + gc_mem_caches(); |
|
66 | + } |
|
67 | + foreach ($classes as $class) { |
|
68 | + $map[$class] = $path; |
|
69 | + } |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - return $map; |
|
74 | - } |
|
73 | + return $map; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Extract the classes in the given file. |
|
78 | - * |
|
79 | - * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
80 | - * deprecated the whole component as of SF4. Our thanks to them. |
|
81 | - * |
|
82 | - * @param string $path The file to check |
|
83 | - * |
|
84 | - * @return array The found classes |
|
85 | - */ |
|
86 | - protected static function findClasses(string $path): array |
|
87 | - { |
|
88 | - $contents = file_get_contents($path); |
|
89 | - $tokens = token_get_all($contents); |
|
90 | - $classes = []; |
|
91 | - $namespace = ''; |
|
92 | - for ($i = 0; isset($tokens[$i]); ++$i) { |
|
93 | - $token = $tokens[$i]; |
|
94 | - if (!isset($token[1])) { |
|
95 | - continue; |
|
96 | - } |
|
97 | - $class = ''; |
|
98 | - switch ($token[0]) { |
|
99 | - case T_NAMESPACE: |
|
100 | - $namespace = ''; |
|
101 | - // If there is a namespace, extract it (PHP 8 test) |
|
102 | - if(\defined('T_NAME_QUALIFIED')){ |
|
103 | - while (isset($tokens[++$i][1])) { |
|
104 | - if ($tokens[$i][0] === T_NAME_QUALIFIED) { |
|
105 | - $namespace = $tokens[$i][1]; |
|
106 | - break; |
|
107 | - } |
|
108 | - } |
|
109 | - }else{ |
|
110 | - while (isset($tokens[++$i][1])) { |
|
111 | - if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR], true)) { |
|
112 | - $namespace .= $tokens[$i][1]; |
|
113 | - } |
|
114 | - } |
|
115 | - } |
|
116 | - $namespace .= '\\'; |
|
117 | - break; |
|
118 | - case T_CLASS: |
|
119 | - case T_INTERFACE: |
|
120 | - case T_TRAIT: |
|
121 | - // Skip usage of ::class constant |
|
122 | - $isClassConstant = false; |
|
123 | - for ($j = $i - 1; $j > 0; --$j) { |
|
124 | - if (!isset($tokens[$j][1])) { |
|
125 | - break; |
|
126 | - } |
|
127 | - if (T_DOUBLE_COLON === $tokens[$j][0]) { |
|
128 | - $isClassConstant = true; |
|
129 | - break; |
|
130 | - } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) { |
|
131 | - break; |
|
132 | - } |
|
133 | - } |
|
134 | - if ($isClassConstant) { |
|
135 | - break; |
|
136 | - } |
|
137 | - // Find the classname |
|
138 | - while (isset($tokens[++$i][1])) { |
|
139 | - $t = $tokens[$i]; |
|
140 | - if (T_STRING === $t[0]) { |
|
141 | - $class .= $t[1]; |
|
142 | - } elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
143 | - break; |
|
144 | - } |
|
145 | - } |
|
146 | - $classes[] = ltrim($namespace . $class, '\\'); |
|
147 | - break; |
|
148 | - default: |
|
149 | - break; |
|
150 | - } |
|
151 | - } |
|
76 | + /** |
|
77 | + * Extract the classes in the given file. |
|
78 | + * |
|
79 | + * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
80 | + * deprecated the whole component as of SF4. Our thanks to them. |
|
81 | + * |
|
82 | + * @param string $path The file to check |
|
83 | + * |
|
84 | + * @return array The found classes |
|
85 | + */ |
|
86 | + protected static function findClasses(string $path): array |
|
87 | + { |
|
88 | + $contents = file_get_contents($path); |
|
89 | + $tokens = token_get_all($contents); |
|
90 | + $classes = []; |
|
91 | + $namespace = ''; |
|
92 | + for ($i = 0; isset($tokens[$i]); ++$i) { |
|
93 | + $token = $tokens[$i]; |
|
94 | + if (!isset($token[1])) { |
|
95 | + continue; |
|
96 | + } |
|
97 | + $class = ''; |
|
98 | + switch ($token[0]) { |
|
99 | + case T_NAMESPACE: |
|
100 | + $namespace = ''; |
|
101 | + // If there is a namespace, extract it (PHP 8 test) |
|
102 | + if(\defined('T_NAME_QUALIFIED')){ |
|
103 | + while (isset($tokens[++$i][1])) { |
|
104 | + if ($tokens[$i][0] === T_NAME_QUALIFIED) { |
|
105 | + $namespace = $tokens[$i][1]; |
|
106 | + break; |
|
107 | + } |
|
108 | + } |
|
109 | + }else{ |
|
110 | + while (isset($tokens[++$i][1])) { |
|
111 | + if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR], true)) { |
|
112 | + $namespace .= $tokens[$i][1]; |
|
113 | + } |
|
114 | + } |
|
115 | + } |
|
116 | + $namespace .= '\\'; |
|
117 | + break; |
|
118 | + case T_CLASS: |
|
119 | + case T_INTERFACE: |
|
120 | + case T_TRAIT: |
|
121 | + // Skip usage of ::class constant |
|
122 | + $isClassConstant = false; |
|
123 | + for ($j = $i - 1; $j > 0; --$j) { |
|
124 | + if (!isset($tokens[$j][1])) { |
|
125 | + break; |
|
126 | + } |
|
127 | + if (T_DOUBLE_COLON === $tokens[$j][0]) { |
|
128 | + $isClassConstant = true; |
|
129 | + break; |
|
130 | + } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) { |
|
131 | + break; |
|
132 | + } |
|
133 | + } |
|
134 | + if ($isClassConstant) { |
|
135 | + break; |
|
136 | + } |
|
137 | + // Find the classname |
|
138 | + while (isset($tokens[++$i][1])) { |
|
139 | + $t = $tokens[$i]; |
|
140 | + if (T_STRING === $t[0]) { |
|
141 | + $class .= $t[1]; |
|
142 | + } elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
143 | + break; |
|
144 | + } |
|
145 | + } |
|
146 | + $classes[] = ltrim($namespace . $class, '\\'); |
|
147 | + break; |
|
148 | + default: |
|
149 | + break; |
|
150 | + } |
|
151 | + } |
|
152 | 152 | |
153 | - return $classes; |
|
154 | - } |
|
153 | + return $classes; |
|
154 | + } |
|
155 | 155 | |
156 | - /** |
|
157 | - * @return string |
|
158 | - */ |
|
159 | - public function getClassNamespace(): string |
|
160 | - { |
|
161 | - if (!$this->namespace) { |
|
162 | - $this->namespace = substr(static::class, 0, strrpos(static::class, '\\')); |
|
163 | - } |
|
156 | + /** |
|
157 | + * @return string |
|
158 | + */ |
|
159 | + public function getClassNamespace(): string |
|
160 | + { |
|
161 | + if (!$this->namespace) { |
|
162 | + $this->namespace = substr(static::class, 0, strrpos(static::class, '\\')); |
|
163 | + } |
|
164 | 164 | |
165 | - return $this->namespace; |
|
166 | - } |
|
165 | + return $this->namespace; |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * @return string |
|
170 | - */ |
|
171 | - public function getClassName(): string |
|
172 | - { |
|
173 | - return static::class; |
|
174 | - } |
|
168 | + /** |
|
169 | + * @return string |
|
170 | + */ |
|
171 | + public function getClassName(): string |
|
172 | + { |
|
173 | + return static::class; |
|
174 | + } |
|
175 | 175 | } |
@@ -25,60 +25,60 @@ |
||
25 | 25 | */ |
26 | 26 | interface TaggableCacheItemInterface |
27 | 27 | { |
28 | - /** |
|
29 | - * @param string $tagName |
|
30 | - * |
|
31 | - * @return ExtendedCacheItemInterface |
|
32 | - * @throws PhpfastcacheInvalidArgumentException |
|
33 | - */ |
|
34 | - public function addTag(string $tagName): ExtendedCacheItemInterface; |
|
28 | + /** |
|
29 | + * @param string $tagName |
|
30 | + * |
|
31 | + * @return ExtendedCacheItemInterface |
|
32 | + * @throws PhpfastcacheInvalidArgumentException |
|
33 | + */ |
|
34 | + public function addTag(string $tagName): ExtendedCacheItemInterface; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param array $tagNames |
|
38 | - * |
|
39 | - * @return ExtendedCacheItemInterface |
|
40 | - * @throws PhpfastcacheInvalidArgumentException |
|
41 | - */ |
|
42 | - public function addTags(array $tagNames): ExtendedCacheItemInterface; |
|
36 | + /** |
|
37 | + * @param array $tagNames |
|
38 | + * |
|
39 | + * @return ExtendedCacheItemInterface |
|
40 | + * @throws PhpfastcacheInvalidArgumentException |
|
41 | + */ |
|
42 | + public function addTags(array $tagNames): ExtendedCacheItemInterface; |
|
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * @param array $tags |
|
47 | - * |
|
48 | - * @return ExtendedCacheItemInterface |
|
49 | - * @throws PhpfastcacheInvalidArgumentException |
|
50 | - */ |
|
51 | - public function setTags(array $tags): ExtendedCacheItemInterface; |
|
45 | + /** |
|
46 | + * @param array $tags |
|
47 | + * |
|
48 | + * @return ExtendedCacheItemInterface |
|
49 | + * @throws PhpfastcacheInvalidArgumentException |
|
50 | + */ |
|
51 | + public function setTags(array $tags): ExtendedCacheItemInterface; |
|
52 | 52 | |
53 | - /** |
|
54 | - * @return array |
|
55 | - */ |
|
56 | - public function getTags(): array; |
|
53 | + /** |
|
54 | + * @return array |
|
55 | + */ |
|
56 | + public function getTags(): array; |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param string $separator |
|
60 | - * |
|
61 | - * @return string |
|
62 | - */ |
|
63 | - public function getTagsAsString(string $separator = ', '): string; |
|
58 | + /** |
|
59 | + * @param string $separator |
|
60 | + * |
|
61 | + * @return string |
|
62 | + */ |
|
63 | + public function getTagsAsString(string $separator = ', '): string; |
|
64 | 64 | |
65 | - /** |
|
66 | - * @param string $tagName |
|
67 | - * |
|
68 | - * @return ExtendedCacheItemInterface |
|
69 | - * @throws PhpfastcacheInvalidArgumentException |
|
70 | - */ |
|
71 | - public function removeTag(string $tagName): ExtendedCacheItemInterface; |
|
65 | + /** |
|
66 | + * @param string $tagName |
|
67 | + * |
|
68 | + * @return ExtendedCacheItemInterface |
|
69 | + * @throws PhpfastcacheInvalidArgumentException |
|
70 | + */ |
|
71 | + public function removeTag(string $tagName): ExtendedCacheItemInterface; |
|
72 | 72 | |
73 | - /** |
|
74 | - * @param array $tagNames |
|
75 | - * |
|
76 | - * @return ExtendedCacheItemInterface |
|
77 | - */ |
|
78 | - public function removeTags(array $tagNames): ExtendedCacheItemInterface; |
|
73 | + /** |
|
74 | + * @param array $tagNames |
|
75 | + * |
|
76 | + * @return ExtendedCacheItemInterface |
|
77 | + */ |
|
78 | + public function removeTags(array $tagNames): ExtendedCacheItemInterface; |
|
79 | 79 | |
80 | - /** |
|
81 | - * @return array |
|
82 | - */ |
|
83 | - public function getRemovedTags(): array; |
|
80 | + /** |
|
81 | + * @return array |
|
82 | + */ |
|
83 | + public function getRemovedTags(): array; |
|
84 | 84 | } |
@@ -36,333 +36,333 @@ |
||
36 | 36 | */ |
37 | 37 | trait ItemExtendedTrait |
38 | 38 | { |
39 | - use ClassNamespaceResolverTrait; |
|
40 | - use TaggableCacheItemTrait; |
|
39 | + use ClassNamespaceResolverTrait; |
|
40 | + use TaggableCacheItemTrait; |
|
41 | 41 | |
42 | - /******************** |
|
42 | + /******************** |
|
43 | 43 | * |
44 | 44 | * PSR-6 Extended Methods |
45 | 45 | * |
46 | 46 | *******************/ |
47 | 47 | |
48 | - /** |
|
49 | - * @var ExtendedCacheItemPoolInterface |
|
50 | - */ |
|
51 | - protected $driver; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var string |
|
55 | - */ |
|
56 | - protected $encodedKey; |
|
57 | - |
|
58 | - /** |
|
59 | - * Item constructor. |
|
60 | - * @param ExtendedCacheItemPoolInterface $driver |
|
61 | - * @param $key |
|
62 | - * @throws PhpfastcacheInvalidArgumentException |
|
63 | - */ |
|
64 | - public function __construct(ExtendedCacheItemPoolInterface $driver, $key) |
|
65 | - { |
|
66 | - if (\is_string($key)) { |
|
67 | - $this->key = $key; |
|
68 | - $this->driver = $driver; |
|
69 | - if($driver->getConfig()->isUseStaticItemCaching()){ |
|
70 | - $this->driver->setItem($this); |
|
71 | - } |
|
72 | - $this->expirationDate = new DateTime(); |
|
73 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
74 | - $this->creationDate = new DateTime(); |
|
75 | - $this->modificationDate = new DateTime(); |
|
76 | - } |
|
77 | - } else { |
|
78 | - throw new PhpfastcacheInvalidArgumentTypeException('string', $key); |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function getEncodedKey(): string |
|
86 | - { |
|
87 | - if (!$this->encodedKey) { |
|
88 | - $keyHashFunction = $this->driver->getConfig()->getDefaultKeyHashFunction(); |
|
89 | - |
|
90 | - if ($keyHashFunction) { |
|
91 | - $this->encodedKey = $keyHashFunction($this->getKey()); |
|
92 | - } else { |
|
93 | - $this->encodedKey = $this->getKey(); |
|
94 | - } |
|
95 | - } |
|
96 | - |
|
97 | - return $this->encodedKey; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @return DateTimeInterface |
|
102 | - */ |
|
103 | - public function getExpirationDate(): DateTimeInterface |
|
104 | - { |
|
105 | - return $this->expirationDate; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Alias of expireAt() with forced $expiration param |
|
110 | - * |
|
111 | - * @param DateTimeInterface $expiration |
|
112 | - * The point in time after which the item MUST be considered expired. |
|
113 | - * If null is passed explicitly, a default value MAY be used. If none is set, |
|
114 | - * the value should be stored permanently or for as long as the |
|
115 | - * implementation allows. |
|
116 | - * |
|
117 | - * @return ExtendedCacheItemInterface |
|
118 | - * The called object. |
|
119 | - */ |
|
120 | - public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface |
|
121 | - { |
|
122 | - return $this->expiresAt($expiration); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @return DateTimeInterface |
|
127 | - * @throws PhpfastcacheLogicException |
|
128 | - */ |
|
129 | - public function getCreationDate(): DateTimeInterface |
|
130 | - { |
|
131 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
132 | - return $this->creationDate; |
|
133 | - } |
|
134 | - |
|
135 | - throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @param DateTimeInterface $date |
|
140 | - * @return ExtendedCacheItemInterface |
|
141 | - * @throws PhpfastcacheLogicException |
|
142 | - */ |
|
143 | - public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
144 | - { |
|
145 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
146 | - $this->creationDate = $date; |
|
147 | - return $this; |
|
148 | - } |
|
149 | - |
|
150 | - throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @return DateTimeInterface |
|
155 | - * @throws PhpfastcacheLogicException |
|
156 | - */ |
|
157 | - public function getModificationDate(): DateTimeInterface |
|
158 | - { |
|
159 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
160 | - return $this->modificationDate; |
|
161 | - } |
|
162 | - |
|
163 | - throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @param DateTimeInterface $date |
|
168 | - * @return ExtendedCacheItemInterface |
|
169 | - * @throws PhpfastcacheLogicException |
|
170 | - */ |
|
171 | - public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
172 | - { |
|
173 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
174 | - $this->modificationDate = $date; |
|
175 | - return $this; |
|
176 | - } |
|
177 | - |
|
178 | - throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * @return int |
|
183 | - */ |
|
184 | - public function getTtl(): int |
|
185 | - { |
|
186 | - return \max(0, $this->expirationDate->getTimestamp() - \time()); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * @return bool |
|
191 | - */ |
|
192 | - public function isExpired(): bool |
|
193 | - { |
|
194 | - return $this->expirationDate->getTimestamp() < (new DateTime())->getTimestamp(); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @return bool |
|
199 | - */ |
|
200 | - public function isNull(): bool |
|
201 | - { |
|
202 | - return $this->data === null; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * @return bool |
|
207 | - */ |
|
208 | - public function isEmpty(): bool |
|
209 | - { |
|
210 | - return empty($this->data); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Return the data length: |
|
215 | - * Either the string length if it's a string (binary mode) |
|
216 | - * # or the number of element (count) if it's an array |
|
217 | - * # or the number returned by count() if it's an object implementing \Countable interface |
|
218 | - * # -1 for anything else |
|
219 | - * @return int |
|
220 | - */ |
|
221 | - public function getLength(): int |
|
222 | - { |
|
223 | - switch (\gettype($this->data)) { |
|
224 | - case 'array': |
|
225 | - case 'object': |
|
226 | - if (\is_array($this->data) || $this->data instanceof Countable) { |
|
227 | - return \count($this->data); |
|
228 | - } |
|
229 | - break; |
|
230 | - |
|
231 | - case 'string': |
|
232 | - return \strlen($this->data); |
|
233 | - break; |
|
234 | - } |
|
235 | - |
|
236 | - return -1; |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - /** |
|
241 | - * @param int $step |
|
242 | - * @return ExtendedCacheItemInterface |
|
243 | - * @throws PhpfastcacheInvalidArgumentException |
|
244 | - */ |
|
245 | - public function increment($step = 1): ExtendedCacheItemInterface |
|
246 | - { |
|
247 | - if (\is_int($step)) { |
|
248 | - $this->fetched = true; |
|
249 | - $this->data += $step; |
|
250 | - } else { |
|
251 | - throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
|
252 | - } |
|
253 | - |
|
254 | - return $this; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * @param int $step |
|
259 | - * @return ExtendedCacheItemInterface |
|
260 | - * @throws PhpfastcacheInvalidArgumentException |
|
261 | - */ |
|
262 | - public function decrement($step = 1): ExtendedCacheItemInterface |
|
263 | - { |
|
264 | - if (\is_int($step)) { |
|
265 | - $this->fetched = true; |
|
266 | - $this->data -= $step; |
|
267 | - } else { |
|
268 | - throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
|
269 | - } |
|
270 | - |
|
271 | - return $this; |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * @param array|string $data |
|
276 | - * @return ExtendedCacheItemInterface |
|
277 | - * @throws PhpfastcacheInvalidArgumentException |
|
278 | - */ |
|
279 | - public function append($data): ExtendedCacheItemInterface |
|
280 | - { |
|
281 | - if (\is_array($this->data)) { |
|
282 | - $this->data[] = $data; |
|
283 | - } else { |
|
284 | - if (\is_string($data)) { |
|
285 | - $this->data .= (string)$data; |
|
286 | - } else { |
|
287 | - throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - return $this; |
|
292 | - } |
|
293 | - |
|
294 | - |
|
295 | - /** |
|
296 | - * @param array|string $data |
|
297 | - * @return ExtendedCacheItemInterface |
|
298 | - * @throws PhpfastcacheInvalidArgumentException |
|
299 | - */ |
|
300 | - public function prepend($data): ExtendedCacheItemInterface |
|
301 | - { |
|
302 | - if (\is_array($this->data)) { |
|
303 | - \array_unshift($this->data, $data); |
|
304 | - } else { |
|
305 | - if (\is_string($data)) { |
|
306 | - $this->data = (string)$data . $this->data; |
|
307 | - } else { |
|
308 | - throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
|
309 | - } |
|
310 | - } |
|
311 | - |
|
312 | - return $this; |
|
313 | - } |
|
314 | - |
|
315 | - /** |
|
316 | - * Return the data as a well-formatted string. |
|
317 | - * Any scalar value will be casted to an array |
|
318 | - * @param int $option \json_encode() options |
|
319 | - * @param int $depth \json_encode() depth |
|
320 | - * @return string |
|
321 | - */ |
|
322 | - public function getDataAsJsonString(int $option = 0, int $depth = 512): string |
|
323 | - { |
|
324 | - $data = $this->get(); |
|
325 | - |
|
326 | - if (\is_object($data) || \is_array($data)) { |
|
327 | - $data = \json_encode($data, $option, $depth); |
|
328 | - } else { |
|
329 | - $data = \json_encode([$data], $option, $depth); |
|
330 | - } |
|
331 | - |
|
332 | - return \json_encode($data, $option, $depth); |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Implements \JsonSerializable interface |
|
337 | - * @return mixed |
|
338 | - */ |
|
339 | - #[\ReturnTypeWillChange] // PHP 8.1 compatibility |
|
340 | - public function jsonSerialize() |
|
341 | - { |
|
342 | - return $this->get(); |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * @param ExtendedCacheItemPoolInterface $driverPool |
|
347 | - * @return bool |
|
348 | - * @throws PhpfastcacheInvalidArgumentException |
|
349 | - */ |
|
350 | - public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool |
|
351 | - { |
|
352 | - return $driverPool->getClassNamespace() === $this->getClassNamespace(); |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * @return array |
|
357 | - * @todo Is it still useful ?? |
|
358 | - * |
|
359 | - * Prevent recursions for Debug (php 5.6+) |
|
360 | - */ |
|
361 | - final public function __debugInfo() |
|
362 | - { |
|
363 | - $info = \get_object_vars($this); |
|
364 | - $info['driver'] = 'object(' . \get_class($info['driver']) . ')'; |
|
365 | - |
|
366 | - return $info; |
|
367 | - } |
|
48 | + /** |
|
49 | + * @var ExtendedCacheItemPoolInterface |
|
50 | + */ |
|
51 | + protected $driver; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var string |
|
55 | + */ |
|
56 | + protected $encodedKey; |
|
57 | + |
|
58 | + /** |
|
59 | + * Item constructor. |
|
60 | + * @param ExtendedCacheItemPoolInterface $driver |
|
61 | + * @param $key |
|
62 | + * @throws PhpfastcacheInvalidArgumentException |
|
63 | + */ |
|
64 | + public function __construct(ExtendedCacheItemPoolInterface $driver, $key) |
|
65 | + { |
|
66 | + if (\is_string($key)) { |
|
67 | + $this->key = $key; |
|
68 | + $this->driver = $driver; |
|
69 | + if($driver->getConfig()->isUseStaticItemCaching()){ |
|
70 | + $this->driver->setItem($this); |
|
71 | + } |
|
72 | + $this->expirationDate = new DateTime(); |
|
73 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
74 | + $this->creationDate = new DateTime(); |
|
75 | + $this->modificationDate = new DateTime(); |
|
76 | + } |
|
77 | + } else { |
|
78 | + throw new PhpfastcacheInvalidArgumentTypeException('string', $key); |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function getEncodedKey(): string |
|
86 | + { |
|
87 | + if (!$this->encodedKey) { |
|
88 | + $keyHashFunction = $this->driver->getConfig()->getDefaultKeyHashFunction(); |
|
89 | + |
|
90 | + if ($keyHashFunction) { |
|
91 | + $this->encodedKey = $keyHashFunction($this->getKey()); |
|
92 | + } else { |
|
93 | + $this->encodedKey = $this->getKey(); |
|
94 | + } |
|
95 | + } |
|
96 | + |
|
97 | + return $this->encodedKey; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @return DateTimeInterface |
|
102 | + */ |
|
103 | + public function getExpirationDate(): DateTimeInterface |
|
104 | + { |
|
105 | + return $this->expirationDate; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Alias of expireAt() with forced $expiration param |
|
110 | + * |
|
111 | + * @param DateTimeInterface $expiration |
|
112 | + * The point in time after which the item MUST be considered expired. |
|
113 | + * If null is passed explicitly, a default value MAY be used. If none is set, |
|
114 | + * the value should be stored permanently or for as long as the |
|
115 | + * implementation allows. |
|
116 | + * |
|
117 | + * @return ExtendedCacheItemInterface |
|
118 | + * The called object. |
|
119 | + */ |
|
120 | + public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface |
|
121 | + { |
|
122 | + return $this->expiresAt($expiration); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @return DateTimeInterface |
|
127 | + * @throws PhpfastcacheLogicException |
|
128 | + */ |
|
129 | + public function getCreationDate(): DateTimeInterface |
|
130 | + { |
|
131 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
132 | + return $this->creationDate; |
|
133 | + } |
|
134 | + |
|
135 | + throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @param DateTimeInterface $date |
|
140 | + * @return ExtendedCacheItemInterface |
|
141 | + * @throws PhpfastcacheLogicException |
|
142 | + */ |
|
143 | + public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
144 | + { |
|
145 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
146 | + $this->creationDate = $date; |
|
147 | + return $this; |
|
148 | + } |
|
149 | + |
|
150 | + throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @return DateTimeInterface |
|
155 | + * @throws PhpfastcacheLogicException |
|
156 | + */ |
|
157 | + public function getModificationDate(): DateTimeInterface |
|
158 | + { |
|
159 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
160 | + return $this->modificationDate; |
|
161 | + } |
|
162 | + |
|
163 | + throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @param DateTimeInterface $date |
|
168 | + * @return ExtendedCacheItemInterface |
|
169 | + * @throws PhpfastcacheLogicException |
|
170 | + */ |
|
171 | + public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
172 | + { |
|
173 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
174 | + $this->modificationDate = $date; |
|
175 | + return $this; |
|
176 | + } |
|
177 | + |
|
178 | + throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * @return int |
|
183 | + */ |
|
184 | + public function getTtl(): int |
|
185 | + { |
|
186 | + return \max(0, $this->expirationDate->getTimestamp() - \time()); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * @return bool |
|
191 | + */ |
|
192 | + public function isExpired(): bool |
|
193 | + { |
|
194 | + return $this->expirationDate->getTimestamp() < (new DateTime())->getTimestamp(); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @return bool |
|
199 | + */ |
|
200 | + public function isNull(): bool |
|
201 | + { |
|
202 | + return $this->data === null; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * @return bool |
|
207 | + */ |
|
208 | + public function isEmpty(): bool |
|
209 | + { |
|
210 | + return empty($this->data); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Return the data length: |
|
215 | + * Either the string length if it's a string (binary mode) |
|
216 | + * # or the number of element (count) if it's an array |
|
217 | + * # or the number returned by count() if it's an object implementing \Countable interface |
|
218 | + * # -1 for anything else |
|
219 | + * @return int |
|
220 | + */ |
|
221 | + public function getLength(): int |
|
222 | + { |
|
223 | + switch (\gettype($this->data)) { |
|
224 | + case 'array': |
|
225 | + case 'object': |
|
226 | + if (\is_array($this->data) || $this->data instanceof Countable) { |
|
227 | + return \count($this->data); |
|
228 | + } |
|
229 | + break; |
|
230 | + |
|
231 | + case 'string': |
|
232 | + return \strlen($this->data); |
|
233 | + break; |
|
234 | + } |
|
235 | + |
|
236 | + return -1; |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * @param int $step |
|
242 | + * @return ExtendedCacheItemInterface |
|
243 | + * @throws PhpfastcacheInvalidArgumentException |
|
244 | + */ |
|
245 | + public function increment($step = 1): ExtendedCacheItemInterface |
|
246 | + { |
|
247 | + if (\is_int($step)) { |
|
248 | + $this->fetched = true; |
|
249 | + $this->data += $step; |
|
250 | + } else { |
|
251 | + throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
|
252 | + } |
|
253 | + |
|
254 | + return $this; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * @param int $step |
|
259 | + * @return ExtendedCacheItemInterface |
|
260 | + * @throws PhpfastcacheInvalidArgumentException |
|
261 | + */ |
|
262 | + public function decrement($step = 1): ExtendedCacheItemInterface |
|
263 | + { |
|
264 | + if (\is_int($step)) { |
|
265 | + $this->fetched = true; |
|
266 | + $this->data -= $step; |
|
267 | + } else { |
|
268 | + throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
|
269 | + } |
|
270 | + |
|
271 | + return $this; |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * @param array|string $data |
|
276 | + * @return ExtendedCacheItemInterface |
|
277 | + * @throws PhpfastcacheInvalidArgumentException |
|
278 | + */ |
|
279 | + public function append($data): ExtendedCacheItemInterface |
|
280 | + { |
|
281 | + if (\is_array($this->data)) { |
|
282 | + $this->data[] = $data; |
|
283 | + } else { |
|
284 | + if (\is_string($data)) { |
|
285 | + $this->data .= (string)$data; |
|
286 | + } else { |
|
287 | + throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + return $this; |
|
292 | + } |
|
293 | + |
|
294 | + |
|
295 | + /** |
|
296 | + * @param array|string $data |
|
297 | + * @return ExtendedCacheItemInterface |
|
298 | + * @throws PhpfastcacheInvalidArgumentException |
|
299 | + */ |
|
300 | + public function prepend($data): ExtendedCacheItemInterface |
|
301 | + { |
|
302 | + if (\is_array($this->data)) { |
|
303 | + \array_unshift($this->data, $data); |
|
304 | + } else { |
|
305 | + if (\is_string($data)) { |
|
306 | + $this->data = (string)$data . $this->data; |
|
307 | + } else { |
|
308 | + throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
|
309 | + } |
|
310 | + } |
|
311 | + |
|
312 | + return $this; |
|
313 | + } |
|
314 | + |
|
315 | + /** |
|
316 | + * Return the data as a well-formatted string. |
|
317 | + * Any scalar value will be casted to an array |
|
318 | + * @param int $option \json_encode() options |
|
319 | + * @param int $depth \json_encode() depth |
|
320 | + * @return string |
|
321 | + */ |
|
322 | + public function getDataAsJsonString(int $option = 0, int $depth = 512): string |
|
323 | + { |
|
324 | + $data = $this->get(); |
|
325 | + |
|
326 | + if (\is_object($data) || \is_array($data)) { |
|
327 | + $data = \json_encode($data, $option, $depth); |
|
328 | + } else { |
|
329 | + $data = \json_encode([$data], $option, $depth); |
|
330 | + } |
|
331 | + |
|
332 | + return \json_encode($data, $option, $depth); |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Implements \JsonSerializable interface |
|
337 | + * @return mixed |
|
338 | + */ |
|
339 | + #[\ReturnTypeWillChange] // PHP 8.1 compatibility |
|
340 | + public function jsonSerialize() |
|
341 | + { |
|
342 | + return $this->get(); |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * @param ExtendedCacheItemPoolInterface $driverPool |
|
347 | + * @return bool |
|
348 | + * @throws PhpfastcacheInvalidArgumentException |
|
349 | + */ |
|
350 | + public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool |
|
351 | + { |
|
352 | + return $driverPool->getClassNamespace() === $this->getClassNamespace(); |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * @return array |
|
357 | + * @todo Is it still useful ?? |
|
358 | + * |
|
359 | + * Prevent recursions for Debug (php 5.6+) |
|
360 | + */ |
|
361 | + final public function __debugInfo() |
|
362 | + { |
|
363 | + $info = \get_object_vars($this); |
|
364 | + $info['driver'] = 'object(' . \get_class($info['driver']) . ')'; |
|
365 | + |
|
366 | + return $info; |
|
367 | + } |
|
368 | 368 | } |