3 * TApplicationComponent class
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
13 * TApplicationComponent class
15 * TApplicationComponent is the base class for all components that are
16 * application-related, such as controls, modules, services, etc.
18 * TApplicationComponent mainly defines a few properties that are shortcuts
19 * to some commonly used methods. The {@link getApplication Application}
20 * property gives the application instance that this component belongs to;
21 * {@link getService Service} gives the current running service;
22 * {@link getRequest Request}, {@link getResponse Response} and {@link getSession Session}
23 * return the request and response modules, respectively;
24 * And {@link getUser User} gives the current user instance.
26 * Besides, TApplicationComponent defines two shortcut methods for
27 * publishing private files: {@link publishAsset} and {@link publishFilePath}.
29 * @author Qiang Xue <qiang.xue@gmail.com>
33 class TApplicationComponent extends TComponent
36 * @return TApplication current application instance
38 public function getApplication()
40 return Prado::getApplication();
44 * @return IService the current service
46 public function getService()
48 return Prado::getApplication()->getService();
52 * @return THttpRequest the current user request
54 public function getRequest()
56 return Prado::getApplication()->getRequest();
60 * @return THttpResponse the response
62 public function getResponse()
64 return Prado::getApplication()->getResponse();
68 * @return THttpSession user session
70 public function getSession()
72 return Prado::getApplication()->getSession();
76 * @return IUser information about the current user
78 public function getUser()
80 return Prado::getApplication()->getUser();
84 * Publishes a private asset and gets its URL.
85 * This method will publish a private asset (file or directory)
86 * and gets the URL to the asset. Note, if the asset refers to
87 * a directory, all contents under that directory will be published.
88 * Also note, it is recommended that you supply a class name as the second
89 * parameter to the method (e.g. publishAsset($assetPath,__CLASS__) ).
90 * By doing so, you avoid the issue that child classes may not work properly
91 * because the asset path will be relative to the directory containing the child class file.
93 * @param string path of the asset that is relative to the directory containing the specified class file.
94 * @param string name of the class whose containing directory will be prepend to the asset path. If null, it means get_class($this).
95 * @return string URL to the asset path.
97 public function publishAsset($assetPath,$className=null)
100 $className=get_class($this);
101 $class=new ReflectionClass($className);
102 $fullPath=dirname($class->getFileName()).DIRECTORY_SEPARATOR.$assetPath;
103 return $this->publishFilePath($fullPath);
107 * Publishes a file or directory and returns its URL.
108 * @param string absolute path of the file or directory to be published
109 * @return string URL to the published file or directory
111 public function publishFilePath($fullPath, $checkTimestamp=false)
113 return Prado::getApplication()->getAssetManager()->publishFilePath($fullPath, $checkTimestamp);