1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Attributes\Providers; |
6
|
|
|
|
7
|
|
|
use Illuminate\Routing\Router; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
10
|
|
|
use Rinvex\Attributes\Contracts\AttributeContract; |
11
|
|
|
use Cortex\Attributes\Console\Commands\SeedCommand; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
13
|
|
|
use Cortex\Attributes\Console\Commands\InstallCommand; |
14
|
|
|
use Cortex\Attributes\Console\Commands\MigrateCommand; |
15
|
|
|
use Cortex\Attributes\Console\Commands\PublishCommand; |
16
|
|
|
use Cortex\Attributes\Console\Commands\RollbackCommand; |
17
|
|
|
|
18
|
|
|
class AttributesServiceProvider extends ServiceProvider |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The commands to be registered. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $commands = [ |
26
|
|
|
SeedCommand::class => 'command.cortex.attributes.seed', |
27
|
|
|
InstallCommand::class => 'command.cortex.attributes.install', |
28
|
|
|
MigrateCommand::class => 'command.cortex.attributes.migrate', |
29
|
|
|
PublishCommand::class => 'command.cortex.attributes.publish', |
30
|
|
|
RollbackCommand::class => 'command.cortex.attributes.rollback', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Register any application services. |
35
|
|
|
* |
36
|
|
|
* This service provider is a great spot to register your various container |
37
|
|
|
* bindings with the application. As you can see, we are registering our |
38
|
|
|
* "Registrar" implementation here. You can add your own bindings too! |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function register() |
43
|
|
|
{ |
44
|
|
|
// Register console commands |
45
|
|
|
! $this->app->runningInConsole() || $this->registerCommands(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Bootstrap any application services. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function boot(Router $router) |
54
|
|
|
{ |
55
|
|
|
// Bind route models and constrains |
56
|
|
|
$router->pattern('attribute', '[a-z0-9-]+'); |
57
|
|
|
$router->model('attribute', AttributeContract::class); |
58
|
|
|
|
59
|
|
|
// Map relations |
60
|
|
|
Relation::morphMap([ |
61
|
|
|
'attribute' => config('rinvex.attributes.models.attribute'), |
62
|
|
|
]); |
63
|
|
|
|
64
|
|
|
// Load resources |
65
|
|
|
require __DIR__.'/../../routes/breadcrumbs.php'; |
66
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web.php'); |
67
|
|
|
$this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/attributes'); |
68
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/attributes'); |
69
|
|
|
$this->app->afterResolving('blade.compiler', function () { |
70
|
|
|
require __DIR__.'/../../routes/menus.php'; |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
// Register blade extensions |
74
|
|
|
$this->registerBladeExtensions(); |
75
|
|
|
|
76
|
|
|
// Publish Resources |
77
|
|
|
! $this->app->runningInConsole() || $this->publishResources(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Publish resources. |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
protected function publishResources() |
86
|
|
|
{ |
87
|
|
|
$this->publishes([realpath(__DIR__.'/../../resources/lang') => resource_path('lang/vendor/cortex/attributes')], 'cortex-attributes-lang'); |
|
|
|
|
88
|
|
|
$this->publishes([realpath(__DIR__.'/../../resources/views') => resource_path('views/vendor/cortex/attributes')], 'cortex-attributes-views'); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Register console commands. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
protected function registerCommands() |
97
|
|
|
{ |
98
|
|
|
// Register artisan commands |
99
|
|
|
foreach ($this->commands as $key => $value) { |
100
|
|
|
$this->app->singleton($value, function ($app) use ($key) { |
|
|
|
|
101
|
|
|
return new $key(); |
102
|
|
|
}); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->commands(array_values($this->commands)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Register the blade extensions. |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
protected function registerBladeExtensions() |
114
|
|
|
{ |
115
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
116
|
|
|
// @attributes($entity) |
117
|
|
|
$bladeCompiler->directive('attributes', function ($expression) { |
118
|
|
|
return "<?php echo {$expression}->getEntityAttributes()->map->render({$expression}, request('accessarea'))->implode(''); ?>"; |
|
|
|
|
119
|
|
|
}); |
120
|
|
|
}); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.