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 344b935 commit af17312
Show file tree
Hide file tree
Showing 12 changed files with 551 additions and 95 deletions.
12 changes: 8 additions & 4 deletions src/Cache/AbstractManifestCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ public function getManifestCacheTopItem(string $key, string $cacheType = self::T
'key' => $key,
];

if (\defined('WP_ENVIRONMENT_TYPE') && \WP_ENVIRONMENT_TYPE === 'development') {
$data = $this->getAllManifests()[$key] ?? [];
} else {
$data = [];

if (\defined('WP_ENVIRONMENT_TYPE') && \WP_ENVIRONMENT_TYPE !== 'development') {
$data = $this->getCache($cacheType)[$key] ?? [];
}

if (!$data) {
$data = $this->getAllManifests()[$key] ?? [];
}

if (!$data) {
return $output;
}
Expand Down Expand Up @@ -218,7 +222,7 @@ protected function getCacheBuilder(): array
'multiple' => false,
],
self::ASSETS_KEY => [
'path' => 'themePath',
'path' => 'themeRoot',
'fileName' => "public{$sep}manifest.json",
'multiple' => false,
],
Expand Down
6 changes: 4 additions & 2 deletions src/Cli/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
use EightshiftLibs\Cli\ParentGroups\CliBlocks;
use EightshiftLibs\Cli\ParentGroups\CliInit;
use EightshiftLibs\Columns\Media\WebPMediaColumnCli;
use EightshiftLibs\Config\ConfigCli;
use EightshiftLibs\Config\ConfigThemeCli;
use EightshiftLibs\Config\ConfigPluginCli;
use EightshiftLibs\ConfigProject\ConfigProjectCli;
use EightshiftLibs\Setup\PluginManageCli;
use EightshiftLibs\View\EscapedViewCli;
Expand Down Expand Up @@ -102,7 +103,8 @@ class Cli
ReusableBlocksHeaderFooterCli::class,
AnalyticsGdprCli::class,
WebPMediaColumnCli::class,
ConfigCli::class,
ConfigThemeCli::class,
ConfigPluginCli::class,
ConfigProjectCli::class,
AcfMetaCli::class,
PostTypeCli::class,
Expand Down
100 changes: 83 additions & 17 deletions src/Config/AbstractConfigData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,103 @@
/**
* The project config class.
*/
abstract class AbstractConfigData implements ConfigDataInterface
abstract class AbstractConfig
{
/**
* Return project absolute path.
* Get the plugin version.
*
* If used in a theme use get_template_directory() and in case it's used in a plugin use __DIR__.
* @return string
*/
public static function getPluginVersion(): string
{
return static::getPluginDetails()['Version'] ?? esc_html('1.0.0');
}

/**
* Get the plugin version.
*
* @return string
*/
public static function getPluginName(): string
{
return static::getPluginDetails()['Name'] ?? esc_html('Plugin');
}

/**
* Get the plugin text domain.
*
* @return string
*/
public static function getPluginTextDomain(): string
{
return static::getPluginDetails()['TextDomain'] ?? esc_html('PluginTextDomain');
}

/**
* Get the theme version.
*
* @return string
*/
public static function getThemeVersion(): string
{
return \wp_get_theme()->get('Version');
}

/**
* Get the theme name.
*
* @return string
*/
public static function getThemeName(): string
{
return \wp_get_theme()->get('Name');
}

/**
* Get the theme text domain.
*
* @return string
*/
public static function getThemeTextDomain(): string
{
return \wp_get_theme()->get('TextDomain');
}

/**
* Return projects absolute path.
*
* @param string $path Additional path to add to project path.
*
* @throws InvalidPath If an invalid URI was passed.
* @throws InvalidPath If the path is not readable.
*
* @return string Valid URI.
* @return string
*/
public static function getProjectPath(string $path = ''): string
{
$locations = [
Components::getProjectPaths('root', $path),
\trailingslashit(\get_stylesheet_directory()) . $path,
\trailingslashit(\get_template_directory()) . $path,
];
$fullPath = Components::getProjectPaths('themeRoot') . ltrim($path, \DIRECTORY_SEPARATOR);

foreach ($locations as $location) {
if (\is_readable($location)) {
return $location;
}
if (!\is_readable($fullPath)) {
throw InvalidPath::missingDirectoryException($fullPath);
}

if (!\is_readable($path)) {
throw InvalidPath::fromUri($path);
return $fullPath;
}

/**
* Get the plugin details.
*
* @return array<string, string>
*/
private static function getPluginDetails(): array
{
if (!\function_exists('get_plugin_data')) {
require_once(\ABSPATH . 'wp-admin/includes/plugin.php');
}

return $path;
$path = Components::getProjectPaths('pluginRoot');

$name = \basename($path);

return \get_plugin_data("{$path}{$name}.php");
}
}
44 changes: 0 additions & 44 deletions src/Config/ConfigDataInterface.php

This file was deleted.

113 changes: 113 additions & 0 deletions src/Config/ConfigPluginCli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

/**
* Class that registers WPCLI command for Config Plugin.
*
* @package EightshiftLibs\Config
*/

declare(strict_types=1);

namespace EightshiftLibs\Config;

use EightshiftLibs\Cli\AbstractCli;
use EightshiftLibs\Cli\ParentGroups\CliCreate;
use EightshiftLibs\Helpers\Components;

/**
* Class ConfigPluginCli
*/
class ConfigPluginCli extends AbstractCli
{
/**
* Get WPCLI command parent name
*
* @return string
*/
public function getCommandParentName(): string
{
return CliCreate::COMMAND_NAME;
}

/**
* Get WPCLI command name
*
* @return string
*/
public function getCommandName(): string
{
return 'config-plugin';
}

/**
* Define default arguments.
*
* @return array<string, int|string|boolean>
*/
public function getDefaultArgs(): array
{
return [
'routes_version' => '1',
];
}

/**
* Get WPCLI command doc
*
* @return array<string, array<int, array<string, bool|string>>|string>
*/
public function getDoc(): array
{
return [
'shortdesc' => 'Create plugin config service class.',
'synopsis' => [
[
'type' => 'assoc',
'name' => 'routes_version',
'description' => 'Define project REST version.',
'optional' => true,
'default' => $this->getDefaultArg('routes_version'),
],
],
'longdesc' => $this->prepareLongDesc("
## USAGE
Used to create plugin config class with settings like project name, version, REST-API name/version, etc.
## EXAMPLES
# Create service class:
$ wp {$this->commandParentName} {$this->getCommandParentName()} {$this->getCommandName()}
## RESOURCES
Service class will be created from this example:
https://github.com/infinum/eightshift-libs/blob/develop/src/Config/ConfigPluginExample.php
"),
];
}

/* @phpstan-ignore-next-line */
public function __invoke(array $args, array $assocArgs)
{
$this->getIntroText($assocArgs);

// Get Props.
$routesVersion = $this->getArg($assocArgs, 'routes_version');

$className = $this->getClassShortName();

// Read the template contents, and replace the placeholders with provided variables.
$class = $this->getExampleTemplate(__DIR__, $className)
->renameClassName($className)
->renameNamespace($assocArgs)
->renameUse($assocArgs);

if (!empty($routesVersion)) {
$class->searchReplaceString($this->getArgTemplate('routes_version'), $routesVersion);
}

// Output final class to new file/folder and finish.
$class->outputWrite(Components::getProjectPaths('srcDestination', 'Config'), "{$className}.php", $assocArgs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

namespace EightshiftBoilerplate\Config;

use EightshiftLibs\Config\AbstractConfigData;
use EightshiftLibs\Config\AbstractConfig;

/**
* The project config class.
*/
class ConfigExample extends AbstractConfigData
class ConfigPluginExample extends AbstractConfig
{
/**
* Method that returns project name.
Expand All @@ -27,7 +27,7 @@ class ConfigExample extends AbstractConfigData
*/
public static function getProjectName(): string
{
return \wp_get_theme('', \dirname(__DIR__, 3))->get('TextDomain');
return self::getPluginName();
}

/**
Expand All @@ -37,7 +37,7 @@ public static function getProjectName(): string
*/
public static function getProjectVersion(): string
{
return \wp_get_theme('', \dirname(__DIR__, 3))->get('Version');
return self::getPluginVersion();
}

/**
Expand Down
Loading

0 comments on commit af17312

Please sign in to comment.