Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding function for building path #21

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Commands/BaseBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,14 @@ protected function ensureExportDirectoryExists(string $currentPath): void
{
$this->output->writeln('<fg=yellow>==></> 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
);
Expand Down
10 changes: 8 additions & 2 deletions src/Commands/BuildEpubCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ibis\Commands;

use Ibis\Config;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -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('<fg=yellow>==></> Adding Book Cover ' . $pathCoverImage . ' ...');

Expand Down Expand Up @@ -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);


Expand Down
29 changes: 23 additions & 6 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ class Config
*/
public $config;

/**
* @var string
*/
public $ibisConfigPath;
public string $ibisConfigPath;

public $contentPath;

Expand All @@ -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;
}

Expand All @@ -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);
Expand Down
Loading