Test Failed
Push — master ( 8f2167...5d2217 )
by Georgi
08:27
created

HasModule   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A view() 0 7 4
A path() 0 4 1
A parentNamespace() 0 2 1
A isSubModuleOf() 0 2 1
A namespace() 0 3 1
A alias() 0 7 3
A relativePath() 0 2 1
A module() 0 3 1
A name() 0 5 2
1
<?php
2
3
namespace Epesi\Core\System\Modules\Concerns;
4
5
use Illuminate\Support\Str;
6
7
trait HasModule
8
{
9
	/**
10
	 * Default module view class
11
	 * 
12
	 * @param string $alias
13
	 * @return string
14
	 */
15
	final public static function view($alias = null)
16
	{
17
		$class = self::namespace() . '\\' . ($alias? Str::studly($alias): (static::name() . 'View'));
18
		
19
		if ($alias) return $class;
20
		
21
		return static::$view?: $class;
22
	}
23
	
24
	/**
25
	 * Module alias
26
	 * 
27
	 * @return string
28
	 */
29
	final public static function alias()
30
	{
31
		if (is_a(static::class, \Epesi\Core\System\Modules\ModuleCore::class, true) && empty(static::$alias)) {
32
			throw new \Exception('Undefined alias in module core class ' . static::class);
33
		}
34
		
35
		return static::$alias?? static::module()::alias();
36
	}
37
	
38
	/**
39
	 * Module core class name
40
	 * 
41
	 * @return string
42
	 */
43
	public static function module()
44
	{
45
		return static::namespace() . '\\' . static::name() . 'Core';
46
	}
47
	
48
	/**
49
	 * Namespace of the module
50
	 *
51
	 * @return string
52
	 */
53
	final public static function namespace()
54
	{
55
		return join('\\', array_slice(explode('\\', static::class), 0, -1));
56
	}
57
	
58
	
59
	/**
60
	 * Base name of the module
61
	 *
62
	 * @return string
63
	 */
64
	final public static function name()
65
	{
66
		$names = array_slice(explode('\\', static::class), -2, -1);
67
		
68
		return $names? reset($names): '';
69
	}
70
	
71
	/**
72
	 * Path to the module directory.
73
	 *
74
	 * @return string path to the module directory
75
	 */
76
	final public static function path() {
77
		$reflection = new \ReflectionClass(static::class);
78
		
79
		return pathinfo($reflection->getFileName(), PATHINFO_DIRNAME);
80
	}
81
	
82
	/**
83
	 * Path to the module directory from installation base directory.
84
	 *
85
	 * @return string path to the module directory
86
	 */
87
	final public static function relativePath() {
88
		return str_ireplace(base_path() . DIRECTORY_SEPARATOR, '', static::path());
89
	}
90
	
91
	final public static function isSubModuleOf($moduleClass) {
92
		return static::parentNamespace() == $moduleClass::namespace();
93
	}
94
	
95
	final public static function parentNamespace() {
96
		return implode('\\', array_slice(explode('\\', static::namespace()), 0, -1));
97
	}
98
}
99