Passed
Push — master ( 83ba19...3d13ab )
by Fabio
04:49
created

TApplicationComponent   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 20%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 93
ccs 4
cts 20
cp 0.2
rs 10
c 2
b 0
f 0
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getService() 0 3 1
A publishAsset() 0 8 2
A publishFilePath() 0 3 1
A getResponse() 0 3 1
A getUser() 0 3 1
A getSession() 0 3 1
A getApplication() 0 3 1
A getRequest() 0 3 1
A getAutoGlobalListen() 0 3 1
1
<?php
2
/**
3
 * TApplicationComponent class
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado
9
 */
10
11
namespace Prado;
12
13
/**
14
 * TApplicationComponent class
15
 *
16
 * TApplicationComponent is the base class for all components that are
17
 * application-related, such as controls, modules, services, etc.
18
 *
19
 * TApplicationComponent mainly defines a few properties that are shortcuts
20
 * to some commonly used methods. The {@link getApplication Application}
21
 * property gives the application instance that this component belongs to;
22
 * {@link getService Service} gives the current running service;
23
 * {@link getRequest Request}, {@link getResponse Response} and {@link getSession Session}
24
 * return the request and response modules, respectively;
25
 * And {@link getUser User} gives the current user instance.
26
 *
27
 * Besides, TApplicationComponent defines two shortcut methods for
28
 * publishing private files: {@link publishAsset} and {@link publishFilePath}.
29
 *
30
 * @author Qiang Xue <[email protected]>
31
 * @package Prado
32
 * @since 3.0
33
 */
34
class TApplicationComponent extends \Prado\TComponent
35
{
36
	/**
37
	 * TApplicationComponents auto listen to global events.
38
	 *
39 91
	 * @return bool returns whether or not to listen.
40
	 */
41 91
	public function getAutoGlobalListen()
42
	{
43
		return true;
44
	}
45
	
46
	/**
47
	 * @return \Prado\TApplication current application instance
48
	 */
49
	public function getApplication()
50
	{
51
		return Prado::getApplication();
52
	}
53
54
	/**
55 12
	 * @return \Prado\IService the current service
56
	 */
57 12
	public function getService()
58
	{
59
		return Prado::getApplication()->getService();
60
	}
61
62
	/**
63
	 * @return \Prado\Web\THttpRequest the current user request
64
	 */
65
	public function getRequest()
66
	{
67
		return Prado::getApplication()->getRequest();
68
	}
69
70
	/**
71
	 * @return \Prado\Web\THttpResponse the response
72
	 */
73
	public function getResponse()
74
	{
75
		return Prado::getApplication()->getResponse();
76
	}
77
78
	/**
79
	 * @return \Prado\Web\THttpSession user session
80
	 */
81
	public function getSession()
82
	{
83
		return Prado::getApplication()->getSession();
84
	}
85
86
	/**
87
	 * @return \Prado\Security\IUser information about the current user
88
	 */
89
	public function getUser()
90
	{
91
		return Prado::getApplication()->getUser();
92
	}
93
94
	/**
95
	 * Publishes a private asset and gets its URL.
96
	 * This method will publish a private asset (file or directory)
97
	 * and gets the URL to the asset. Note, if the asset refers to
98
	 * a directory, all contents under that directory will be published.
99
	 * Also note, it is recommended that you supply a class name as the second
100
	 * parameter to the method (e.g. publishAsset($assetPath,__CLASS__) ).
101
	 * By doing so, you avoid the issue that child classes may not work properly
102
	 * because the asset path will be relative to the directory containing the child class file.
103
	 *
104
	 * @param string $assetPath path of the asset that is relative to the directory containing the specified class file.
105
	 * @param string $className name of the class whose containing directory will be prepend to the asset path. If null, it means get_class($this).
106
	 * @return string URL to the asset path.
107
	 */
108
	public function publishAsset($assetPath, $className = null)
109
	{
110
		if ($className === null) {
111
			$className = get_class($this);
112
		}
113
		$class = new \ReflectionClass($className);
114
		$fullPath = dirname($class->getFileName()) . DIRECTORY_SEPARATOR . $assetPath;
115
		return $this->publishFilePath($fullPath);
116
	}
117
118
	/**
119
	 * Publishes a file or directory and returns its URL.
120
	 * @param string $fullPath absolute path of the file or directory to be published
121
	 * @param mixed $checkTimestamp
122
	 * @return string URL to the published file or directory
123
	 */
124
	public function publishFilePath($fullPath, $checkTimestamp = false)
125
	{
126
		return Prado::getApplication()->getAssetManager()->publishFilePath($fullPath, $checkTimestamp);
127
	}
128
}
129