Skip to content

Commit

Permalink
updating helpers from the PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
iruzevic committed Apr 27, 2024
1 parent af17312 commit dd9acd1
Show file tree
Hide file tree
Showing 22 changed files with 217 additions and 181 deletions.
17 changes: 12 additions & 5 deletions src/Blocks/AbstractBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class AbstractBlocks implements ServiceInterface, RenderableBlockInterf
*
* @var array<string, array<string, mixed>>
*/
const BLOCKS_BUILDER = [
private const BLOCKS_BUILDER = [
AbstractManifestCache::BLOCKS_KEY => [
'multiple' => true,
'validation' => [
Expand Down Expand Up @@ -86,6 +86,8 @@ abstract class AbstractBlocks implements ServiceInterface, RenderableBlockInterf

/**
* Instance variable for manifest cache.
*
* @var ManifestCacheInterface
*/
protected $manifestCache;

Expand All @@ -94,7 +96,8 @@ abstract class AbstractBlocks implements ServiceInterface, RenderableBlockInterf
*
* @param ManifestCacheInterface $manifestCache Inject manifest cache.
*/
public function __construct(ManifestCacheInterface $manifestCache) {
public function __construct(ManifestCacheInterface $manifestCache)
{
$this->manifestCache = $manifestCache;
}

Expand Down Expand Up @@ -233,7 +236,7 @@ public function registerBlocks(): void
* @param array<string, mixed> $attributes Array of attributes as defined in block's manifest.json.
* @param string $innerBlockContent Block's content if using inner blocks.
*
* @throws InvalidBlock Throws error if block view is missing.
* @throws InvalidPath Throws error if file doesn't exist.
*
* @return string Html template for block.
*/
Expand Down Expand Up @@ -310,7 +313,7 @@ public function getCustomCategory(array $categories, WP_Block_Editor_Context $bl
* @param array<string, mixed> $attributes Attributes array to pass in template.
* @param string|null $innerBlockContent If using inner blocks content pass the data.
*
* @throws InvalidBlock Throws an error if wrapper file doesn't exist.
* @throws InvalidPath Throws error if file doesn't exist.
*
* @return void Includes an HTML view, or throws an error if the view is missing.
*/
Expand Down Expand Up @@ -402,6 +405,8 @@ private function getManifest(string $type): array
*
* @param string $type Type of the manifest.
*
* @throws InvalidManifest If the manifest is missing or has an error.
*
* @return array<array<string, mixed>>
*/
private function getManifestItems(string $type): array
Expand All @@ -424,14 +429,16 @@ private function getManifestItems(string $type): array
}
}

return array_values($data) ?? [];
return \array_values($data);
}

/**
* Get single item from the manifest.
*
* @param string $type Type of the manifest.
*
* @throws InvalidManifest If the manifest is missing or has an error.
*
* @return array<string, mixed>
*/
private function getManifestItem(string $type): array
Expand Down
93 changes: 80 additions & 13 deletions src/Cache/AbstractManifestCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,96 @@ abstract class AbstractManifestCache implements ManifestCacheInterface
{
private const TRANSIENT_NAME = 'eightshift_manifest_cache_';

/**
* Key for blocks.
*
* @var string
*/
public const BLOCKS_KEY = 'blocks';

/**
* Key for components.
*
* @var string
*/
public const COMPONENTS_KEY = 'components';

/**
* Key for variations.
*
* @var string
*/
public const VARIATIONS_KEY = 'variations';

/**
* Key for wrapper.
*
* @var string
*/
public const WRAPPER_KEY = 'wrapper';

/**
* Key for settings.
*
* @var string
*/
public const SETTINGS_KEY = 'settings';

/**
* Key for assets.
*
* @var string
*/
public const ASSETS_KEY = 'assets';

/**
* Key for config.
*
* @var string
*/
public const CONFIG_KEY = 'config';

/**
* Key for styles.
*
* @var string
*/
public const STYLES_KEY = 'styles';

/**
* Key for paths.
*
* @var string
*/
public const PATHS_KEY = 'paths';

/**
* Key for cache type blocks.
*
* @var string
*/
public const TYPE_BLOCKS = 'blocks';

/**
* Key for cache type assets.
*
* @var string
*/
public const TYPE_ASSETS = 'assets';

/**
* Blocks namespace.
*
* @var string
*/
private $blocksNamespace = '';

/**
* Get cache name.
*
* @return string Cache name.
*/
public abstract function getCacheName(): string;
abstract public function getCacheName(): string;

/**
* Get cache duration.
Expand Down Expand Up @@ -118,9 +187,7 @@ public function getManifestCacheSubItem(string $key, string $name, string $cache
}

/**
* Set cache.
*
* @param string $type Type of the cache.
* Set all cache.
*
* @return void
*/
Expand Down Expand Up @@ -153,7 +220,7 @@ protected function setCache(string $type = self::TYPE_BLOCKS): void
*
* @param string $type Type of the cache.
*
* @return array<string, array> Array of cache.
* @return array<string, array<mixed>> Array of cache.
*/
protected function getCache(string $type = self::TYPE_BLOCKS): array
{
Expand All @@ -169,7 +236,7 @@ protected function getCache(string $type = self::TYPE_BLOCKS): array
/**
* Unset cache.
*
* @return array<string, array> Array of cache.
* @param string $type Type of the cache.
*
* @return void
*/
Expand All @@ -181,7 +248,7 @@ protected function deleteCache(string $type = self::TYPE_BLOCKS): void
/**
* Get cache builder.
*
* @return array<string, array> Array of cache builder.
* @return array<string, array<string, mixed>> Array of cache builder.
*/
protected function getCacheBuilder(): array
{
Expand Down Expand Up @@ -232,7 +299,7 @@ protected function getCacheBuilder(): array
/**
* Get all manifests from the paths.
*
* @return array<string, array> Array of manifests.
* @return array<string, array<mixed>> Array of manifests.
*/
private function getAllManifests(): array
{
Expand Down Expand Up @@ -266,7 +333,7 @@ private function getItem(string $path, array $data, string $parent): array
return [];
}

$file = \file_get_contents($path);
$file = \file_get_contents($path); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents

if (!$file) {
return [];
Expand All @@ -285,7 +352,7 @@ private function getItem(string $path, array $data, string $parent): array
if (isset($fileDecoded[$key])) {
continue;
}

switch ($type) {
case 'array':
$fileDecoded[$key] = [];
Expand All @@ -302,7 +369,7 @@ private function getItem(string $path, array $data, string $parent): array

switch ($parent) {
case self::BLOCKS_KEY:
$namespace = $this->blocksNamespace ?? '';
$namespace = $this->blocksNamespace;

if ($namespace) {
$fileDecoded['namespace'] = $namespace;
Expand All @@ -324,7 +391,7 @@ private function getItem(string $path, array $data, string $parent): array
* @param array<mixed> $data Data array.
* @param string $parent Parent key.
*
* @return array<string, array> Array of items.
* @return array<string, array<mixed>> Array of items.
*/
private function geItems(string $path, array $data, string $parent): array
{
Expand Down Expand Up @@ -367,7 +434,7 @@ private function getFullPath($type, $name = ''): string
$path = $data['path'] ?? '';
$fileName = $data['fileName'] ?? '';

$path = Components::getProjectPaths($path) ?? $path;
$path = Components::getProjectPaths($path);

if (!$name) {
return \rtrim($path, $sep) . "{$sep}{$fileName}";
Expand Down
32 changes: 16 additions & 16 deletions src/Cache/ManifestCacheExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
* @package EightshiftBoilerplate\Cache
*/

declare(strict_types=1);
declare(strict_types=1);

namespace EightshiftBoilerplate\Cache;

use EightshiftLibs\Cache\AbstractManifestCache;

/**
* The project config class.
*/
class ManifestCache extends AbstractManifestCache
{
namespace EightshiftBoilerplate\Cache;

use EightshiftBoilerplate\Config\Config;
use EightshiftLibs\Cache\AbstractManifestCache;

/**
* The project config class.
*/
class ManifestCache extends AbstractManifestCache
{
/**
* Get cache name.
*
* @return string Cache name.
*/
public function getCacheName(): string
{
return 'es_forms'; // TODO: Change this
}
}

public function getCacheName(): string
{
return Config::getProjectTextDomain();
}
}
3 changes: 1 addition & 2 deletions src/Cli/AbstractCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace EightshiftLibs\Cli;

use EightshiftLibs\Exception\InvalidBlock;
use EightshiftLibs\Exception\InvalidPath;
use EightshiftLibs\Helpers\Components;
use FilesystemIterator;
Expand Down Expand Up @@ -925,7 +924,7 @@ public function prepareArgsManual(array $args): string
* @param string $source Source path.
* @param string $destination Destination path.
*
* @throws InvalidBlock If block file is missing.
* @throws InvalidPath Exception in case the source path is missing.
*
* @return void
*/
Expand Down
10 changes: 5 additions & 5 deletions src/Config/AbstractConfigData.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class AbstractConfig
*/
public static function getPluginVersion(): string
{
return static::getPluginDetails()['Version'] ?? esc_html('1.0.0');
return static::getPluginDetails()['Version'] ?? \esc_html('1.0.0');
}

/**
Expand All @@ -35,7 +35,7 @@ public static function getPluginVersion(): string
*/
public static function getPluginName(): string
{
return static::getPluginDetails()['Name'] ?? esc_html('Plugin');
return static::getPluginDetails()['Name'] ?? \esc_html('Plugin');
}

/**
Expand All @@ -45,7 +45,7 @@ public static function getPluginName(): string
*/
public static function getPluginTextDomain(): string
{
return static::getPluginDetails()['TextDomain'] ?? esc_html('PluginTextDomain');
return static::getPluginDetails()['TextDomain'] ?? \esc_html('PluginTextDomain');
}

/**
Expand Down Expand Up @@ -89,7 +89,7 @@ public static function getThemeTextDomain(): string
*/
public static function getProjectPath(string $path = ''): string
{
$fullPath = Components::getProjectPaths('themeRoot') . ltrim($path, \DIRECTORY_SEPARATOR);
$fullPath = Components::getProjectPaths('themeRoot') . \ltrim($path, \DIRECTORY_SEPARATOR);

if (!\is_readable($fullPath)) {
throw InvalidPath::missingDirectoryException($fullPath);
Expand All @@ -103,7 +103,7 @@ public static function getProjectPath(string $path = ''): string
*
* @return array<string, string>
*/
private static function getPluginDetails(): array
protected static function getPluginDetails(): array
{
if (!\function_exists('get_plugin_data')) {
require_once(\ABSPATH . 'wp-admin/includes/plugin.php');
Expand Down
10 changes: 10 additions & 0 deletions src/Config/ConfigPluginExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public static function getProjectVersion(): string
return self::getPluginVersion();
}

/**
* Method that returns project text domain.
*
* Generally used for caching and translations.
*/
public static function getProjectTextDomain(): string
{
return self::getPluginTextDomain();
}

/**
* Method that returns project REST-API namespace.
*
Expand Down
Loading

0 comments on commit dd9acd1

Please sign in to comment.