Mememe

Php code posted
created at 28 Dec 14:43, updated at 17 Jan 15:32

Edit | Back
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Validator;
use Blade;

class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    /**
     * App validator rules
     */
    Validator::resolver(function($translator, $data, $rules, $messages)
    {
      return new \App\Validators\ExistsValidator($translator, $data, $rules, $messages);
    });
  }

  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    /**
     * Register Blade Template Extensions.
     */
    // Add @continue for Loops
    Blade::directive('continue', function($expression)
    {
      return '<?php continue; ?>';
    });
    // Add @break for Loops
    Blade::directive('break', function($expression)
    {
      return '<?php break; ?>';
    });
    // Add @dd for debugging
    Blade::directive('dd', function ($expression)
    {
      return "<?php dd({$expression}); ?>";
    });
    // Add @debug for debugging
    Blade::directive('debug', function ($expression)
    {
      return "<?php debug({$expression}); ?>";
    });
    // Set variable
    Blade::directive('set', function ($expression)
    {
      // Break the Expression into Pieces
      $segments = explode(',', $expression, 2);

      // Return the Conversion
      return "<?php " . $segments[0] . " = " . $segments[1] . "; ?>";
    });

    /**
     * ACL
     */
    // has(module) :
    Blade::directive('module', function($expression) {
      return "<?php if (Auth::check() && Auth::user()->hasModule({$expression})): ?>";
    });
    Blade::directive('endmodule', function() {
      return "<?php endif; ?>";
    });

    // is(role) :
    Blade::directive('role', function($expression) {
      return "<?php if (Auth::check() && Auth::user()->isRole({$expression})): ?>";
    });
    Blade::directive('endrole', function() {
      return "<?php endif; ?>";
    });

    // can(permission) :
    Blade::directive('permission', function($expression) {
      return "<?php if (Auth::check() && Auth::user()->canPermission({$expression})): ?>";
    });
    Blade::directive('endpermission', function() {
      return "<?php endif; ?>";
    });

    // Switch case (blade) [Exemple dans sidebar/menu.blade.php]
    // http://stackoverflow.com/questions/29897508/switch-in-laravel-5-blade
    Blade::extend(function($value, $compiler)
    {
      $value = preg_replace('/(\s*)@switch\((.*)\)(?=\s)/', '$1<?php switch($2):', $value);
      $value = preg_replace('/(\s*)@endswitch(?=\s)/', '$1endswitch; ?>', $value);
      $value = preg_replace('/(\s*)@case\((.*)\)(?=\s)/', '$1case $2: ?>', $value);
      $value = preg_replace('/(?<=\s)@default(?=\s)/', 'default: ?>', $value);
      $value = preg_replace('/(?<=\s)@breakswitch(?=\s)/', '<?php break;', $value);

      return $value;
    });
  }
}
2.79 KB in 6 ms with coderay