Skip to content

Commit 4bf1adf

Browse files
authored
Merge pull request #15 from imagewize/config-publication-options
Config Publication Options
2 parents e48abc9 + 0621cd3 commit 4bf1adf

File tree

3 files changed

+81
-14
lines changed

3 files changed

+81
-14
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [2.0.1] - 2025-10-16
11+
12+
### Fixed
13+
14+
- **Config Loading Issue** - Fixed template configuration not loading in production environments
15+
- Added fallback config loading directly from package config file
16+
- Resolves "No templates found" error when running `wp acorn sage-native-block:create`
17+
- Templates now load correctly even if Laravel config() helper fails
18+
- Improved resilience for production WordPress/Acorn environments
19+
1020
## [2.0.0] - 2025-10-14
1121

1222
### Changed - BREAKING CHANGES
@@ -207,7 +217,8 @@ Templates automatically appear in the category selection menu on next run.
207217
- Configuration documentation
208218
- Feature overview and examples
209219

210-
[Unreleased]: https://github.com/imagewize/sage-native-block/compare/v2.0.0...HEAD
220+
[Unreleased]: https://github.com/imagewize/sage-native-block/compare/v2.0.1...HEAD
221+
[2.0.1]: https://github.com/imagewize/sage-native-block/compare/v2.0.0...v2.0.1
211222
[2.0.0]: https://github.com/imagewize/sage-native-block/compare/v1.1.0...v2.0.0
212223
[1.1.0]: https://github.com/imagewize/sage-native-block/compare/v1.0.2...v1.1.0
213224
[1.0.2]: https://github.com/imagewize/sage-native-block/compare/v1.0.1...v1.0.2

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,24 @@ You can install this package with Composer from your Sage 11+ theme root directo
2323
composer require imagewize/sage-native-block --dev
2424
```
2525

26-
**NB** You can drop `--dev` but then it will be included in your production build.
26+
**That's it!** The package is ready to use. No additional setup required.
2727

28+
You can drop `--dev` but then it will be included in your production build.
2829

29-
## Configuration
30+
## Configuration (Optional)
3031

31-
You can publish the config file with:
32+
The package works out of the box with default settings. However, you can optionally publish the config file to customize template settings:
3233

3334
```shell
3435
wp acorn vendor:publish --provider="Imagewize\SageNativeBlockPackage\Providers\SageNativeBlockServiceProvider"
3536
```
3637

37-
**NB**: This is recommended to customize template settings. The package includes default configuration with 5 block templates and typography/spacing presets.
38+
**When to publish:**
39+
- You want to customize typography or spacing presets
40+
- You want to add your own template definitions to the config
41+
- You're experiencing config loading issues in your environment (rare)
42+
43+
**Note:** Since v2.0.1, the package automatically falls back to loading config directly if it's not published, making this step truly optional.
3844

3945
## Usage
4046

@@ -221,7 +227,15 @@ resources/js/blocks/testimonial/
221227

222228
### Typography and Spacing Presets
223229

224-
After publishing the config file (`wp acorn vendor:publish`), you can customize typography and spacing presets in `config/sage-native-block.php` to match your theme's design system.
230+
**Optional:** If you want to customize global typography and spacing presets used by package templates, publish the config file:
231+
232+
```bash
233+
wp acorn vendor:publish --provider="Imagewize\SageNativeBlockPackage\Providers\SageNativeBlockServiceProvider"
234+
```
235+
236+
Then edit `config/sage-native-block.php` to match your theme's design system.
237+
238+
**Note:** This only affects package templates (basic, generic, nynaeve). Your custom templates in `block-templates/` are unaffected and use whatever styles you define in them.
225239

226240
### Creating Custom Templates
227241

src/Console/SageNativeBlockCommand.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ public function handle(RootsFilesystem $rootsFiles): int
158158
}
159159

160160
// Get template display name
161-
$templateConfig = config('sage-native-block.templates')[$template] ?? [];
161+
$allTemplates = array_merge($this->getTemplatesConfig(), $this->getThemeTemplates());
162+
$templateConfig = $allTemplates[$template] ?? [];
162163
$templateName = $templateConfig['name'] ?? $template;
163164

164165
// Extract the base name for directory structure
@@ -541,6 +542,47 @@ protected function updateJsFile(RootsFilesystem $rootsFiles): ?bool
541542
}
542543
}
543544

545+
/**
546+
* Get templates configuration with fallback to package config file.
547+
* This ensures templates are available even if config() helper fails.
548+
*/
549+
protected function getTemplatesConfig(): array
550+
{
551+
$templates = config('sage-native-block.templates', null);
552+
553+
// If config is not loaded (returns null or empty), load directly from package
554+
if ($templates === null || empty($templates)) {
555+
$configPath = dirname(__DIR__, 2) . '/config/sage-native-block.php';
556+
557+
if (file_exists($configPath)) {
558+
$config = require $configPath;
559+
$templates = $config['templates'] ?? [];
560+
}
561+
}
562+
563+
return $templates ?: [];
564+
}
565+
566+
/**
567+
* Get default template with fallback to package config file.
568+
*/
569+
protected function getDefaultTemplate(): string
570+
{
571+
$default = config('sage-native-block.default_template', null);
572+
573+
// If config is not loaded, load directly from package
574+
if ($default === null) {
575+
$configPath = dirname(__DIR__, 2) . '/config/sage-native-block.php';
576+
577+
if (file_exists($configPath)) {
578+
$config = require $configPath;
579+
$default = $config['default_template'] ?? 'basic';
580+
}
581+
}
582+
583+
return $default ?: 'basic';
584+
}
585+
544586
/**
545587
* Get available template categories dynamically.
546588
* Returns categories from config templates plus auto-detected theme templates.
@@ -558,7 +600,7 @@ protected function getAvailableCategories(): array
558600
$categoryNames = [];
559601

560602
// Get categories from config (for package-provided themes like Nynaeve)
561-
$templates = config('sage-native-block.templates', []);
603+
$templates = $this->getTemplatesConfig();
562604
foreach ($templates as $template) {
563605
if (isset($template['category']) &&
564606
!in_array($template['category'], ['basic', 'generic'])) {
@@ -693,7 +735,7 @@ protected function promptForTemplateCategory(): string
693735
protected function promptForTemplate(?string $category = null): string
694736
{
695737
// Get package templates from config
696-
$packageTemplates = config('sage-native-block.templates', []);
738+
$packageTemplates = $this->getTemplatesConfig();
697739

698740
// Get theme templates
699741
$themeTemplates = $this->getThemeTemplates();
@@ -703,7 +745,7 @@ protected function promptForTemplate(?string $category = null): string
703745

704746
if (empty($allTemplates)) {
705747
$this->warn('No templates found. Using default.');
706-
return config('sage-native-block.default_template', 'basic');
748+
return $this->getDefaultTemplate();
707749
}
708750

709751
// Filter templates by category if provided
@@ -715,7 +757,7 @@ protected function promptForTemplate(?string $category = null): string
715757

716758
if (empty($allTemplates)) {
717759
$this->warn("No templates found for category '{$category}'. Using default.");
718-
return config('sage-native-block.default_template', 'basic');
760+
return $this->getDefaultTemplate();
719761
}
720762

721763
// For 'basic' category, just return the basic template directly
@@ -731,7 +773,7 @@ protected function promptForTemplate(?string $category = null): string
731773
$keys[] = $key;
732774
}
733775

734-
$defaultIndex = array_search(config('sage-native-block.default_template', 'basic'), $keys);
776+
$defaultIndex = array_search($this->getDefaultTemplate(), $keys);
735777
if ($defaultIndex === false) {
736778
$defaultIndex = 0;
737779
}
@@ -750,7 +792,7 @@ protected function promptForTemplate(?string $category = null): string
750792
protected function isValidTemplate(string $template): bool
751793
{
752794
// Check package templates
753-
$packageTemplates = config('sage-native-block.templates', []);
795+
$packageTemplates = $this->getTemplatesConfig();
754796
if (isset($packageTemplates[$template])) {
755797
return true;
756798
}
@@ -777,7 +819,7 @@ protected function getStubPath(string $template): string
777819
}
778820

779821
// Fall back to package templates
780-
$packageTemplates = config('sage-native-block.templates', []);
822+
$packageTemplates = $this->getTemplatesConfig();
781823
if (isset($packageTemplates[$template])) {
782824
return $packageTemplates[$template]['stub_path'] ?? 'block';
783825
}

0 commit comments

Comments
 (0)