TranslationServiceProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A provides() 0 3 1
A register() 0 23 2
1
<?php
2
3
namespace JosephNC\Translation;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Support\Facades\Blade;
7
use Illuminate\Support\ServiceProvider;
8
9
class TranslationServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Indicates if loading of the provider is deferred.
13
     *
14
     * @var bool
15
     */
16
    protected $defer = false;
17
18
    /**
19
     * Set up the blade directive.
20
     */
21
    public function boot()
22
    {
23
        Blade::directive('trans', function ( string $text, array $replacements = [], string $to_locale = '' ) {
24
            $translation = __trans( $text, $replacements, $to_locale );
25
26
            return "<?php echo $translation ?>";
27
        });
28
    }
29
30
    /**
31
     * Register the service provider.
32
     *
33
     * @method void package(string $package, string $namespace, string $path)
34
     */
35
    public function register()
36
    {
37
        if (PHP_SESSION_NONE == session_status()) session_start(); // Start Session
38
39
        // Allow configuration to be publishable.
40
        $this->publishes([
41
            __DIR__.'/Config/config.php' => config_path('translation.php'),
42
        ], 'config');
43
44
        // Allow migrations to be publishable.
45
        $this->publishes([
46
            __DIR__.'/Migrations/' => base_path('/database/migrations'),
47
        ], 'migrations');
48
49
        // Include the helpers file for global `__trans()` function
50
        require_once __DIR__.'/helpers.php';
51
52
        // Bind translation to the IoC.
53
        $this->app->bind( 'translation', function ( Application $app ) {
54
            return new Translation( $app );
55
        } );
56
57
        register_shutdown_function( [ $this->app->translation, 'shutdown' ] );
0 ignored issues
show
Bug introduced by
Accessing translation on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
58
    }
59
60
    /**
61
     * Get the services provided by the provider.
62
     *
63
     * @return array
64
     */
65
    public function provides() : array
66
    {
67
        return ['translation'];
68
    }
69
}
70