Completed
Push — master ( 4697b9...ca7fa2 )
by Nazar
04:04
created

RequireJS::get_requirejs_paths_find_package()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 3
eloc 4
nc 4
nop 2
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2014-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Page\Includes;
9
use
10
	cs\Config,
11
	cs\Event;
12
13
trait RequireJS {
14
	/**
15
	 * @return string[]
16
	 */
17
	protected function get_requirejs_paths () {
18
		$Config = Config::instance();
19
		$paths  = [];
20
		foreach ($Config->components['modules'] as $module_name => $module_data) {
21
			if ($module_data['active'] == Config\Module_Properties::UNINSTALLED) {
22
				continue;
23
			}
24
			$this->get_requirejs_paths_add_aliases(MODULES."/$module_name", $paths);
25
		}
26
		foreach ($Config->components['plugins'] as $plugin_name) {
27
			$this->get_requirejs_paths_add_aliases(PLUGINS."/$plugin_name", $paths);
28
		}
29
		$directories_to_browse = [
30
			DIR.'/bower_components',
31
			DIR.'/node_modules'
32
		];
33
		Event::instance()->fire(
34
			'System/Page/requirejs',
35
			[
36
				'paths'                 => &$paths,
37
				'directories_to_browse' => &$directories_to_browse
38
			]
39
		);
40
		foreach ($directories_to_browse as $dir) {
41
			foreach (get_files_list($dir, false, 'd', true) as $d) {
42
				$this->get_requirejs_paths_find_package($d, $paths);
43
			}
44
		}
45
		return $paths;
46
	}
47
	/**
48
	 * @param string   $dir
49
	 * @param string[] $paths
50
	 */
51
	protected function get_requirejs_paths_add_aliases ($dir, &$paths) {
52
		if (is_dir("$dir/includes/js")) {
53
			$name         = basename($dir);
54
			$paths[$name] = $this->absolute_path_to_relative("$dir/includes/js");
1 ignored issue
show
Bug introduced by
It seems like absolute_path_to_relative() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
55
			foreach ((array)@file_get_json("$dir/meta.json")['provide'] as $p) {
56
				if (strpos($p, '/') !== false) {
57
					$paths[$p] = $paths[$name];
58
				}
59
			}
60
		}
61
	}
62
	/**
63
	 * @param string   $dir
64
	 * @param string[] $paths
65
	 */
66
	protected function get_requirejs_paths_find_package ($dir, &$paths) {
67
		$path = $this->get_requirejs_paths_find_package_bower($dir) ?: $this->get_requirejs_paths_find_package_npm($dir);
68
		if ($path) {
69
			$paths[basename($dir)] = $this->absolute_path_to_relative(substr($path, 0, -3));
1 ignored issue
show
Bug introduced by
It seems like absolute_path_to_relative() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
70
		}
71
	}
72
	/**
73
	 * @param string $dir
74
	 *
75
	 * @return string
76
	 */
77
	protected function get_requirejs_paths_find_package_bower ($dir) {
78
		$bower = @file_get_json("$dir/bower.json");
79
		foreach (@(array)$bower['main'] as $main) {
80
			if (preg_match('/\.js$/', $main)) {
81
				$main = substr($main, 0, -3);
82
				// There is a chance that minified file is present
83
				$main = file_exists_with_extension("$dir/$main", ['min.js', 'js']);
84
				if ($main) {
85
					return $main;
86
				}
87
			}
88
		}
89
		return null;
90
	}
91
	/**
92
	 * @param string $dir
93
	 *
94
	 * @return false|string
95
	 */
96
	protected function get_requirejs_paths_find_package_npm ($dir) {
97
		$package = @file_get_json("$dir/package.json");
98
		// If we have browser-specific declaration - use it
99
		/** @noinspection NestedTernaryOperatorInspection */
100
		$main = @$package['browser'] ?: (@$package['jspm']['main'] ?: @$package['main']);
101
		if (preg_match('/\.js$/', $main)) {
102
			$main = substr($main, 0, -3);
103
		}
104
		if ($main) {
105
			// There is a chance that minified file is present
106
			return file_exists_with_extension("$dir/$main", ['min.js', 'js']) ?: file_exists_with_extension("$dir/dist/$main", ['min.js', 'js']);
107
		}
108
		return false;
109
	}
110
}
111