Skip to content

Commit

Permalink
feat: add theme publishing functionality, UI button and a new command…
Browse files Browse the repository at this point in the history
… `igniter:theme-publish`

Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Oct 13, 2024
1 parent 4f5cca3 commit a58adcd
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 36 deletions.
25 changes: 0 additions & 25 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,6 @@ parameters:
count: 1
path: src/Admin/FormWidgets/RecordEditor.php

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Query\\\\Builder\\:\\:getQuery\\(\\)\\.$#"
count: 2
path: src/Admin/FormWidgets/Relation.php

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Query\\\\Builder\\:\\:sorted\\(\\)\\.$#"
count: 1
Expand Down Expand Up @@ -2960,11 +2955,6 @@ parameters:
count: 1
path: src/Flame/Providers/EventServiceProvider.php

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Query\\\\Builder\\:\\:getQuery\\(\\)\\.$#"
count: 1
path: src/Flame/Providers/MacroServiceProvider.php

-
message: "#^Method Igniter\\\\Flame\\\\Scaffold\\\\GeneratorCommand\\:\\:makeDirectory\\(\\) should return string but return statement is missing\\.$#"
count: 1
Expand Down Expand Up @@ -3255,11 +3245,6 @@ parameters:
count: 1
path: src/Flame/Support/Helpers/helpers.php

-
message: "#^Function traceLog invoked with 1 parameter, 0 required\\.$#"
count: 7
path: src/Flame/Support/Helpers/helpers.php

-
message: "#^Parameter \\#2 \\$time of function mdate expects string\\|null, \\(int\\|false\\) given\\.$#"
count: 2
Expand Down Expand Up @@ -3430,11 +3415,6 @@ parameters:
count: 1
path: src/Main/Classes/MainController.php

-
message: "#^Function traceLog invoked with 1 parameter, 0 required\\.$#"
count: 1
path: src/Main/Classes/MainController.php

-
message: "#^PHPDoc tag @param for parameter \\$name with type mixed is not subtype of native type string\\.$#"
count: 1
Expand Down Expand Up @@ -4300,11 +4280,6 @@ parameters:
count: 1
path: src/System/Classes/ExtensionManager.php

-
message: "#^Function traceLog invoked with 1 parameter, 0 required\\.$#"
count: 2
path: src/System/Classes/ExtensionManager.php

-
message: "#^Property Illuminate\\\\Foundation\\\\PackageManifest\\:\\:\\$manifest \\(array\\) does not accept null\\.$#"
count: 1
Expand Down
3 changes: 3 additions & 0 deletions resources/lang/en/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@
'button_source' => '<i class="fa fa-file"></i>&nbsp;&nbsp;Edit template files',
'button_customize' => '<i class="fa fa-paint-brush"></i>&nbsp;&nbsp;Customize',
'button_child' => '<i class="fa fa-child"></i>&nbsp;&nbsp;Create child theme',
'button_publish' => 'Publish theme files',
'button_choose' => 'Choose a component to attach',
'button_new_source' => 'New %s',
'button_rename_source' => 'Rename %s',
Expand All @@ -559,6 +560,8 @@
'alert_changes_confirm' => 'Conflicting versions, template file has changed. Reload the page to continue.',
'alert_customize_not_active' => 'You can only customize an active theme.',
'alert_component_partial_not_found' => 'The selected component partial does not exist in the component directory',
'alert_no_publish_custom' => 'Publishing custom theme files required a child theme located within the themes directory',
'alert_publish_confirm' => 'Are you sure you wish to publish the theme files? This will overwrite the existing theme files.',
],

'updates' => [
Expand Down
6 changes: 6 additions & 0 deletions resources/models/main/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
'class' => 'btn btn-success',
'href' => 'updates',
],
'publish' => [
'label' => 'lang:igniter::system.themes.button_publish',
'class' => 'btn btn-default pull-right',
'data-request' => 'onPublish',
'data-request-confirm' => 'lang:igniter::system.themes.alert_publish_confirm',
],
],
];

Expand Down
12 changes: 12 additions & 0 deletions src/Admin/Traits/ListExtendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,16 @@ public static function extendListColumns(callable $callback)
$callback($widget, $widget->model);
});
}

public static function extendListQuery(callable $callback)
{
$calledClass = self::getCalledExtensionClass();
Event::listen('admin.list.extendQuery', function($widget, $query) use ($calledClass, $callback) {
if (!is_a($widget->getController(), $calledClass)) {
return;
}

$callback($widget, $query);
});
}
}
1 change: 0 additions & 1 deletion src/Admin/Widgets/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ public function getTabs(): array
public function getTab($tab): ?FormTabs
{
return $this->allTabs[$tab] ?? null;

}

/**
Expand Down
25 changes: 24 additions & 1 deletion src/Flame/Igniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Igniter

protected static bool $autoloadExtensions = true;

protected static array $publishesThemeFiles = [];

/**
* Set the extensions path for the application.
*
Expand Down Expand Up @@ -169,7 +171,7 @@ public static function loadMigrationsFrom(string $path, string $namespace)
static::$migrationPaths[$namespace] = $path;
}

public function ignoreMigrations(string $namespace = '*')
public static function ignoreMigrations(string $namespace = '*')
{
static::$ignoreMigrations[] = $namespace;

Expand Down Expand Up @@ -317,4 +319,25 @@ public static function autoloadExtensions(?bool $value = null)

static::$autoloadExtensions = $value;
}

public static function publishesThemeFiles(string|array $paths): void
{
foreach ((array)$paths as $path => $publishTo) {
if (is_numeric($path)) {
$path = $publishTo;
$publishTo = null;
}

if (is_null($publishTo)) {
$publishTo = $path;
}

static::$publishesThemeFiles[$path] = $publishTo;
}
}

public static function publishableThemeFiles(): array
{
return static::$publishesThemeFiles;
}
}
17 changes: 8 additions & 9 deletions src/Main/Classes/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Igniter\Main\Template\Layout as LayoutTemplate;
use Igniter\Main\Template\Page as PageTemplate;
use Igniter\Main\Template\Partial as PartialTemplate;
use Illuminate\Support\Collection;

class Theme
{
Expand Down Expand Up @@ -124,14 +125,14 @@ public function getAssetPath(): string

public function getPathsToPublish(): array
{
$publishPath = $this->config['publish-paths'] ?? null;
$publishPath = $this->config['publish-paths'] ?? [];

if (!$publishPath && File::exists($this->getAssetPath())) {
return [$this->getAssetPath() => public_path('vendor/'.$this->name)];
}

$result = [];
foreach ($this->config['publish-paths'] ?? [] as $path) {
foreach ($publishPath as $path) {
if (File::isDirectory($this->path.$path)) {
$result[$this->path.$path] = public_path('vendor/'.$this->name);
}
Expand Down Expand Up @@ -414,24 +415,22 @@ protected function getFindInPaths(): array
//
//

public function listPages()
public function listPages(): Collection
{
return PageTemplate::listInTheme($this->getName());
}

public function listPartials()
public function listPartials(): Collection
{
return PartialTemplate::listInTheme($this->getName());
}

public function listLayouts()
public function listLayouts(): Collection
{
return LayoutTemplate::listInTheme($this->getName());
}

public function getPagesOptions() {}

public function listRequires()
public function listRequires(): array
{
return array_merge($this->hasParent() ? $this->getParent()->listRequires() : [], $this->requires);
}
Expand All @@ -458,7 +457,7 @@ public function makeFileSource(): SourceInterface
return $this->fileSource = $source;
}

public function onTemplate($dirName): Model
public function onTemplate(string $dirName): Model
{
$modelClass = $this->getTemplateClass($dirName);

Expand Down
12 changes: 12 additions & 0 deletions src/Main/Http/Controllers/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Igniter\System\Helpers\CacheHelper;
use Igniter\System\Traits\ManagesUpdates;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Artisan;

class Themes extends \Igniter\Admin\Classes\AdminController
{
Expand Down Expand Up @@ -158,6 +159,17 @@ public function index_onSetDefault(): RedirectResponse
return $this->redirectBack();
}

public function index_onPublish(): RedirectResponse
{
Artisan::call('igniter:theme-publish', ['--force' => true]);

logger()->info($output = Artisan::output());

flash()->success(nl2br($output));

return $this->redirectBack();
}

public function edit_onReset(string $context, string $themeCode): ?RedirectResponse
{
$formController = $this->asExtension('FormController');
Expand Down
72 changes: 72 additions & 0 deletions src/System/Console/Commands/ThemePublish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Igniter\System\Console\Commands;

use Igniter\Flame\Exception\SystemException;
use Igniter\Flame\Igniter;
use Igniter\Main\Classes\Theme;
use Igniter\Main\Classes\ThemeManager;
use Illuminate\Foundation\Console\VendorPublishCommand;
use Symfony\Component\Console\Input\InputOption;

class ThemePublish extends VendorPublishCommand
{
use \Illuminate\Console\ConfirmableTrait;

protected $name = 'igniter:theme-publish';

protected $description = 'Publish any publishable theme files from extensions';

protected $signature;

protected ?Theme $activeTheme;

/**
* Execute the console command.
* @return void
*/
public function handle()
{
$this->determineWhatShouldBePublished();

$published = false;

$activeThemePath = $this->activeTheme->getPath();
foreach (Igniter::publishableThemeFiles() as $path => $publishTo) {
$this->publishItem($path, $activeThemePath.'/'.$publishTo);
$published = true;
}

if ($published === false) {
$this->comment('No publishable custom files for theme ['.$this->activeTheme->getName().'].');
}

$this->info('Publishing complete.');
}

protected function determineWhatShouldBePublished()
{
throw_unless(
$this->activeTheme = resolve(ThemeManager::class)->getActiveTheme(),
new SystemException(lang('igniter::admin.alert_error_nothing'))
);

throw_if(
$this->activeTheme->locked,
new SystemException(lang('igniter::system.themes.alert_theme_locked'))
);

throw_if(
!str_starts_with($this->activeTheme->getPath(), theme_path()),
new SystemException(lang('igniter::system.themes.alert_no_publish_custom'))
);
}

protected function getOptions()
{
return [
['existing', null, InputOption::VALUE_NONE, 'Publish and overwrite only the files that have already been published'],
['force', null, InputOption::VALUE_NONE, 'Force publish.'],
];
}
}
1 change: 1 addition & 0 deletions src/System/Providers/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ConsoleServiceProvider extends BaseConsoleServiceProvider
'extension.remove' => Console\Commands\ExtensionRemove::class,
'theme.install' => Console\Commands\ThemeInstall::class,
'theme.remove' => Console\Commands\ThemeRemove::class,
'theme.publish' => Console\Commands\ThemePublish::class,
'theme.vendor-publish' => Console\Commands\ThemeVendorPublish::class,
'language.install' => Console\Commands\LanguageInstall::class,
];
Expand Down

0 comments on commit a58adcd

Please sign in to comment.