Skip to content

Commit 4515f09

Browse files
authored
Merge pull request #161 from hydephp/refactor-hydeserviceprovider
Refactor the HydeServiceProvider structure
2 parents 859dd44 + aa53cf0 commit 4515f09

File tree

4 files changed

+183
-38
lines changed

4 files changed

+183
-38
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This serves two purposes:
2525
- for new features.
2626

2727
### Changed
28-
- for changes in existing functionality.
28+
- internal: Move service provider helper methods to the RegistersFileLocations trait
2929

3030
### Deprecated
3131
- for soon-to-be removed features.

packages/framework/src/Concerns/RegistersFileLocations.php

+33
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33
namespace Hyde\Framework\Concerns;
44

55
use Hyde\Framework\Contracts\AbstractPage;
6+
use Hyde\Framework\StaticPageBuilder;
67

8+
/**
9+
* This trait registers the file paths for important Hyde locations.
10+
*
11+
* If you want to customize these directories, the recommended way is to
12+
* create a service provider that uses this trait, and change your
13+
* paths in the register method, like in the HydeServiceProvider.
14+
*
15+
* Remember that your overriding provider should be loaded after the HSP.
16+
*/
717
trait RegistersFileLocations
818
{
919
/**
@@ -41,4 +51,27 @@ protected function registerOutputDirectories(array $directoryMapping): void
4151
$class::$outputDirectory = unslash($location);
4252
}
4353
}
54+
55+
/**
56+
* If you are loading Blade views from a different directory,
57+
* you need to add the path to the view.php config. This is
58+
* here done automatically when registering the provider.
59+
*/
60+
protected function discoverBladeViewsIn(string $directory): void
61+
{
62+
config(['view.paths' => array_unique(array_merge(
63+
config('view.paths', []),
64+
[base_path($directory)]
65+
))]);
66+
}
67+
68+
/**
69+
* The absolute path to the directory when the compiled site is stored.
70+
*
71+
* Warning! This directory is emptied when compiling the site.
72+
*/
73+
protected function storeCompiledSiteIn(string $directory): void
74+
{
75+
StaticPageBuilder::$outputPath = $directory;
76+
}
4477
}

packages/framework/src/HydeServiceProvider.php

+21-35
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,13 @@ public function register(): void
4141
DocumentationPage::class => config('docs.output_directory', 'docs'),
4242
]);
4343

44-
$this->discoverBladeViewsIn('_pages');
45-
4644
$this->storeCompiledSiteIn(Hyde::path(
4745
unslash(config('hyde.output_directory', '_site'))
4846
));
4947

50-
$this->commands([
51-
Commands\HydePublishHomepageCommand::class,
52-
Commands\HydeUpdateConfigsCommand::class,
53-
Commands\HydePublishViewsCommand::class,
54-
Commands\HydeRebuildStaticSiteCommand::class,
55-
Commands\HydeBuildStaticSiteCommand::class,
56-
Commands\HydeBuildSitemapCommand::class,
57-
Commands\HydeBuildRssFeedCommand::class,
58-
Commands\HydeBuildSearchCommand::class,
59-
Commands\HydeMakePostCommand::class,
60-
Commands\HydeMakePageCommand::class,
61-
Commands\HydeValidateCommand::class,
62-
Commands\HydeInstallCommand::class,
63-
Commands\HydeDebugCommand::class,
64-
Commands\HydeServeCommand::class,
65-
66-
Commands\HydePackageDiscoverCommand::class,
67-
]);
48+
$this->discoverBladeViewsIn(BladePage::getSourceDirectory());
6849

50+
$this->registerHydeConsoleCommands();
6951
$this->registerModuleServiceProviders();
7052
}
7153

@@ -100,24 +82,28 @@ public function boot(): void
10082
}
10183

10284
/**
103-
* If you are loading Blade views from a different directory,
104-
* you need to add the path to the view.php config. This is
105-
* here done automatically when registering this provider.
85+
* Register the HydeCLI console commands.
10686
*/
107-
protected function discoverBladeViewsIn(string $directory): void
87+
protected function registerHydeConsoleCommands(): void
10888
{
109-
config(['view.paths' => array_merge(
110-
config('view.paths', []),
111-
[base_path($directory)]
112-
)]);
113-
}
89+
$this->commands([
90+
Commands\HydePublishHomepageCommand::class,
91+
Commands\HydeUpdateConfigsCommand::class,
92+
Commands\HydePublishViewsCommand::class,
93+
Commands\HydeRebuildStaticSiteCommand::class,
94+
Commands\HydeBuildStaticSiteCommand::class,
95+
Commands\HydeBuildSitemapCommand::class,
96+
Commands\HydeBuildRssFeedCommand::class,
97+
Commands\HydeBuildSearchCommand::class,
98+
Commands\HydeMakePostCommand::class,
99+
Commands\HydeMakePageCommand::class,
100+
Commands\HydeValidateCommand::class,
101+
Commands\HydeInstallCommand::class,
102+
Commands\HydeDebugCommand::class,
103+
Commands\HydeServeCommand::class,
114104

115-
/**
116-
* The absolute path to the directory when the compiled site is stored.
117-
*/
118-
protected function storeCompiledSiteIn(string $directory): void
119-
{
120-
StaticPageBuilder::$outputPath = $directory;
105+
Commands\HydePackageDiscoverCommand::class,
106+
]);
121107
}
122108

123109
/**

packages/framework/tests/Unit/HydeServiceProviderTest.php

+128-2
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,34 @@
22

33
namespace Hyde\Framework\Testing\Unit;
44

5+
use Hyde\Framework\Contracts\AssetServiceContract;
6+
use Hyde\Framework\Hyde;
57
use Hyde\Framework\HydeServiceProvider;
8+
use Hyde\Framework\Models\Pages\BladePage;
9+
use Hyde\Framework\Models\Pages\DocumentationPage;
10+
use Hyde\Framework\Models\Pages\MarkdownPage;
11+
use Hyde\Framework\Models\Pages\MarkdownPost;
12+
use Hyde\Framework\Modules\DataCollections\DataCollectionServiceProvider;
13+
use Hyde\Framework\Services\AssetService;
14+
use Hyde\Framework\StaticPageBuilder;
615
use Hyde\Testing\TestCase;
16+
use Illuminate\Support\Facades\Artisan;
717

18+
/**
19+
* @todo #162 Improve testing for this class.
20+
*
21+
* @covers \Hyde\Framework\HydeServiceProvider
22+
* @covers \Hyde\Framework\Concerns\RegistersFileLocations
23+
*/
824
class HydeServiceProviderTest extends TestCase
925
{
1026
protected HydeServiceProvider $provider;
1127

1228
public function setUp(): void
1329
{
14-
$this->provider = new HydeServiceProvider(app());
15-
1630
parent::setUp();
31+
32+
$this->provider = new HydeServiceProvider(app());
1733
}
1834

1935
public function test_provider_is_constructed()
@@ -30,4 +46,114 @@ public function test_provider_has_boot_method()
3046
{
3147
$this->assertTrue(method_exists($this->provider, 'boot'));
3248
}
49+
50+
public function test_provider_registers_asset_service_contract()
51+
{
52+
$this->assertTrue($this->app->bound(AssetServiceContract::class));
53+
$this->assertInstanceOf(AssetServiceContract::class, $this->app->make(AssetServiceContract::class));
54+
$this->assertInstanceOf(AssetService::class, $this->app->make(AssetServiceContract::class));
55+
}
56+
57+
public function test_provider_registers_source_directories()
58+
{
59+
BladePage::$sourceDirectory = '';
60+
MarkdownPage::$sourceDirectory = '';
61+
MarkdownPost::$sourceDirectory = '';
62+
DocumentationPage::$sourceDirectory = '';
63+
64+
$this->assertEquals('', BladePage::getSourceDirectory());
65+
$this->assertEquals('', MarkdownPage::getSourceDirectory());
66+
$this->assertEquals('', MarkdownPost::getSourceDirectory());
67+
$this->assertEquals('', DocumentationPage::getSourceDirectory());
68+
69+
$this->provider->register();
70+
71+
$this->assertEquals('_pages', BladePage::getSourceDirectory());
72+
$this->assertEquals('_pages', MarkdownPage::getSourceDirectory());
73+
$this->assertEquals('_posts', MarkdownPost::getSourceDirectory());
74+
$this->assertEquals('_docs', DocumentationPage::getSourceDirectory());
75+
}
76+
77+
public function test_provider_registers_output_directories()
78+
{
79+
BladePage::$outputDirectory = 'foo';
80+
MarkdownPage::$outputDirectory = 'foo';
81+
MarkdownPost::$outputDirectory = 'foo';
82+
DocumentationPage::$outputDirectory = 'foo';
83+
84+
$this->assertEquals('foo', BladePage::getOutputDirectory());
85+
$this->assertEquals('foo', MarkdownPage::getOutputDirectory());
86+
$this->assertEquals('foo', MarkdownPost::getOutputDirectory());
87+
$this->assertEquals('foo', DocumentationPage::getOutputDirectory());
88+
89+
$this->provider->register();
90+
91+
$this->assertEquals('', BladePage::getOutputDirectory());
92+
$this->assertEquals('', MarkdownPage::getOutputDirectory());
93+
$this->assertEquals('posts', MarkdownPost::getOutputDirectory());
94+
$this->assertEquals('docs', DocumentationPage::getOutputDirectory());
95+
}
96+
97+
public function test_provider_registers_configured_documentation_output_directory()
98+
{
99+
$this->assertEquals('docs', DocumentationPage::getOutputDirectory());
100+
101+
config(['docs.output_directory' => 'foo']);
102+
103+
$this->provider->register();
104+
105+
$this->assertEquals('foo', DocumentationPage::getOutputDirectory());
106+
}
107+
108+
public function test_provider_registers_site_output_directory()
109+
{
110+
$this->assertEquals(Hyde::path('_site'), StaticPageBuilder::$outputPath);
111+
112+
config(['hyde.output_directory' => 'foo']);
113+
114+
$this->provider->register();
115+
116+
$this->assertEquals(Hyde::path('foo'), StaticPageBuilder::$outputPath);
117+
}
118+
119+
public function test_provider_registers_blade_view_discovery_location_for_configured_blade_view_path()
120+
{
121+
config(['view.paths' => []]);
122+
$this->assertEquals([], config('view.paths'));
123+
124+
$this->provider->register();
125+
126+
$this->assertEquals([Hyde::path('_pages')], config('view.paths'));
127+
}
128+
129+
public function test_blade_view_locations_are_only_registered_once_per_key()
130+
{
131+
config(['view.paths' => []]);
132+
$this->assertEquals([], config('view.paths'));
133+
134+
$this->provider->register();
135+
$this->provider->register();
136+
137+
$this->assertEquals([Hyde::path('_pages')], config('view.paths'));
138+
}
139+
140+
public function test_provider_registers_console_commands()
141+
{
142+
$commands = array_map(function ($command) {
143+
return get_class($command);
144+
}, Artisan::all());
145+
146+
foreach (glob(Hyde::vendorPath('src/Commands/*.php')) as $file) {
147+
$class = 'Hyde\Framework\Commands\\'.basename($file, '.php');
148+
149+
$this->assertContains($class, $commands);
150+
}
151+
}
152+
153+
public function test_provider_registers_additional_module_service_providers()
154+
{
155+
$this->provider->register();
156+
157+
$this->assertArrayHasKey(DataCollectionServiceProvider::class, $this->app->getLoadedProviders());
158+
}
33159
}

0 commit comments

Comments
 (0)