PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/core/class.gdn.php

https://gitlab.com/sheldonels/Garden
PHP | 352 lines | 155 code | 48 blank | 149 comment | 18 complexity | 44f00b2ad009fe37fde11e70864d9857 MD5 | raw file
  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Vanilla Forums Inc.
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
  8. Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
  9. */
  10. class Gdn {
  11. /// CONSTANTS ///
  12. const AliasAuthenticator = 'Authenticator';
  13. const AliasConfig = 'Config';
  14. const AliasDatabase = 'Database';
  15. const AliasDatabaseStructure = 'DatabaseStructure';
  16. const AliasDispatcher = 'Dispatcher';
  17. const AliasLocale = 'Locale';
  18. const AliasPermissionModel = 'PermissionModel';
  19. const AliasRequest = 'Request';
  20. const AliasRouter = 'Router';
  21. const AliasSession = 'Session';
  22. const AliasSqlDriver = 'SqlDriver';
  23. const AliasUserModel = 'UserModel';
  24. const AliasPluginManager = 'PluginManager';
  25. const FactoryInstance = 'Instance';
  26. const FactoryPrototype = 'Prototype';
  27. const FactorySingleton = 'Singleton';
  28. const FactoryRealSingleton = 'RealSingleton';
  29. /// PROPERTIES ///
  30. protected static $_Config = NULL;
  31. /** @var Gdn_Factory The factory used to create core objects in the application. */
  32. protected static $_Factory = NULL;
  33. /** @var boolean Whether or not Gdn::FactoryInstall should overwrite existing objects. */
  34. protected static $_FactoryOverwrite = TRUE;
  35. protected static $_Locale = NULL;
  36. protected static $_Session = NULL;
  37. /// METHODS ///
  38. public static function Authenticator() {
  39. $Result = self::Factory(self::AliasAuthenticator);
  40. return $Result;
  41. }
  42. /**
  43. * Get a configuration setting for the application.
  44. * @param string $Name The name of the configuration setting. Settings in different sections are seperated by a dot ('.')
  45. * @param mixed $Default The result to return if the configuration setting is not found.
  46. * @return mixed The configuration setting.
  47. */
  48. public static function Config($Name = FALSE, $Default = FALSE) {
  49. $Config = self::$_Config;
  50. if($Name === FALSE)
  51. $Result = $Config;
  52. else
  53. $Result = $Config->Get($Name, $Default);
  54. return $Result;
  55. }
  56. public static function Dispatcher() {
  57. $Result = self::Factory(self::AliasDispatcher);
  58. return $Result;
  59. }
  60. /**
  61. * Get a reference to the default database object.
  62. * @return Gdn_Database
  63. */
  64. public static function Database() {
  65. $Result = self::Factory(self::AliasDatabase);
  66. return $Result;
  67. }
  68. /**
  69. * Get an object from the factory.
  70. * @param string $Alias The alias of the class.
  71. * @param mixed $Args A variable number of arguments to pass to the constructor.
  72. * @see Gdn_Factory::Factory()
  73. */
  74. public static function Factory($Alias = FALSE) {
  75. if ($Alias === FALSE)
  76. return self::$_Factory;
  77. // Get the arguments to pass to the factory.
  78. $Args = func_get_args();
  79. array_shift($Args);
  80. return self::$_Factory->Factory($Alias, $Args);
  81. }
  82. /**
  83. * Checks whether or not a factory alias exists.
  84. *
  85. * @param string $Alias The alias of the factory to check for.
  86. * @return boolean Whether or not a factory definintion exists.
  87. * @see Gdn_Factory::Exists()
  88. */
  89. public static function FactoryExists($Alias) {
  90. $Result = self::$_Factory->Exists($Alias);
  91. return $Result;
  92. }
  93. /**
  94. * Install a class to the factory.
  95. *
  96. * @param string $Alias An alias for the class that will be used to retreive instances of it.
  97. * @param string $ClassName The actual name of the class.
  98. * @param string $Path The path to the class' file. You can prefix the path with ~ to start at the application root.
  99. * @param string $FactoryType The way objects will be instantiated for the class. One of (Gdn::FactoryInstance, Gdn::FactoryPrototype, Gdn::FactorySingleton).
  100. * @see Gdn_Factory::Install()
  101. */
  102. public static function FactoryInstall($Alias, $ClassName, $Path, $FactoryType = self::FactoryInstance, $Data = NULL) {
  103. // Don't overwrite an existing definition.
  104. if(self::$_FactoryOverwrite === FALSE && self::FactoryExists($Alias))
  105. return;
  106. self::$_Factory->Install($Alias, $ClassName, $Path, $FactoryType, $Data);
  107. // Cache some of the more commonly used factory objects as properties.
  108. switch($Alias) {
  109. case self::AliasConfig:
  110. self::$_Config = self::Factory($Alias);
  111. break;
  112. case self::AliasSession:
  113. self::$_Session = NULL;
  114. break;
  115. }
  116. }
  117. /**
  118. * Installs a dependency to the factory.
  119. *
  120. * @param string $Alias The alias of the class that will have the dependency.
  121. * @param string $PropertyName The name of the property on the class that will have the dependency.
  122. * @param string $SourceAlias The alias of the class that will provide the value of the property when objects are instantiated.
  123. * @see Gdn_Factory::InstalDependency()
  124. */
  125. public static function FactoryInstallDependency($Alias, $PropertyName, $SourceAlias) {
  126. self::$_Factory->InstallDependency($Alias, $PropertyName, $SourceAlias);
  127. }
  128. /**
  129. * Installs a dependency to the factory with the settings from a configuration.
  130. *
  131. * @param mixed $Config The configuration of the factory definition. This argument can be of the following types:
  132. * - <b>string</b>: The configuration will be looked up by calling inline{@link Gdn::Config()}
  133. * - <b>array</b>: The configuration will be set from the array.
  134. * @param string $Alias The class alias to install into the factory. If omitted then it must be in the configuration.
  135. *
  136. * The dependency will be installed from the configuration array depending on the following keys:
  137. * - <b>Alias</b>: Optional if $Alias is passed as an argument.
  138. * - <b>PropertyName</b>: Required.
  139. * - <b>SourceAlias</b>: Required.
  140. * - <b>Override</b> Optional.
  141. * All of these values are passed to the corresponding argument in inline{@link Gdn::FactoryInstallDependency()}.
  142. */
  143. public static function FactoryInstallDependencyFromConfig($Config, $Alias = NULL) {
  144. if(is_string($Config))
  145. $Config = self::Config($Config);
  146. if(is_null($Alias))
  147. $Alias = $Config['Alias'];
  148. $PropertyName = $Config['PropertyName'];
  149. $SourceAlias = $Config['SourceAlias'];
  150. $Override = ArrayValue('Override', $Config, TRUE);
  151. self::FactoryInstallDependency($Alias, $PropertyName, $SourceAlias, $Override);
  152. }
  153. /**
  154. * Installs a class to the factory with the settings from a configuration.
  155. *
  156. * @param mixed $Config The configuration of the factory definition. This argument can be of the following types:
  157. * - <b>string</b>: The configuration will be looked up by calling inline{@link Gdn::Config()}
  158. * - <b>array</b>: The configuration will be set from the array.
  159. * @param string $Alias The class alias to install into the factory. If omitted then it must be in the configuration.
  160. *
  161. * The factory will be installed from the configuration array depending on the following keys:
  162. * - <b>Alias</b>: Optional if $Alias is passed as an argument.
  163. * - <b>FactoryType</b>: Required.
  164. * - <b>Data</b>: Optional.
  165. * - <b>Override</b> Optional.
  166. * - <b>Dependencies</b> Optional. Dependencies for the class can be defined as a subarray. Each item in the subarray will be passed to inline{@link Gdn::FactoryInstallDependencyFromConfig}.
  167. * All of these values (except Dependencies) are passed to the corresponding argument in inline{@link Gdn::FactoryInstall()}.
  168. */
  169. public static function FactoryInstallFromConfig($Config, $Alias = NULL) {
  170. if(is_string($Config))
  171. $Config = self::Config($Config);
  172. if(is_null($Alias))
  173. $Alias = $Config['Alias'];
  174. $FactoryType = $Config['FactoryType'];
  175. $Data = ArrayValue('Data', $Config, NULL);
  176. $Override = ArrayValue('Override', $Config, TRUE);
  177. self::FactoryInstall($Alias, $Config['ClassName'], $Config['Path'], $FactoryType, $Data, $Override);
  178. if(array_key_exists('Dependencies', $Config)) {
  179. $Dependencies = $Config['Dependencies'];
  180. foreach($Dependencies as $Index => $DependencyConfig) {
  181. self::FactoryInstallFromConfig($DependencyConfig, $Alias);
  182. }
  183. }
  184. }
  185. public static function FactoryOverwrite($Value = NULL) {
  186. $Result = (self::$_FactoryOverwrite & 1 > 0);
  187. if(!is_null($Value)) {
  188. self::$_FactoryOverwrite = $Value;
  189. }
  190. return $Result;
  191. }
  192. /**
  193. * Uninstall an class from the factory.
  194. *
  195. * @param string $Alias The alias of the class to uninstall.
  196. * @see Gdn_Factory::Uninstall()
  197. */
  198. public static function FactoryUninstall($Alias) {
  199. self::$_Factory->Uninstall($Alias);
  200. }
  201. /**
  202. * Uninstall a dependency from the factory.
  203. *
  204. * @see Gdn_Factory::UninstallDependency()
  205. */
  206. public static function FactoryUninstallDependency($Alias, $PropertyName = NULL) {
  207. self::$_Factory->UninstallDependency($Alias, $PropertyName);
  208. }
  209. /**
  210. * @return Gdn_Locale
  211. */
  212. public static function Locale() {
  213. if(is_null(self::$_Locale))
  214. self::$_Locale = self::Factory(self::AliasLocale);
  215. return self::$_Locale;
  216. }
  217. /**
  218. * Get the permission model for the application.
  219. *
  220. * @return PermissionModel
  221. */
  222. public static function PermissionModel() {
  223. return self::Factory(self::AliasPermissionModel);
  224. }
  225. /**
  226. * Get the current request object
  227. *
  228. * @return Gdn_Request
  229. */
  230. public static function Request($NewRequest=NULL) {
  231. $Request = self::Factory(self::AliasRequest);
  232. if (!is_null($NewRequest))
  233. $Request->Import($NewRequest);
  234. return $Request;
  235. }
  236. /**
  237. * Get the router object
  238. *
  239. * @return Gdn_Router
  240. */
  241. public static function Router() {
  242. return self::Factory(self::AliasRouter);
  243. }
  244. /**
  245. * Get the session object.
  246. *
  247. * @return Gdn_Session
  248. */
  249. public static function Session() {
  250. if(is_null(self::$_Session))
  251. self::$_Session = self::Factory(self::AliasSession);
  252. return self::$_Session;
  253. }
  254. /**
  255. * Get a reference to the default SQL driver object.
  256. *
  257. * @return Gdn_SQLDriver
  258. * @see Gdn_Database::SQL()
  259. */
  260. public static function SQL() {
  261. $Database = self::Database();
  262. $Result = $Database->SQL();
  263. return $Result;
  264. }
  265. /**
  266. * Get a reference to the default database structure object.
  267. * @return Gdn_DatabaseStructure
  268. */
  269. public static function Structure() {
  270. $Database = self::Database();
  271. $Result = $Database->Structure();
  272. return $Result;
  273. }
  274. /**
  275. * Translates a code into the selected locale's definition.
  276. *
  277. * @param string $Code The code related to the language-specific definition.
  278. * @param string $Default The default value to be displayed if the translation code is not found.
  279. * @return string The translated string or $Code if there is no value in $Default.
  280. */
  281. public static function Translate($Code, $Default = '') {
  282. if ($Default == '')
  283. $Default = $Code;
  284. return Gdn::Locale()->Translate($Code, $Default);
  285. }
  286. /**
  287. * Get a reference to the user model.
  288. *
  289. * @return UserModel
  290. */
  291. public static function UserModel() {
  292. return self::Factory(self::AliasUserModel);
  293. }
  294. /**
  295. * Set the object used as the factory for the api.
  296. *
  297. * @param Gdn_Factory $Factory The object used as the factory.
  298. * @param boolean $Override whether to override the property if it is already set.
  299. */
  300. public static function SetFactory($Factory, $Override = TRUE) {
  301. if ($Override || is_null(self::$_Factory))
  302. self::$_Factory = $Factory;
  303. }
  304. }