-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
SheetsServiceProvider.php
90 lines (73 loc) · 2.7 KB
/
SheetsServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Spatie\Sheets;
use Illuminate\Support\ServiceProvider;
use League\CommonMark\CommonMarkConverter;
use Spatie\Sheets\ContentParsers\MarkdownParser;
use Spatie\Sheets\ContentParsers\MarkdownWithFrontMatterParser;
use Spatie\Sheets\PathParsers\SlugParser;
use Spatie\Sheets\Repositories\FilesystemRepository;
class SheetsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/sheets.php' => config_path('sheets.php'),
], 'config');
}
}
/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/sheets.php', 'sheets');
$this->app->when(MarkdownWithFrontMatterParser::class)
->needs(CommonMarkConverter::class)
->give(function () {
return new CommonMarkConverter();
});
$this->app->when(MarkdownParser::class)
->needs(CommonMarkConverter::class)
->give(function () {
return new CommonMarkConverter();
});
$this->app->singleton(Sheets::class, function () {
$sheets = new Sheets();
foreach (config('sheets.collections', []) as $name => $config) {
if (is_int($name)) {
$name = $config;
$config = [];
}
$config = $this->mergeCollectionConfigWithDefaults($name, $config);
$factory = new Factory(
$this->app->make($config['path_parser']),
$this->app->make($config['content_parser']),
$config['sheet_class']
);
$repository = $this->app->make($config['repository'], compact('factory', 'config'));
$sheets->registerCollection($name, $repository);
}
if (config('sheets.default_collection')) {
$sheets->setDefaultCollection(config('sheets.default_collection'));
}
return $sheets;
});
$this->app->alias(Sheets::class, 'sheets');
}
protected function mergeCollectionConfigWithDefaults(string $name, array $config): array
{
$defaults = [
'disk' => $name,
'sheet_class' => Sheet::class,
'path_parser' => SlugParser::class,
'content_parser' => MarkdownWithFrontMatterParser::class,
'extension' => 'md',
'repository' => FilesystemRepository::class,
];
return array_merge($defaults, $config);
}
}