Issues (1474)

framework/TApplicationComponent.php (1 issue)

Severity
1
<?php
2
3
/**
4
 * TApplicationComponent class
5
 *
6
 * @author Qiang Xue <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado;
12
13
use Prado\TApplicationMode;
14
15
/**
16
 * TApplicationComponent class
17
 *
18
 * TApplicationComponent is the base class for all components that are
19
 * application-related, such as controls, modules, services, etc.
20
 *
21
 * TApplicationComponent mainly defines a few properties that are shortcuts
22
 * to some commonly used methods. The {@see getApplication Application}
23
 * property gives the application instance that this component belongs to;
24
 * {@see getService Service} gives the current running service;
25
 * {@see getRequest Request}, {@see getResponse Response} and {@see getSession Session}
26
 * return the request and response modules, respectively;
27
 * And {@see getUser User} gives the current user instance.
28
 *
29
 * Besides, TApplicationComponent defines two shortcut methods for
30
 * publishing private files: {@see publishAsset} and {@see publishFilePath}.
31
 *
32
 * @author Qiang Xue <[email protected]>
33
 * @since 3.0
34
 */
35
class TApplicationComponent extends \Prado\TComponent
36
{
37
	public const FX_CACHE_FILE = 'fxevent.cache';
38
	/**
39 91
	 * TApplicationComponents auto listen to global events.
40
	 *
41 91
	 * @return bool returns whether or not to listen.
42
	 */
43
	public function getAutoGlobalListen()
44
	{
45
		return true;
46
	}
47
48
	/**
49
	 * This caches the 'fx' events for PRADO classes in the application cache
50
	 * @param object $class The object to get the 'fx' events.
51
	 * @return string[] fx events from a specific class
52
	 */
53
	protected function getClassFxEvents($class)
54
	{
55 12
		static $_classfx = [];
56
		static $_classfxSize = 0;
57 12
		static $_loaded = false;
58
59
		$app = $this->getApplication();
60
		$cacheFile = $mode = null;
61
		if ($app) {
0 ignored issues
show
$app is of type Prado\TApplication, thus it always evaluated to true.
Loading history...
62
			$cacheFile = $app->getRuntimePath() . DIRECTORY_SEPARATOR . self::FX_CACHE_FILE;
63
			if ((($mode = $app->getMode()) === TApplicationMode::Normal || $mode === TApplicationMode::Performance) && !$_loaded) {
64
				$_loaded = true;
65
				if (($content = @file_get_contents($cacheFile)) !== false) {
66
					$_classfx = @unserialize($content) ?? [];
67
					$_classfxSize = count($_classfx);
68
				}
69
			}
70
		}
71
		$className = $class::class;
72
		if (array_key_exists($className, $_classfx)) {
73
			return $_classfx[$className];
74
		}
75
		$fx = parent::getClassFxEvents($class);
76
		$_classfx[$className] = $fx;
77
		if ($cacheFile) {
78
			if ($mode === TApplicationMode::Performance) {
79
				file_put_contents($cacheFile, serialize($_classfx), LOCK_EX);
80
			} elseif ($mode === TApplicationMode::Normal) {
81
				static $_flipClassMap = null;
82
83
				if ($_flipClassMap === null) {
84
					$_flipClassMap = array_flip(Prado::$classMap);
85
				}
86
				$classData = array_intersect_key($_classfx, $_flipClassMap);
87
				if (($c = count($classData)) > $_classfxSize) {
88
					$_classfxSize = $c;
89
					file_put_contents($cacheFile, serialize($_classfx), LOCK_EX);
90
				}
91
			}
92
		}
93
		return $fx;
94
	}
95
96
	/**
97
	 * @return \Prado\TApplication current application instance
98
	 */
99
	public function getApplication()
100
	{
101
		return Prado::getApplication();
102
	}
103
104
	/**
105
	 * @return \Prado\TService the current service
106
	 */
107
	public function getService()
108
	{
109
		return Prado::getApplication()->getService();
110
	}
111
112
	/**
113
	 * @return \Prado\Web\THttpRequest the current user request
114
	 */
115
	public function getRequest()
116
	{
117
		return Prado::getApplication()->getRequest();
118
	}
119
120
	/**
121
	 * @return \Prado\Web\THttpResponse the response
122
	 */
123
	public function getResponse()
124
	{
125
		return Prado::getApplication()->getResponse();
126
	}
127
128
	/**
129
	 * @return \Prado\Web\THttpSession user session
130
	 */
131
	public function getSession()
132
	{
133
		return Prado::getApplication()->getSession();
134
	}
135
136
	/**
137
	 * @return \Prado\Security\IUser information about the current user
138
	 */
139
	public function getUser()
140
	{
141
		return Prado::getApplication()->getUser();
142
	}
143
144
	/**
145
	 * Publishes a private asset and gets its URL.
146
	 * This method will publish a private asset (file or directory)
147
	 * and gets the URL to the asset. Note, if the asset refers to
148
	 * a directory, all contents under that directory will be published.
149
	 * Also note, it is recommended that you supply a class name as the second
150
	 * parameter to the method (e.g. publishAsset($assetPath,__CLASS__) ).
151
	 * By doing so, you avoid the issue that child classes may not work properly
152
	 * because the asset path will be relative to the directory containing the child class file.
153
	 *
154
	 * @param string $assetPath path of the asset that is relative to the directory containing the specified class file.
155
	 * @param string $className name of the class whose containing directory will be prepend to the asset path. If null, it means $this::class.
156
	 * @return string URL to the asset path.
157
	 */
158
	public function publishAsset($assetPath, $className = null)
159
	{
160
		if ($className === null) {
161
			$className = $this::class;
162
		}
163
		$class = new \ReflectionClass($className);
164
		$fullPath = dirname($class->getFileName()) . DIRECTORY_SEPARATOR . $assetPath;
165
		return $this->publishFilePath($fullPath);
166
	}
167
168
	/**
169
	 * Publishes a file or directory and returns its URL.
170
	 * @param string $fullPath absolute path of the file or directory to be published
171
	 * @param mixed $checkTimestamp
172
	 * @return string URL to the published file or directory
173
	 */
174
	public function publishFilePath($fullPath, $checkTimestamp = false)
175
	{
176
		return Prado::getApplication()->getAssetManager()->publishFilePath($fullPath, $checkTimestamp);
177
	}
178
}
179