1 | <?php |
||
2 | |||
3 | namespace Sirgrimorum\JSLocalization; |
||
4 | |||
5 | use Illuminate\Support\ServiceProvider; |
||
6 | use Illuminate\Foundation\AliasLoader; |
||
7 | use Blade; |
||
8 | |||
9 | class JSLocalizationServiceProvider extends ServiceProvider { |
||
10 | |||
11 | /** |
||
12 | * Bootstrap services. |
||
13 | * |
||
14 | * @return void |
||
15 | */ |
||
16 | public function boot() { |
||
17 | $this->publishes([ |
||
18 | __DIR__ . '/Config/jslocalization.php' => config_path('sirgrimorum/jslocalization.php'), |
||
19 | ], 'config'); |
||
20 | |||
21 | Blade::directive('jslocalization', function($expression) { |
||
22 | $auxExpression = explode(',', str_replace(['(', ')', ' ', '"', "'"], '', $expression)); |
||
23 | if (count($auxExpression) > 2) { |
||
24 | $langfile = $auxExpression[0]; |
||
25 | $group = $auxExpression[1]; |
||
26 | $basevar = $auxExpression[2]; |
||
27 | } elseif (count($auxExpression) > 1) { |
||
28 | $langfile = $auxExpression[0]; |
||
29 | $group = $auxExpression[1]; |
||
30 | $basevar = ""; |
||
31 | } else { |
||
32 | $langfile = $auxExpression[0]; |
||
33 | $group = ""; |
||
34 | $basevar = ""; |
||
35 | } |
||
36 | $translations = new \Sirgrimorum\JSLocalization\BindTranslationsToJs($this->app, config('sirgrimorum.jslocalization.bind_trans_vars_to_this_view', 'layout.app'), config('sirgrimorum.jslocalization.trans_group', 'messages'), config('sirgrimorum.jslocalization.default_base_var', 'translations')); |
||
37 | return $translations->get($langfile, $group, $basevar); |
||
38 | }); |
||
39 | Blade::directive('jsmodel', function($expression) { |
||
40 | $auxExpression = explode(',', str_replace([' ', '"', "'"], '', $expression)); |
||
41 | //echo "<pre>" . print_r($auxExpression, true) . "</pre>"; |
||
42 | if (count($auxExpression) > 1) { |
||
43 | if ($auxExpression[0] == 'Auth::user()') { |
||
44 | $model = \Auth::user(); |
||
45 | } else { |
||
46 | if (stripos($auxExpression[0], "::") !== false) { |
||
47 | list($base, $comando) = explode("::", $auxExpression[0]); |
||
48 | if (stripos($auxExpression[0], "->") !== false) { |
||
49 | $comandos = explode("->", $comando); |
||
50 | $cuenta = 0; |
||
51 | foreach ($comandos as $subComando) { |
||
52 | if (stripos($subComando, "()") !== false) { |
||
53 | $subComando = str_replace("()", "", $subComando); |
||
54 | if ($cuenta == 0) { |
||
55 | $model = $base::{$subComando}(); |
||
56 | } else { |
||
57 | $model = $model->{$subComando}(); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
58 | } |
||
59 | } else { |
||
60 | if ($cuenta == 0) { |
||
61 | $model = $base::$subComando; |
||
0 ignored issues
–
show
|
|||
62 | } else { |
||
63 | $model = $model->$subComando; |
||
64 | } |
||
65 | } |
||
66 | $cuenta++; |
||
67 | } |
||
68 | } else { |
||
69 | if (stripos($comando, "()") !== false) { |
||
70 | $comando = str_replace("()", "", $comando); |
||
71 | $model = $base::{$comando}(); |
||
72 | } else { |
||
73 | $model = $base::$comando; |
||
0 ignored issues
–
show
|
|||
74 | } |
||
75 | } |
||
76 | } else { |
||
77 | $model = $auxExpression[0]; |
||
78 | } |
||
79 | } |
||
80 | $variable = $auxExpression[1]; |
||
81 | } else { |
||
82 | $model = $auxExpression[0]; |
||
83 | $variable = ""; |
||
84 | } |
||
85 | $translations = new \Sirgrimorum\JSLocalization\BindTranslationsToJs($this->app, config('sirgrimorum.jslocalization.bind_trans_vars_to_this_view', 'layout.app'), config('sirgrimorum.jslocalization.trans_group', 'messages'), config('sirgrimorum.jslocalization.default_base_var', 'translations')); |
||
86 | return $translations->put($model, $variable); |
||
87 | }); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Register services. |
||
92 | * |
||
93 | * @return void |
||
94 | */ |
||
95 | public function register() { |
||
96 | $this->app->singleton(BindTranslationsToJs::class, function($app) { |
||
97 | $view = config('sirgrimorum.jslocalization.bind_trans_vars_to_this_view', 'welcome'); |
||
98 | $group = config('sirgrimorum.jslocalization.trans_group', 'mensajes'); |
||
99 | $basevar = config('sirgrimorum.jslocalization.default_base_var', 'translations'); |
||
100 | |||
101 | return new BindTranslationsToJs($app, $view, $group, $basevar); |
||
102 | }); |
||
103 | $loader = AliasLoader::getInstance(); |
||
104 | $loader->alias( |
||
105 | 'JSLocalization', \Sirgrimorum\JSLocalization\BindTranslationsToJs::class |
||
106 | ); |
||
107 | //$this->app->alias(BindTranslationsToJs::class, 'JSLocalization'); |
||
108 | $this->mergeConfigFrom( |
||
109 | __DIR__ . '/Config/jslocalization.php', 'sirgrimorum.jslocalization' |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | } |
||
114 |