diff --git a/src/Commands/BaseBuildCommand.php b/src/Commands/BaseBuildCommand.php index bc3082b..226d07e 100644 --- a/src/Commands/BaseBuildCommand.php +++ b/src/Commands/BaseBuildCommand.php @@ -156,9 +156,14 @@ protected function ensureExportDirectoryExists(string $currentPath): void { $this->output->writeln('==> Preparing Export Directory ...'); - if (!$this->disk->isDirectory($currentPath . '/export')) { + if (!$this->disk->isDirectory( + Config::buildPath($currentPath, "export") + )) { $this->disk->makeDirectory( - $currentPath . '/export', + Config::buildPath( + $currentPath, + "export" + ), 0755, true ); diff --git a/src/Commands/BuildEpubCommand.php b/src/Commands/BuildEpubCommand.php index 4a0eb95..eaa9170 100644 --- a/src/Commands/BuildEpubCommand.php +++ b/src/Commands/BuildEpubCommand.php @@ -2,6 +2,7 @@ namespace Ibis\Commands; +use Ibis\Config; use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -129,7 +130,7 @@ protected function buildEpub(Collection $chapters, array $config, string $curren $coverImage = $config['cover']['image']; } - $pathCoverImage = $currentPath . '/assets/' . $coverImage; + $pathCoverImage = Config::buildPath($currentPath, 'assets', $coverImage); if ($this->disk->isFile($pathCoverImage)) { $this->output->writeln('==> Adding Book Cover ' . $pathCoverImage . ' ...'); @@ -168,7 +169,12 @@ protected function buildEpub(Collection $chapters, array $config, string $curren $book->finalize(); - $epubFilename = $currentPath . '/export/' . $this->config->outputFileName() . '.epub'; + //$epubFilename = $currentPath . '/export/' . $this->config->outputFileName() . '.epub'; + $epubFilename = Config::buildPath( + $currentPath, + "export", + $this->config->outputFileName() . '.epub' + ); $book->saveBook($epubFilename); diff --git a/src/Config.php b/src/Config.php index a676e8b..29c1a0b 100644 --- a/src/Config.php +++ b/src/Config.php @@ -11,10 +11,7 @@ class Config */ public $config; - /** - * @var string - */ - public $ibisConfigPath; + public string $ibisConfigPath; public $contentPath; @@ -26,7 +23,7 @@ public function __construct(public $workingPath = "") $this->workingPath = "./"; } - $this->ibisConfigPath = $this->workingPath . '/ibis.php'; + $this->ibisConfigPath = self::buildPath($this->workingPath, 'ibis.php'); $this->config = require $this->ibisConfigPath; } @@ -35,11 +32,31 @@ public static function load($directory = ""): self return new self($directory); } + public static function buildPath(...$pathElements): string + { + //$paths = func_get_args(); + $last_key = count($pathElements) - 1; + array_walk($pathElements, static function (&$val, $key) use ($last_key): void { + $val = match ($key) { + 0 => rtrim($val, '/ '), + $last_key => ltrim($val, '/ '), + default => trim($val, '/ '), + }; + }); + $first = array_shift($pathElements); + $last = array_pop($pathElements); + $paths = array_filter($pathElements); // clean empty elements to prevent double slashes + array_unshift($paths, $first); + $paths[] = $last; + + return implode('/', $paths); + } + public function setContentPath($directory = ""): bool { $this->contentPath = $directory; if ($this->contentPath === "") { - $this->contentPath = $this->workingPath . DIRECTORY_SEPARATOR . "content"; + $this->contentPath = self::buildPath($this->workingPath, "content"); } return is_dir($this->contentPath);