Skip to content

Commit

Permalink
[MERGE] - PR #7 to develop
Browse files Browse the repository at this point in the history
[CHORE]: Re-do component structure
  • Loading branch information
asciito authored Oct 3, 2023
2 parents 13bf337 + 19fcc41 commit 7bcc9b2
Show file tree
Hide file tree
Showing 51 changed files with 829 additions and 967 deletions.
82 changes: 82 additions & 0 deletions src/Package/Concerns/HasCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Asciito\LaravelPackage\Package\Concerns;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;

trait HasCommand
{
protected bool $preventLoadDefaultCommandFolder = false;

protected bool $shouldIncludeCommandFromFolder = false;

public function hasCommand(): bool
{
return $this->shouldLoadDefaultCommandFolder()
|| $this->getRegister('command')->isNotEmpty();
}

public function withCommand(string|array $command = []): static
{
$this->ensureRegistersInitialize('command');

if (filled($command)) {
$this->register(
'command',
$command,
);
}

$this->shouldIncludeCommandFromFolder = true;

return $this;
}

public function excludeCommand(string|array $path): static
{
$this->exclude('command', $path);

return $this;
}

public function getRegisteredCommand(): Collection
{
$files = $this->getDefaultCommandFiles();

return $files
->merge($this->getRegister('command'))
->filter(fn (string $path) => ! in_array($path, $this->getExclude('command')->all()));
}

public function getDefaultCommandFiles(): Collection
{
if (! $this->shouldLoadDefaultCommandFolder()) {
return collect();
}

return $this->getFilesFrom($this->getBasePath('console/commands'))
->map(fn (string $path): string => Str::of($path)
->basename('.php')
->prepend(
'\\',
$this->getNamespace(),
'\\Console\\Commands\\',
),
);

}

public function preventDefaultCommand(): static
{
$this->preventLoadDefaultCommandFolder = true;

return $this;
}

private function shouldLoadDefaultCommandFolder(): bool
{
return ! $this->preventLoadDefaultCommandFolder
&& $this->shouldIncludeCommandFromFolder;
}
}
86 changes: 0 additions & 86 deletions src/Package/Concerns/HasCommands.php

This file was deleted.

79 changes: 35 additions & 44 deletions src/Package/Concerns/HasConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,67 @@

namespace Asciito\LaravelPackage\Package\Concerns;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

trait HasConfig
{
protected string $configPath;

protected array $configFiles = [];

protected bool $preventLoadDefaultConfigFolder = false;

protected bool $shouldIncludeConfigFromFolder = false;

protected array $excludedConfig = [];

protected array $unpublishedConfig = [];

public function hasConfig(): bool
{
return $this->shouldLoadDefaultConfigFolder() || filled($this->configFiles);
}

private function shouldLoadDefaultConfigFolder(): bool
{
return ! $this->preventLoadDefaultConfigFolder && $this->shouldIncludeConfigFromFolder;
return $this->shouldLoadDefaultConfigFolder()
|| $this->getRegister('config')->isNotEmpty();
}

public function withConfig(string|array $config = [], bool $publish = true): static
{
$this->ensureRegistersInitialize('config');

if (filled($config)) {
$this->configFiles = collect($config)
->mapWithKeys(fn (string $value) => [$value => $publish])
->merge($this->configFiles)
->all();
$this->register(
'config',
Arr::mapWithKeys(Arr::wrap($config), fn (string $path): array => [$path => $publish])
);
}

$this->shouldIncludeConfigFromFolder = true;

return $this;
}

public function preventDefaultConfig(): static
public function excludeConfig(string|array $path): static
{
$this->preventLoadDefaultConfigFolder = true;
$this->exclude('config', Arr::wrap($path));

return $this;
}

public function getRegisteredConfig(): Collection
{
$config = $this->loadConfigDefaultFolder();
$files = $this->getDefaultConfigFiles();

return collect($this->configFiles)
->merge($config)
->filter(function (bool $_, string $config) {
return ! in_array($config, $this->excludedConfig);
})
return $files
->merge($this->getRegister('config'))
->except($this->getExclude('config'))
->keys();
}

private function loadConfigDefaultFolder(): array
public function getPublishableConfig(): Collection
{
if (! $this->shouldLoadDefaultConfigFolder()) {
return [];
}

return $this->loadFilesFrom($this->getConfigPath())->all();
$files = $this->getDefaultConfigFiles();

return $files
->merge($this->getRegister('config'))
->filter()
->except($this->getExclude('config'))
->mapWithKeys(fn (bool $_, string $path) => [
$path => config_path(basename($path)),
]);
}

public function getConfigPath(string $path = ''): string
Expand All @@ -82,29 +77,25 @@ public function setConfigPath(string $path): static
return $this;
}

public function getPublishableConfig(): Collection
public function getDefaultConfigFiles(): Collection
{
$config = $this->loadConfigDefaultFolder();
if (! $this->shouldLoadDefaultConfigFolder()) {
return collect();
}

return collect($this->configFiles)
->merge($config)
->filter(function (bool $publish, string $config) {
return $publish && ! in_array($config, [...$this->excludedConfig, ...$this->unpublishedConfig]);
})
->keys();
return $this->getFilesFrom($this->configPath)
->mapWithKeys(fn (string $path) => [$path => true]);
}

public function unregisterConfig(string $path): static
public function preventDefaultConfig(): static
{
$this->excludedConfig[] = absolute($path, $this->getConfigPath());
$this->preventLoadDefaultConfigFolder = true;

return $this;
}

public function unpublishConfig(string $path): static
private function shouldLoadDefaultConfigFolder(): bool
{
$this->unpublishedConfig[] = absolute($path, $this->getConfigPath());

return $this;
return ! $this->preventLoadDefaultConfigFolder && $this->shouldIncludeConfigFromFolder;
}
}
Loading

0 comments on commit 7bcc9b2

Please sign in to comment.