Completed
Push — master ( 457f25...cbe6f3 )
by Henri
03:03
created

Plugins::getPluginsJS()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 7
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
class Plugins {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
    private $globalPlugins;
5
6
    public function __construct($globalPlugins=array()) {
7
        $this->globalPlugins = $globalPlugins;
8
    }
9
    
10
    /**
11
     * Returns the plugin configurations found from plugin folders inside the plugins folder
12
     * @return array
13
     */
14
    private function getPlugins() 
15
    {
16
        $plugins = array();
17
        $pluginconfs = glob('plugins/*/plugin.json');
18
        foreach ($pluginconfs as $path) {
19
            $folder = explode('/', $path)[1];
20
            if (file_exists($path)) {
21
                $plugins[$folder] = json_decode(file_get_contents($path), true);
22
            }
23
        }
24
        return $plugins;
25
    }
26
27
    /**
28
     * Returns the plugin configurations found from plugin folders 
29
     * inside the plugins folder filtered by filetype.
30
     * @param string $type filetype e.g. 'css', 'js' or 'template'
31
     * @return array
32
     */
33
    private function filterPlugins($type) {
34
        $plugins = $this->getPlugins();
35
        $ret = array();
36
        if (!empty($plugins)) {
37
            foreach ($plugins as $name => $files) {
38
                if (isset($files[$type])) {
39
                    $ret[$name] = array();
40
                    foreach ($files[$type] as $file) {
41
                        array_push($ret[$name], 'plugins/' . $name . '/' . $file);
42
                    }
43
                }
44
            } 
45
        }
46
        return $ret;
47
    }
48
49
    /**
50
     * Returns the plugin configurations found from plugin folders 
51
     * inside the plugins folder filtered by plugin name (the folder name).
52
     * @param string $type filetype e.g. 'css', 'js' or 'template'
53
     * @param array $names the plugin name strings (foldernames) in an array 
54
     * @return array
55
     */
56
    private function filterPluginsByName($type, $names) {
57
        $files = $this->filterPlugins($type);
58
        foreach ($files as $plugin => $filelist) {
59
            if (!in_array($plugin, $names)) {
60
                unset($files[$plugin]);
61
            }
62
        }
63
        return $files;
64
    }
65
66
    /**
67
     * Returns an array of javascript filepaths
68
     * @param array $names the plugin name strings (foldernames) in an array 
69
     * @return array
70
     */
71 View Code Duplication
    public function getPluginsJS($names=null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
        if ($names) {
73
            $names = array_merge($this->globalPlugins, $names);
74
            return $this->filterPluginsByName('js', $names);
75
        }
76
        return $this->filterPluginsByName('js', $this->globalPlugins);
77
    }
78
79
    /**
80
     * Returns an array of css filepaths
81
     * @param array $names the plugin name strings (foldernames) in an array 
82
     * @return array
83
     */
84 View Code Duplication
    public function getPluginsCSS($names=null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
        if ($names) {
86
            $names = array_merge($this->globalPlugins, $names);
87
            return $this->filterPluginsByName('css', $names);
88
        }
89
        return $this->filterPluginsByName('css', $this->globalPlugins);
90
    }
91
92
    /**
93
     * Returns an array of template filepaths
94
     * @param array $names the plugin name strings (foldernames) in an array 
95
     * @return array
96
     */
97 View Code Duplication
    public function getPluginsTemplates($names=null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
        if ($names) {
99
            $names = array_merge($this->globalPlugins, $names);
100
            return $this->filterPluginsByName('templates', $names);
101
        }
102
        return $this->filterPluginsByName('templates', $this->globalPlugins);
103
    }
104
105
    /**
106
     * Returns an array of template files contents as strings
107
     * @param array $names the plugin name strings (foldernames) in an array 
108
     * @return array
109
     */
110
    public function getTemplates($names=null) {
111
        $templateStrings = array();
112
        $plugins = $this->getPluginsTemplates($names);
113
        foreach ($plugins as $folder => $templates) {
114
            foreach ($templates as $path) {
115
                if (file_exists($path)) {
116
                    $filename = explode('/', $path)[sizeof(explode('/', $path))-1];
117
                    $id = $folder . '-' . substr($filename, 0 , (strrpos($filename, ".")));
118
                    $templateStrings[$id] = file_get_contents($path);
119
                }
120
            }
121
        }
122
        return $templateStrings;
123
    }
124
}
125