Skip to content

Commit 94fc00c

Browse files
authored
Release 10.4.0 (#456)
* updating cache
1 parent 811fabf commit 94fc00c

File tree

4 files changed

+63
-138
lines changed

4 files changed

+63
-138
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44

55
This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a CHANGELOG](https://keepachangelog.com/).
66

7+
## [10.4.0]
8+
9+
### Changed
10+
11+
- Internal caching mechanism for manifests.
12+
- All caching is now stored in the `eightshift` folder in the root of the theme or plugin.
13+
- All manifest caching is now stored on the disk and not in the transients.
14+
- `Render` method for faster rendering time.
15+
716
## [10.3.1]
817

918
### Added
@@ -859,6 +868,7 @@ Init setup
859868
- Gutenberg Blocks Registration.
860869
- Assets Manifest data.
861870

871+
[10.4.0]: https://github.com/infinum/eightshift-libs/compare/10.3.1...10.4.0
862872
[10.3.1]: https://github.com/infinum/eightshift-libs/compare/10.3.0...10.3.1
863873
[10.3.0]: https://github.com/infinum/eightshift-libs/compare/10.2.0...10.3.0
864874
[10.2.0]: https://github.com/infinum/eightshift-libs/compare/10.1.0...10.2.0

src/Cache/AbstractManifestCache.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,6 @@ abstract public function getCacheName(): string;
5656
*/
5757
abstract public function getVersion(): string;
5858

59-
/**
60-
* Get cache duration.
61-
* Default is 0 = infinite.
62-
*
63-
* @return int Cache duration.
64-
*/
65-
public function getDuration(): int
66-
{
67-
return 0;
68-
}
69-
7059
/**
7160
* Set all cache.
7261
*
@@ -78,7 +67,6 @@ public function setAllCache(): void
7867
$this->getCacheBuilder(),
7968
$this->getCacheName(),
8069
$this->getVersion(),
81-
$this->getDuration()
8270
);
8371
}
8472

src/Helpers/CacheTrait.php

Lines changed: 15 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ trait CacheTrait
4747
private static $version = '';
4848

4949
/**
50-
* Cache duration.
50+
* Namespace for blocks.
5151
*
52-
* @var int
52+
* @var string
5353
*/
54-
private static $duration = 0;
54+
private static $blocksNamespace = '';
5555

5656
/**
57-
* Namespace for blocks.
57+
* Cache file name.
5858
*
5959
* @var string
6060
*/
61-
private static $blocksNamespace = '';
61+
private static $cacheFileName = '';
6262

6363
// -----------------------------------------------------
6464
// CACHE
@@ -70,26 +70,19 @@ trait CacheTrait
7070
* @param array<mixed> $cacheBuilder Cache builder.
7171
* @param string $cacheName Cache name.
7272
* @param string $version Cache version.
73-
* @param int $duration Cache duration.
7473
*
7574
* @return void
7675
*/
7776
public static function setCacheDetails(
7877
array $cacheBuilder,
7978
string $cacheName,
80-
string $version,
81-
int $duration,
79+
string $version
8280
): void {
8381
self::$cacheBuilder = $cacheBuilder;
8482
self::$cacheName = $cacheName;
8583
self::$version = $version;
86-
self::$duration = $duration;
87-
88-
if (!Helpers::isCacheVersionValid() || !Helpers::shouldCache()) {
89-
Helpers::deleteAllCache();
90-
}
84+
self::$cacheFileName = \str_replace(' ', '', "{$cacheName}Manifests{$version}.json");
9185

92-
Helpers::setCacheVersion();
9386
Helpers::setAllCache();
9487
}
9588

@@ -109,30 +102,19 @@ public static function setAllCache(): void
109102
return;
110103
}
111104

112-
$data = \get_transient(Helpers::getCacheTransientName());
105+
$cacheFile = Helpers::getEightshiftOutputPath(self::$cacheFileName);
113106

114-
if (!$data) {
115-
$data = self::getAllManifests();
116-
117-
\set_transient(
118-
Helpers::getCacheTransientName(),
119-
\wp_json_encode($data),
120-
self::$duration
121-
);
122-
123-
self::$cache = $data;
107+
if (\file_exists($cacheFile)) {
108+
$handle = \fopen($cacheFile, 'r');
109+
$output = \stream_get_contents($handle);
124110

111+
self::$cache = \json_decode($output, true);
125112
return;
126113
}
127114

128-
$data = \json_decode($data, true);
129-
if (!$data) {
130-
self::$cache = [];
131-
return;
132-
}
133-
115+
$data = self::getAllManifests();
116+
\file_put_contents($cacheFile, \wp_json_encode($data)); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
134117
self::$cache = $data;
135-
136118
return;
137119
}
138120

@@ -156,77 +138,6 @@ public static function getCacheName(): string
156138
return self::$cacheName;
157139
}
158140

159-
/**
160-
* Get cache transient name.
161-
*
162-
* @param string $type Type of the cache.
163-
*
164-
* @return string
165-
*/
166-
public static function getCacheTransientName(string $type = ''): string
167-
{
168-
$name = AbstractManifestCache::TRANSIENT_PREFIX_NAME . '_' . self::$cacheName;
169-
170-
if (!$type) {
171-
return $name;
172-
}
173-
174-
return "{$name}_{$type}";
175-
}
176-
177-
/**
178-
* Check if cache version is valid.
179-
*
180-
* @return bool
181-
*/
182-
public static function isCacheVersionValid(): bool
183-
{
184-
return self::getCacheVersion() === self::$version;
185-
}
186-
187-
/**
188-
* Set version cache.
189-
*
190-
* @return void
191-
*/
192-
public static function setCacheVersion(): void
193-
{
194-
$name = self::getCacheTransientName(AbstractManifestCache::VERSION_KEY);
195-
196-
$cache = \get_transient($name);
197-
198-
if (!$cache) {
199-
\set_transient($name, self::$version);
200-
}
201-
}
202-
203-
/**
204-
* Get cache version.
205-
*
206-
* @return string
207-
*/
208-
public static function getCacheVersion(): string
209-
{
210-
$cache = \get_transient(self::getCacheTransientName(AbstractManifestCache::VERSION_KEY)) ?: ''; // phpcs:ignore WordPress.PHP.DisallowShortTernary.Found
211-
212-
if (!$cache) {
213-
self::setCacheVersion();
214-
}
215-
216-
return $cache;
217-
}
218-
219-
/**
220-
* Unset all manifest cache.
221-
*
222-
* @return void
223-
*/
224-
public static function deleteAllCache(): void
225-
{
226-
\delete_transient(self::getCacheTransientName());
227-
\delete_transient(self::getCacheTransientName(AbstractManifestCache::VERSION_KEY));
228-
}
229-
230141
/**
231142
* Check if we should cache the service classes.
232143
*
@@ -236,7 +147,7 @@ public static function shouldCache(): bool
236147
{
237148
return !(
238149
(\defined('WP_ENVIRONMENT_TYPE') &&
239-
(\WP_ENVIRONMENT_TYPE === 'development' || \WP_ENVIRONMENT_TYPE === 'local')) ||
150+
(\WP_ENVIRONMENT_TYPE === 'development' || \WP_ENVIRONMENT_TYPE === 'local')) ||
240151
\defined('WP_CLI')
241152
);
242153
}

src/Helpers/Helpers.php

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ class Helpers
9898
'pluginRoot',
9999
];
100100

101+
/**
102+
* Cached flipped array for faster lookups.
103+
*
104+
* @var array<string, int>|null
105+
*/
106+
private static ?array $allowedNamesFlipped = null;
107+
101108
/**
102109
* Renders a components and (optionally) passes some attributes to it.
103110
*
@@ -120,62 +127,70 @@ public static function render(
120127
string $renderPrefixPath = '',
121128
string $renderContent = ''
122129
): string {
123-
$manifest = [];
124-
130+
// Set default path name if not provided.
125131
if (!$renderPathName) {
126132
$renderPathName = Helpers::getConfigUseLegacyComponents() ? 'components' : 'blocks';
127133
}
128134

135+
// Validate path name early.
136+
if (self::$allowedNamesFlipped === null) {
137+
self::$allowedNamesFlipped = \array_flip(self::PROJECT_RENDER_ALLOWED_NAMES);
138+
}
139+
140+
if (!isset(self::$allowedNamesFlipped[$renderPathName])) {
141+
throw InvalidPath::wrongOrNotAllowedParentPathException($renderPathName, \implode(', ', self::PROJECT_RENDER_ALLOWED_NAMES));
142+
}
143+
144+
// Initialize variables.
145+
$manifest = [];
146+
$renderPath = '';
147+
$componentName = '';
148+
149+
// Extract component/block name once if needed.
150+
if ($renderPrefixPath && ($renderPathName === 'components' || $renderPathName === 'blocks')) {
151+
$componentName = \explode(\DIRECTORY_SEPARATOR, $renderPrefixPath)[0] ?? '';
152+
}
153+
154+
// Build path and get manifest based on path type.
129155
switch ($renderPathName) {
130156
case 'components':
131-
$componentName = \explode(\DIRECTORY_SEPARATOR, $renderPrefixPath)[0] ?? '';
132-
133157
if ($componentName) {
134158
$renderPath = Helpers::getProjectPaths('components', [$renderPrefixPath, "{$renderName}.php"]);
135159
$manifest = Helpers::getComponent($componentName);
136160
} else {
137161
$renderPath = Helpers::getProjectPaths('components', [$renderPrefixPath, $renderName, "{$renderName}.php"]);
138162
$manifest = Helpers::getComponent($renderName);
139163
}
140-
141-
unset($componentName);
142-
143164
break;
144165
case 'wrapper':
145-
$manifest = Helpers::getWrapper();
146166
$renderPath = Helpers::getProjectPaths('wrapper', ["{$renderName}.php"]);
167+
$manifest = Helpers::getWrapper();
147168
break;
148169
case 'blocks':
149-
$blockName = \explode(\DIRECTORY_SEPARATOR, $renderPrefixPath)[0] ?? '';
150-
151-
if ($blockName) {
170+
if ($componentName) {
152171
$renderPath = Helpers::getProjectPaths('blocks', [$renderPrefixPath, "{$renderName}.php"]);
153-
$manifest = Helpers::getBlock($blockName);
172+
$manifest = Helpers::getBlock($componentName);
154173
} else {
155174
$renderPath = Helpers::getProjectPaths('blocks', [$renderPrefixPath, $renderName, "{$renderName}.php"]);
156175
$manifest = Helpers::getBlock($renderName);
157176
}
158-
159-
unset($blockName);
160177
break;
161178
default:
162179
$renderPath = Helpers::getProjectPaths('', [$renderPathName, $renderPrefixPath, "{$renderName}.php"]);
163180
break;
164181
}
165182

166-
if (!isset(\array_flip(self::PROJECT_RENDER_ALLOWED_NAMES)[$renderPathName])) {
167-
throw InvalidPath::wrongOrNotAllowedParentPathException($renderPathName, \implode(', ', self::PROJECT_RENDER_ALLOWED_NAMES));
168-
}
169-
183+
// Check if file exists.
170184
if (!\file_exists($renderPath)) {
171185
throw InvalidPath::missingFileException($renderPath);
172186
}
173187

174-
// Merge default attributes with the component attributes.
188+
// Merge default attributes with the component attributes if needed.
175189
if ($renderUseComponentDefaults && !empty($manifest)) {
176190
$renderAttributes = Helpers::getDefaultRenderAttributes($manifest, $renderAttributes);
177191
}
178192

193+
// Start output buffering and include the file.
179194
\ob_start();
180195

181196
// Allowed variables are $attributes, $renderAttributes, $renderContent, $renderPath, $manifest, $globalManifest.
@@ -184,15 +199,16 @@ public static function render(
184199

185200
unset(
186201
$renderName,
202+
$renderAttributes,
187203
$renderPathName,
188204
$renderUseComponentDefaults,
189205
$renderPrefixPath,
206+
$componentName,
190207
);
191208

192209
include $renderPath;
193210

194211
unset(
195-
$renderAttributes,
196212
$attributes,
197213
$renderContent,
198214
$renderPath,
@@ -226,7 +242,7 @@ public static function getProjectPaths(string $type = '', array|string $suffix =
226242
case 'root':
227243
return self::joinPaths([$projectRoot, ...$suffix]);
228244
case 'eightshift':
229-
return self::joinPaths([$projectRoot, 'eightshift', ...$suffix]);
245+
return self::joinPaths([$root, 'eightshift', ...$suffix]);
230246
case 'src':
231247
return self::joinPaths([$root, 'src', ...$suffix]);
232248
case 'public':
@@ -285,7 +301,7 @@ public static function getEightshiftOutputPath($fileName = ''): string
285301
}
286302

287303
if ($fileName) {
288-
return $filePath . \DIRECTORY_SEPARATOR . $fileName;
304+
return "{$filePath}{$fileName}";
289305
}
290306

291307
return $filePath;

0 commit comments

Comments
 (0)