diff --git a/src/Blocks/AbstractBlocksCli.php b/src/Blocks/AbstractBlocksCli.php index 3737f85c8..937e3717e 100644 --- a/src/Blocks/AbstractBlocksCli.php +++ b/src/Blocks/AbstractBlocksCli.php @@ -38,7 +38,8 @@ protected function blocksMove(array $assocArgs, string $outputDir, bool $isCompo $root = $this->getProjectRootPath(); $rootNode = $this->getFrontendLibsBlockPath(); - $sourcePathFolder = "{$rootNode}/{$outputDir}/"; + $ds = \DIRECTORY_SEPARATOR; + $sourcePathFolder = "{$rootNode}{$ds}{$outputDir}{$ds}"; $blocks = \scandir($sourcePathFolder); $blocksFullList = \array_diff((array)$blocks, ['..', '.']); @@ -53,13 +54,13 @@ protected function blocksMove(array $assocArgs, string $outputDir, bool $isCompo // Iterate blocks/components. foreach ($blocks as $block) { - $path = "{$outputDir}/{$block}"; + $path = "{$outputDir}{$ds}{$block}"; $sourcePath = "{$sourcePathFolder}{$block}"; - if (!\getenv('ES_TEST')) { - $destinationPath = $root . \DIRECTORY_SEPARATOR . $path; + if (!\getenv('TEST')) { + $destinationPath = "{$root}{$ds}{$path}"; } else { - $destinationPath = $this->getProjectRootPath(true) . '/cliOutput'; + $destinationPath = "{$this->getProjectRootPath(true)}{$ds}cliOutput"; } $typePlural = !$isComponents ? 'blocks' : 'components'; @@ -115,11 +116,12 @@ protected function blocksMove(array $assocArgs, string $outputDir, bool $isCompo */ private function moveBlock(string $destinationPath, string $sourcePath, string $name, array $assocArgs, string $path, string $typeSingular): void { + $ds = \DIRECTORY_SEPARATOR; // Create folder in project if missing. - \mkdir("{$destinationPath}/"); + \mkdir("{$destinationPath}{$ds}"); // Move block/component to project folder. - $this->copyRecursively($sourcePath, "{$destinationPath}/"); + $this->copyRecursively($sourcePath, "{$destinationPath}{$ds}"); $typeSingular = \ucfirst($typeSingular); diff --git a/src/Blocks/BlocksCli.php b/src/Blocks/BlocksCli.php index 68aa43162..a6bb34bc1 100644 --- a/src/Blocks/BlocksCli.php +++ b/src/Blocks/BlocksCli.php @@ -144,11 +144,11 @@ private function blocksInit(array $args): void WP_CLI::runcommand("{$this->commandParentName} use_wrapper {$this->prepareArgsManual($args)}"); foreach (static::COMPONENTS as $component) { - WP_CLI::runcommand("{$this->commandParentName} use_component --name='{$component}' {$this->prepareArgsManual($args)}"); + WP_CLI::runcommand("{$this->commandParentName} use_component --name={$component} {$this->prepareArgsManual($args)}"); } foreach (static::BLOCKS as $block) { - WP_CLI::runcommand("{$this->commandParentName} use_block --name='{$block}' {$this->prepareArgsManual($args)}"); + WP_CLI::runcommand("{$this->commandParentName} use_block --name={$block} {$this->prepareArgsManual($args)}"); } WP_CLI::success('Blocks successfully set.'); diff --git a/src/Cli/AbstractCli.php b/src/Cli/AbstractCli.php index 8f72fcbf5..6532a0a71 100644 --- a/src/Cli/AbstractCli.php +++ b/src/Cli/AbstractCli.php @@ -246,11 +246,12 @@ function ($item) { */ public function getExampleTemplate(string $currentDir, string $fileName, bool $skipMissing = false): self { - $path = "{$currentDir}/{$this->getExampleFileName( $fileName )}.php"; + $ds = \DIRECTORY_SEPARATOR; + $path = "{$currentDir}{$ds}{$this->getExampleFileName( $fileName )}.php"; // If you pass file name with extension the version will be used. if (\strpos($fileName, '.') !== false) { - $path = "{$currentDir}/{$fileName}"; + $path = "{$currentDir}{$ds}{$fileName}"; } $templateFile = ''; @@ -345,21 +346,25 @@ public function outputWrite(string $outputDir, string $outputFile, array $args = */ public function getOutputDir(string $path = ''): string { - if (\function_exists('\add_action') && !\getenv('ES_TEST')) { + $ds = \DIRECTORY_SEPARATOR; + + if (\function_exists('\add_action') && !\getenv('TEST')) { $root = $this->getProjectRootPath(); } else { - $root = $this->getProjectRootPath(true) . '/cliOutput'; + $root = "{$this->getProjectRootPath(true)}{$ds}cliOutput"; } - $ds = \DIRECTORY_SEPARATOR; - $root = \rtrim($root, $ds); $root = \trim($root, $ds); $path = \rtrim($path, $ds); $path = \trim($path, $ds); - return "{$ds}{$root}{$ds}{$path}"; + if ($ds === '/') { + return "{$ds}{$root}{$ds}{$path}"; + } + + return "{$root}{$ds}{$path}"; } /** @@ -657,11 +662,12 @@ public function searchReplaceString(string $oldString, string $newString): self */ public function getComposer(array $args = []): array { + $ds = \DIRECTORY_SEPARATOR; if (!isset($args['config_path'])) { if (\function_exists('\add_action')) { - $composerPath = $this->getProjectRootPath() . '/composer.json'; + $composerPath = "{$this->getProjectRootPath()}{$ds}composer.json"; } else { - $composerPath = $this->getProjectRootPath(true) . '/composer.json'; + $composerPath = "{$this->getProjectRootPath(true)}{$ds}composer.json"; } } else { $composerPath = $args['config_path']; @@ -834,7 +840,8 @@ public function getProjectConfigRootPath(bool $isDev = false): string */ public function getFrontendLibsPath(string $path = ''): string { - return "{$this->getProjectRootPath()}/node_modules/@eightshift/frontend-libs/{$path}"; + $ds = \DIRECTORY_SEPARATOR; + return "{$this->getProjectRootPath()}{$ds}node_modules{$ds}@eightshift{$ds}frontend-libs{$ds}{$path}"; } /** @@ -846,11 +853,12 @@ public function getFrontendLibsPath(string $path = ''): string */ public function getLibsPath(string $path = ''): string { - if (\getenv('ES_TEST')) { - return "{$this->getProjectRootPath()}/{$path}"; + $ds = \DIRECTORY_SEPARATOR; + if (\getenv('TEST')) { + return "{$this->getProjectRootPath()}{$ds}{$path}"; } - return "{$this->getProjectRootPath()}/vendor/infinum/eightshift-libs/{$path}"; + return "{$this->getProjectRootPath()}{$ds}vendor{$ds}infinum{$ds}eightshift-libs{$ds}{$path}"; } /** @@ -860,7 +868,8 @@ public function getLibsPath(string $path = ''): string */ public function getFrontendLibsBlockPath(): string { - return $this->getFrontendLibsPath('blocks/init'); + $ds = \DIRECTORY_SEPARATOR; + return $this->getFrontendLibsPath("blocks{$ds}init"); } /** @@ -872,16 +881,17 @@ public function getFrontendLibsBlockPath(): string */ public function getFullBlocksFiles(string $name): array { + $ds = \DIRECTORY_SEPARATOR; return [ "{$name}.php", "{$name}-block.js", "{$name}-hooks.js", "{$name}-transforms.js", "{$name}.js", - "docs/story.js", - "components/{$name}-editor.js", - "components/{$name}-toolbar.js", - "components/{$name}-options.js", + "docs{$ds}story.js", + "components{$ds}{$name}-editor.js", + "components{$ds}{$name}-toolbar.js", + "components{$ds}{$name}-options.js", ]; } diff --git a/src/Cli/CliInitAll.php b/src/Cli/CliInitAll.php index 7815e2c41..a54c6b4f5 100644 --- a/src/Cli/CliInitAll.php +++ b/src/Cli/CliInitAll.php @@ -42,8 +42,9 @@ public function getCommandName(): string public function getDoc(): array { return [ - 'shortdesc' => 'Generates initial setup for all service classes in the WordPress theme project. - This command is used only in develop mode. For this to work you must set global constant ES_DEVELOP_MODE to true.', + 'shortdesc' => + 'Generates initial setup for all service classes in the WordPress theme project. + This command is used only in develop mode. For this to work you must set global constant ES_DEVELOP_MODE to true.', ]; } diff --git a/src/Helpers/Components.php b/src/Helpers/Components.php index 942a16b0c..cb43c44d8 100644 --- a/src/Helpers/Components.php +++ b/src/Helpers/Components.php @@ -215,7 +215,11 @@ public static function getManifestDirect(string $path): array $sep = \DIRECTORY_SEPARATOR; $path = \trim($path, $sep); - $manifest = "{$sep}{$path}{$sep}manifest.json"; + $manifest = "{$path}{$sep}manifest.json"; + + if ($sep === '/') { + $manifest = "{$sep}{$manifest}"; + } if (!\file_exists($manifest)) { throw ComponentException::throwUnableToLocateComponent($manifest); diff --git a/src/Helpers/CssVariablesTrait.php b/src/Helpers/CssVariablesTrait.php index f0f36025f..3d76fbbf5 100644 --- a/src/Helpers/CssVariablesTrait.php +++ b/src/Helpers/CssVariablesTrait.php @@ -151,104 +151,102 @@ public static function outputCssVariablesInline(): string return ''; } + // Prepare final output. + $output = ''; + $styles = Components::getStyles(); // Bailout if styles are missing. - if (!$styles) { - return ''; - } - - // Define variables from globalManifest. - $breakpointsData = self::getSettingsGlobalVariablesBreakpoints(); - - // Sort breakpoints in ascending order. - \asort($breakpointsData); - - // Populate min values. - $breakpointsMin = \array_map( - static function ($item) { - return "min---{$item}"; - }, - \array_values($breakpointsData) - ); - // Append 0 value. - \array_unshift($breakpointsMin, 'min---0'); - - // Populate max values. - $breakpointsMax = \array_map( - static function ($item) { - return "max---{$item}"; - }, - \array_reverse(\array_values($breakpointsData)) - ); - // Append 0 value. - \array_unshift($breakpointsMax, 'max---0'); - - // Return empty array of items. - $breakpoints = \array_map( - static function () { - return ''; - }, - \array_flip(\array_values(\array_merge($breakpointsMin, $breakpointsMax))) - ); + if ($styles) { + // Define variables from globalManifest. + $breakpointsData = self::getSettingsGlobalVariablesBreakpoints(); - // Loop styles. - foreach ($styles as $style) { - $name = $style['name'] ?? ''; - $unique = $style['unique'] ?? ''; - $variables = $style['variables'] ?? []; + // Sort breakpoints in ascending order. + \asort($breakpointsData); - // Bailout if variables are missing. - if (!$variables) { - continue; - } + // Populate min values. + $breakpointsMin = \array_map( + static function ($item) { + return "min---{$item}"; + }, + \array_values($breakpointsData) + ); + // Append 0 value. + \array_unshift($breakpointsMin, 'min---0'); - $uniqueSelector = "[data-id='{$unique}']"; + // Populate max values. + $breakpointsMax = \array_map( + static function ($item) { + return "max---{$item}"; + }, + \array_reverse(\array_values($breakpointsData)) + ); + // Append 0 value. + \array_unshift($breakpointsMax, 'max---0'); - if (!$unique) { - $uniqueSelector = ''; - } + // Return empty array of items. + $breakpoints = \array_map( + static function () { + return ''; + }, + \array_flip(\array_values(\array_merge($breakpointsMin, $breakpointsMax))) + ); - foreach ($variables as $data) { - $type = $data['type'] ?? ''; - $value = $data['value'] ?? ''; - $variable = $data['variable'] ?? ''; + // Loop styles. + foreach ($styles as $style) { + $name = $style['name'] ?? ''; + $unique = $style['unique'] ?? ''; + $variables = $style['variables'] ?? []; - // Bailout if variable is missing. - if (!$variable) { + // Bailout if variables are missing. + if (!$variables) { continue; } - // Bailout if breakpont is missing. - if (!isset($breakpoints["{$type}---{$value}"])) { - continue; - } + $uniqueSelector = "[data-id='{$unique}']"; - // Populate breakpoint. - $breakpoints["{$type}---{$value}"] .= "\n.{$name}{$uniqueSelector}{\n{$variable}\n} "; - } - } + if (!$unique) { + $uniqueSelector = ''; + } - // Prepare final output. - $output = ''; + foreach ($variables as $data) { + $type = $data['type'] ?? ''; + $value = $data['value'] ?? ''; + $variable = $data['variable'] ?? ''; - // Loop breakpoints in correct order. - foreach ($breakpoints as $breakpointKey => $breakpointValue) { - $breakpointKey = \explode('---', $breakpointKey); + // Bailout if variable is missing. + if (!$variable) { + continue; + } - $type = $breakpointKey[0] ?? ''; - $value = $breakpointKey[1] ?? ''; + // Bailout if breakpont is missing. + if (!isset($breakpoints["{$type}---{$value}"])) { + continue; + } - // Bailout if empty value. - if (!$breakpointValue) { - continue; + // Populate breakpoint. + $breakpoints["{$type}---{$value}"] .= "\n.{$name}{$uniqueSelector}{\n{$variable}\n} "; + } } - // If value is 0 then this breakpoint has no media query. - if ($value === '0') { - $output .= "{$breakpointValue}\n"; - } else { - $output .= "\n@media ({$type}-width:{$value}px){{$breakpointValue}}\n "; + // Loop breakpoints in correct order. + foreach ($breakpoints as $breakpointKey => $breakpointValue) { + $breakpointKey = \explode('---', $breakpointKey); + + $type = $breakpointKey[0] ?? ''; + $value = $breakpointKey[1] ?? ''; + + // Bailout if empty value. + if (!$breakpointValue) { + continue; + } + + // If value is 0 then this breakpoint has no media query. + if ($value === '0') { + $output .= "{$breakpointValue}\n"; + } else { + $output .= "\n@media ({$type}-width:{$value}px){{$breakpointValue}}\n "; + } } }