Issues (4)

libraries/VueLoader.php (4 issues)

1
<?php
2
defined('BASEPATH') OR exit('No direct script access allowed');
3
4
class VueLoader
5
{
6
  /**
7
   * [groups array from @vue config.]
8
   *
9
   * @var array
10
   */
11
  private $groups;
12
13
  /**
14
   * [__construct Class Constructor]
15
   *
16
   * @date  2020-02-09
17
   *
18
   * @param array|null $params Initialization array. config values in
19
   *                           app/vue.php will be passed here if loaded with
20
   *                           $this->load->package('francis94c/vue-ci');
21
   */
22
  function __construct(?array $params=null)
23
  {
24
    get_instance()->load->helper('inflector');
0 ignored issues
show
The function get_instance was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
    /** @scrutinizer ignore-call */ 
25
    get_instance()->load->helper('inflector');
Loading history...
25
26
    if ($params) $this->groups = $params['groups'] ?? [];
27
  }
28
29
  /**
30
   * [loadGroup Load Vue Component Group.]
31
   *
32
   * @date  2020-02-09
33
   *
34
   * @param string $groupName Group Name.
35
   */
36
  public function loadGroup(string $groupName):void
37
  {
38
    $prefix = $this->groups[$groupName]['components_view_prefix'] ?? '';
39
    foreach ($this->groups[$groupName]['components'] ?? [] as $component) {
40
      $this->loadComponent($prefix.$component);
41
    }
42
    foreach ($this->groups[$groupName]['scripts'] ?? [] as $script) {
43
      $this->loadScript($script);
44
    }
45
  }
46
47
  /**
48
   * [loadScript Load a Code Ignoter View file (PHP) as JS, stripping off any
49
   * opening and closing <script> tags contained wihin and minifying the
50
   * resulting script.]
51
   *
52
   * @date  2020-02-09
53
   *
54
   * @param string $viewPath Path to View, Pass what you would pass to
55
   * $this->load->view if you were in a Controller.
56
   */
57
  public function loadScript(string $viewPath):void
58
  {
59
    $script = get_instance()->load->view($viewPath, null, true);
0 ignored issues
show
The function get_instance was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
    $script = /** @scrutinizer ignore-call */ get_instance()->load->view($viewPath, null, true);
Loading history...
60
    $script = preg_replace('/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/m', '$1', $script);
61
    echo preg_replace('/(<script>|<script type="text\/javascript">|<\/script>|\r|\n|  )/', '', $script);
62
  }
63
64
  /**
65
   * [loadComponent Load a Code Ignoter View file (PHP) as Vue Component (.vue file),
66
   * stripping off any opening and closing <script> tags contained wihin and
67
   * minifying the resulting script. It additionally processes the contents of
68
   * the <template> block and passes it as a property to the component script
69
   * template field. The contents of the <script> block are equally assigned to
70
   * a variable matching the given $viewPath in camel casing with the words 'vue'
71
   * and 'components' ommited from it.]
72
   *
73
   * @date  2020-02-09
74
   *
75
   * @param string $viewPath Path to View, Pass what you would pass to
76
   * $this->load->view if you were in a Controller.
77
   */
78
  public function loadComponent($viewPath):void
79
  {
80
    $component = get_instance()->load->view($viewPath, null, true);
0 ignored issues
show
The function get_instance was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
    $component = /** @scrutinizer ignore-call */ get_instance()->load->view($viewPath, null, true);
Loading history...
81
82
    preg_match_all('/<template>.+<\/template>/s', $component, $matches);
83
    $template = $this->prepare_template($matches[0][0]);
84
85
    preg_match_all('/<script>.+<\/script>/s', $component, $matches);
86
    $script = $matches[0][0];
87
88
    $script = preg_replace('/template:(.)+,/', '', $script);
89
90
    if (preg_match('/{/', $script, $matches, PREG_OFFSET_CAPTURE)) {
91
      $script = substr_replace($script, "template: '$template', ", $matches[0][1] + 1, 0);
92
    }
93
94
    $script = preg_replace('/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/m', '$1', $script);
95
96
    $script = preg_replace('/(<script>|<script type="text\/javascript">|<\/script>|\r|\n|  )/', '', $script);
97
98
    $viewPath = str_replace('vue/', '', $viewPath);
99
    $viewPath = str_replace('components/', '', $viewPath);
100
101
    $script = 'let ' . camelize(preg_replace('/(-|\/|\\\)/', '_', $viewPath)) . ' = ' . $script;
0 ignored issues
show
The function camelize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
    $script = 'let ' . /** @scrutinizer ignore-call */ camelize(preg_replace('/(-|\/|\\\)/', '_', $viewPath)) . ' = ' . $script;
Loading history...
102
103
    echo $script;
104
  }
105
106
  /**
107
   * [prepare_template Minifies the contents of a <template> block, escapes
108
   * single quote characters and strips of the <template> tags.]
109
   *
110
   * @date   2020-02-09
111
   *
112
   * @param  string $template String (HTML) enclosed in <template> tags.
113
   * @return string           Processed/Prepared template string.
114
   */
115
  private function prepare_template(string $template):string
116
  {
117
    $template = preg_replace('/(<(|\/)template>)/', '', $template);
118
    $template = preg_replace('/(\r|\n|\r\n)/', '', $template);
119
    return trim(str_replace("'", "\\'", $template));
120
  }
121
}
122