Completed
Push — develop ( 5f1475...a7e293 )
by Abdelrahman
12:42 queued 11:21
created

AttributesServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 96
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 9 3
B boot() 0 39 5
A registerBladeExtensions() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Attributes\Providers;
6
7
use Illuminate\Routing\Router;
8
use Cortex\Attributes\Models\Attribute;
9
use Illuminate\Support\ServiceProvider;
10
use Rinvex\Support\Traits\ConsoleTools;
11
use Illuminate\View\Compilers\BladeCompiler;
12
use Cortex\Attributes\Console\Commands\SeedCommand;
13
use Illuminate\Database\Eloquent\Relations\Relation;
14
use Cortex\Attributes\Console\Commands\InstallCommand;
15
use Cortex\Attributes\Console\Commands\MigrateCommand;
16
use Cortex\Attributes\Console\Commands\PublishCommand;
17
use Cortex\Attributes\Console\Commands\RollbackCommand;
18
19
class AttributesServiceProvider extends ServiceProvider
20
{
21
    use ConsoleTools;
22
23
    /**
24
     * The commands to be registered.
25
     *
26
     * @var array
27
     */
28
    protected $commands = [
29
        SeedCommand::class => 'command.cortex.attributes.seed',
30
        InstallCommand::class => 'command.cortex.attributes.install',
31
        MigrateCommand::class => 'command.cortex.attributes.migrate',
32
        PublishCommand::class => 'command.cortex.attributes.publish',
33
        RollbackCommand::class => 'command.cortex.attributes.rollback',
34
    ];
35
36
    /**
37
     * Register any application services.
38
     *
39
     * This service provider is a great spot to register your various container
40
     * bindings with the application. As you can see, we are registering our
41
     * "Registrar" implementation here. You can add your own bindings too!
42
     *
43
     * @return void
44
     */
45
    public function register(): void
46
    {
47
        // Bind eloquent models to IoC container
48
        $this->app['config']['rinvex.attributes.models.attribute'] === Attribute::class
49
        || $this->app->alias('rinvex.attributes.attribute', Attribute::class);
50
51
        // Register console commands
52
        ! $this->app->runningInConsole() || $this->registerCommands();
53
    }
54
55
    /**
56
     * Bootstrap any application services.
57
     *
58
     * @return void
59
     */
60
    public function boot(Router $router): void
61
    {
62
        // Bind route models and constrains
63
        $router->pattern('attribute', '[a-zA-Z0-9-]+');
64
        $router->model('attribute', config('rinvex.attributes.models.attribute'));
65
66
        // Map relations
67
        Relation::morphMap([
68
            'attribute' => config('rinvex.attributes.models.attribute'),
69
        ]);
70
71
        // Load resources
72
        require __DIR__.'/../../routes/breadcrumbs/adminarea.php';
73
        $this->loadRoutesFrom(__DIR__.'/../../routes/web/adminarea.php');
74
        $this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/attributes');
75
        $this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/attributes');
76
        $this->app->runningInConsole() || $this->app->afterResolving('blade.compiler', function () {
77
            require __DIR__.'/../../routes/menus/adminarea.php';
78
        });
79
80
        // Add default attributes types
81
        Attribute::typeMap([
82
            'integer' => \Rinvex\Attributes\Models\Type\Integer::class,
83
            'boolean' => \Rinvex\Attributes\Models\Type\Boolean::class,
84
            'select' => \Rinvex\Attributes\Models\Type\Varchar::class,
85
            'textarea' => \Rinvex\Attributes\Models\Type\Text::class,
86
            'radio' => \Rinvex\Attributes\Models\Type\Varchar::class,
87
            'check' => \Rinvex\Attributes\Models\Type\Varchar::class,
88
            'text' => \Rinvex\Attributes\Models\Type\Varchar::class,
89
        ]);
90
91
        // Register blade extensions
92
        $this->registerBladeExtensions();
93
94
        // Publish Resources
95
        ! $this->app->runningInConsole() || $this->publishesLang('cortex/attributes');
96
        ! $this->app->runningInConsole() || $this->publishesViews('cortex/attributes');
97
        ! $this->app->runningInConsole() || $this->publishesMigrations('cortex/attributes');
98
    }
99
100
    /**
101
     * Register the blade extensions.
102
     *
103
     * @return void
104
     */
105
    protected function registerBladeExtensions(): void
106
    {
107
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
108
            // @attributes($entity)
109
            $bladeCompiler->directive('attributes', function ($expression) {
110
                return "<?php echo {$expression}->getEntityAttributes()->map->render({$expression}, request()->route('accessarea'))->implode('') ?: view('cortex/attributes::".request()->route('accessarea').".partials.no-results'); ?>";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 235 characters

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.

Loading history...
111
            });
112
        });
113
    }
114
}
115