Passed
Push — master ( 370e51...582fe4 )
by Georgi
03:26
created

HasModule::relativePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Epesi\Core\System\Integration\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): self::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\Integration\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
	 * Base name of the module
60
	 *
61
	 * @return string
62
	 */
63
	final public static function name()
64
	{
65
		$names = array_slice(explode('\\', static::class), -2, -1);
66
		
67
		return $names? reset($names): '';
68
	}
69
	
70
	/**
71
	 * Path to the module directory.
72
	 *
73
	 * @return string path to the module directory
74
	 */
75
	final public static function path() {
76
		$reflection = new \ReflectionClass(static::class);
77
		
78
		return pathinfo($reflection->getFileName(), PATHINFO_DIRNAME);
79
	}
80
	
81
	/**
82
	 * Path to the module directory from installation base directory.
83
	 *
84
	 * @return string path to the module directory
85
	 */
86
	final public static function relativePath() {
87
		return str_ireplace(base_path() . DIRECTORY_SEPARATOR, '', static::path());
88
	}
89
}
90