PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/library/core/class.gdn.php

https://github.com/dkobia/Garden
PHP | 397 lines | 179 code | 53 blank | 165 comment | 19 complexity | bca63513bd57882d88ab1fce160d4972 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 AliasCache = 'Cache';
  14. const AliasConfig = 'Config';
  15. const AliasDatabase = 'Database';
  16. const AliasDatabaseStructure = 'DatabaseStructure';
  17. const AliasDispatcher = 'Dispatcher';
  18. const AliasLocale = 'Locale';
  19. const AliasPermissionModel = 'PermissionModel';
  20. const AliasRequest = 'Request';
  21. const AliasRouter = 'Router';
  22. const AliasSession = 'Session';
  23. const AliasSlice = 'Slice';
  24. const AliasSqlDriver = 'SqlDriver';
  25. const AliasUserModel = 'UserModel';
  26. const AliasPluginManager = 'PluginManager';
  27. const FactoryInstance = 'Instance';
  28. const FactoryPrototype = 'Prototype';
  29. const FactorySingleton = 'Singleton';
  30. const FactoryRealSingleton = 'RealSingleton';
  31. /// PROPERTIES ///
  32. protected static $_Config = NULL;
  33. /** @var Gdn_Factory The factory used to create core objects in the application. */
  34. protected static $_Factory = NULL;
  35. /** @var boolean Whether or not Gdn::FactoryInstall should overwrite existing objects. */
  36. protected static $_FactoryOverwrite = TRUE;
  37. protected static $_Locale = NULL;
  38. protected static $_Request = NULL;
  39. protected static $_PluginManager = NULL;
  40. protected static $_Session = NULL;
  41. /// METHODS ///
  42. /** @return Gdn_Auth */
  43. public static function Authenticator() {
  44. $Result = self::Factory(self::AliasAuthenticator);
  45. return $Result;
  46. }
  47. /**
  48. * Get the cache object
  49. *
  50. * @return Gdn_Cache
  51. */
  52. public static function Cache() {
  53. return self::Factory(self::AliasCache);
  54. }
  55. /**
  56. * Get a configuration setting for the application.
  57. * @param string $Name The name of the configuration setting. Settings in different sections are seperated by a dot ('.')
  58. * @param mixed $Default The result to return if the configuration setting is not found.
  59. * @return mixed The configuration setting.
  60. */
  61. public static function Config($Name = FALSE, $Default = FALSE) {
  62. $Config = self::$_Config;
  63. if($Name === FALSE)
  64. $Result = $Config;
  65. else
  66. $Result = $Config->Get($Name, $Default);
  67. return $Result;
  68. }
  69. /** Gets the global dispatcher object.
  70. *
  71. * @return Gdn_Dispatcher
  72. */
  73. public static function Dispatcher() {
  74. $Result = self::Factory(self::AliasDispatcher);
  75. return $Result;
  76. }
  77. /**
  78. * Get a reference to the default database object.
  79. * @return Gdn_Database
  80. */
  81. public static function Database() {
  82. $Result = self::Factory(self::AliasDatabase);
  83. return $Result;
  84. }
  85. /**
  86. * Get an object from the factory.
  87. * @param string $Alias The alias of the class.
  88. * @param mixed $Args A variable number of arguments to pass to the constructor.
  89. * @see Gdn_Factory::Factory()
  90. */
  91. public static function Factory($Alias = FALSE) {
  92. if ($Alias === FALSE)
  93. return self::$_Factory;
  94. // Get the arguments to pass to the factory.
  95. //$Args = array($Arg1, $Arg2, $Arg3, $Arg4, $Arg5);
  96. $Args = func_get_args();
  97. array_shift($Args);
  98. return self::$_Factory->Factory($Alias, $Args);
  99. }
  100. /**
  101. * Checks whether or not a factory alias exists.
  102. *
  103. * @param string $Alias The alias of the factory to check for.
  104. * @return boolean Whether or not a factory definintion exists.
  105. * @see Gdn_Factory::Exists()
  106. */
  107. public static function FactoryExists($Alias) {
  108. $Result = self::$_Factory->Exists($Alias);
  109. return $Result;
  110. }
  111. /**
  112. * Install a class to the factory.
  113. *
  114. * @param string $Alias An alias for the class that will be used to retreive instances of it.
  115. * @param string $ClassName The actual name of the class.
  116. * @param string $Path The path to the class' file. You can prefix the path with ~ to start at the application root.
  117. * @param string $FactoryType The way objects will be instantiated for the class. One of (Gdn::FactoryInstance, Gdn::FactoryPrototype, Gdn::FactorySingleton).
  118. * @see Gdn_Factory::Install()
  119. */
  120. public static function FactoryInstall($Alias, $ClassName, $Path, $FactoryType = self::FactoryInstance, $Data = NULL) {
  121. // Don't overwrite an existing definition.
  122. if(self::$_FactoryOverwrite === FALSE && self::FactoryExists($Alias))
  123. return;
  124. self::$_Factory->Install($Alias, $ClassName, $Path, $FactoryType, $Data);
  125. // Cache some of the more commonly used factory objects as properties.
  126. switch($Alias) {
  127. case self::AliasConfig:
  128. self::$_Config = self::Factory($Alias);
  129. break;
  130. case self::AliasRequest:
  131. self::$_Request = self::Factory($Alias);
  132. break;
  133. case self::AliasPluginManager:
  134. self::$_PluginManager = self::Factory($Alias);
  135. break;
  136. case self::AliasSession:
  137. self::$_Session = NULL;
  138. break;
  139. }
  140. }
  141. /**
  142. * Installs a dependency to the factory.
  143. *
  144. * @param string $Alias The alias of the class that will have the dependency.
  145. * @param string $PropertyName The name of the property on the class that will have the dependency.
  146. * @param string $SourceAlias The alias of the class that will provide the value of the property when objects are instantiated.
  147. * @see Gdn_Factory::InstalDependency()
  148. */
  149. public static function FactoryInstallDependency($Alias, $PropertyName, $SourceAlias) {
  150. self::$_Factory->InstallDependency($Alias, $PropertyName, $SourceAlias);
  151. }
  152. /**
  153. * Installs a dependency to the factory with the settings from a configuration.
  154. *
  155. * @param mixed $Config The configuration of the factory definition. This argument can be of the following types:
  156. * - <b>string</b>: The configuration will be looked up by calling inline{@link Gdn::Config()}
  157. * - <b>array</b>: The configuration will be set from the array.
  158. * @param string $Alias The class alias to install into the factory. If omitted then it must be in the configuration.
  159. *
  160. * The dependency will be installed from the configuration array depending on the following keys:
  161. * - <b>Alias</b>: Optional if $Alias is passed as an argument.
  162. * - <b>PropertyName</b>: Required.
  163. * - <b>SourceAlias</b>: Required.
  164. * - <b>Override</b> Optional.
  165. * All of these values are passed to the corresponding argument in inline{@link Gdn::FactoryInstallDependency()}.
  166. */
  167. public static function FactoryInstallDependencyFromConfig($Config, $Alias = NULL) {
  168. if(is_string($Config))
  169. $Config = self::Config($Config);
  170. if(is_null($Alias))
  171. $Alias = $Config['Alias'];
  172. $PropertyName = $Config['PropertyName'];
  173. $SourceAlias = $Config['SourceAlias'];
  174. $Override = ArrayValue('Override', $Config, TRUE);
  175. self::FactoryInstallDependency($Alias, $PropertyName, $SourceAlias, $Override);
  176. }
  177. /**
  178. * Installs a class to the factory with the settings from a configuration.
  179. *
  180. * @param mixed $Config The configuration of the factory definition. This argument can be of the following types:
  181. * - <b>string</b>: The configuration will be looked up by calling inline{@link Gdn::Config()}
  182. * - <b>array</b>: The configuration will be set from the array.
  183. * @param string $Alias The class alias to install into the factory. If omitted then it must be in the configuration.
  184. *
  185. * The factory will be installed from the configuration array depending on the following keys:
  186. * - <b>Alias</b>: Optional if $Alias is passed as an argument.
  187. * - <b>FactoryType</b>: Required.
  188. * - <b>Data</b>: Optional.
  189. * - <b>Override</b> Optional.
  190. * - <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}.
  191. * All of these values (except Dependencies) are passed to the corresponding argument in inline{@link Gdn::FactoryInstall()}.
  192. */
  193. public static function FactoryInstallFromConfig($Config, $Alias = NULL) {
  194. if(is_string($Config))
  195. $Config = self::Config($Config);
  196. if(is_null($Alias))
  197. $Alias = $Config['Alias'];
  198. $FactoryType = $Config['FactoryType'];
  199. $Data = ArrayValue('Data', $Config, NULL);
  200. $Override = ArrayValue('Override', $Config, TRUE);
  201. self::FactoryInstall($Alias, $Config['ClassName'], $Config['Path'], $FactoryType, $Data, $Override);
  202. if(array_key_exists('Dependencies', $Config)) {
  203. $Dependencies = $Config['Dependencies'];
  204. foreach($Dependencies as $Index => $DependencyConfig) {
  205. self::FactoryInstallFromConfig($DependencyConfig, $Alias);
  206. }
  207. }
  208. }
  209. public static function FactoryOverwrite($Value = NULL) {
  210. $Result = (self::$_FactoryOverwrite & 1 > 0);
  211. if(!is_null($Value)) {
  212. self::$_FactoryOverwrite = $Value;
  213. }
  214. return $Result;
  215. }
  216. /**
  217. * Uninstall an class from the factory.
  218. *
  219. * @param string $Alias The alias of the class to uninstall.
  220. * @see Gdn_Factory::Uninstall()
  221. */
  222. public static function FactoryUninstall($Alias) {
  223. self::$_Factory->Uninstall($Alias);
  224. }
  225. /**
  226. * Uninstall a dependency from the factory.
  227. *
  228. * @see Gdn_Factory::UninstallDependency()
  229. */
  230. public static function FactoryUninstallDependency($Alias, $PropertyName = NULL) {
  231. self::$_Factory->UninstallDependency($Alias, $PropertyName);
  232. }
  233. /**
  234. * @return Gdn_Locale
  235. */
  236. public static function Locale() {
  237. if(is_null(self::$_Locale))
  238. self::$_Locale = self::Factory(self::AliasLocale);
  239. return self::$_Locale;
  240. }
  241. /**
  242. * Get the permission model for the application.
  243. *
  244. * @return PermissionModel
  245. */
  246. public static function PermissionModel() {
  247. return self::Factory(self::AliasPermissionModel);
  248. }
  249. /**
  250. * Get the plugin manager for the application.
  251. *
  252. * @return Gdn_PluginManager
  253. */
  254. public static function PluginManager() {
  255. return self::$_PluginManager; //self::Factory(self::AliasPluginManager);
  256. }
  257. /**
  258. * Get or set the current request object.
  259. * @param Gdn_Rewuest $NewRequest The new request or null to just get the request.
  260. * @return Gdn_Request
  261. */
  262. public static function Request($NewRequest = NULL) {
  263. $Request = self::$_Request; //self::Factory(self::AliasRequest);
  264. if (!is_null($NewRequest)) {
  265. if(is_string($NewRequest))
  266. $Request->WithURI($NewRequest);
  267. elseif(is_object($NewRequest))
  268. $Request->FromImport($NewRequest);
  269. }
  270. return $Request;
  271. }
  272. /**
  273. * Get the router object
  274. *
  275. * @return Gdn_Router
  276. */
  277. public static function Router() {
  278. return self::Factory(self::AliasRouter);
  279. }
  280. /**
  281. * Get the session object.
  282. *
  283. * @return Gdn_Session
  284. */
  285. public static function Session() {
  286. if(is_null(self::$_Session))
  287. self::$_Session = self::Factory(self::AliasSession);
  288. return self::$_Session;
  289. }
  290. public static function Slice($Slice) {
  291. $Result = self::Factory(self::AliasSlice);
  292. return $Result->Execute($Slice);
  293. }
  294. /**
  295. * Get a reference to the default SQL driver object.
  296. *
  297. * @return Gdn_SQLDriver
  298. * @see Gdn_Database::SQL()
  299. */
  300. public static function SQL() {
  301. $Database = self::Database();
  302. $Result = $Database->SQL();
  303. return $Result;
  304. }
  305. /**
  306. * Get a reference to the default database structure object.
  307. * @return Gdn_DatabaseStructure
  308. */
  309. public static function Structure() {
  310. $Database = self::Database();
  311. $Result = $Database->Structure();
  312. return $Result;
  313. }
  314. /**
  315. * Translates a code into the selected locale's definition.
  316. *
  317. * @param string $Code The code related to the language-specific definition.
  318. * @param string $Default The default value to be displayed if the translation code is not found.
  319. * @return string The translated string or $Code if there is no value in $Default.
  320. */
  321. public static function Translate($Code, $Default = '') {
  322. if ($Default == '')
  323. $Default = $Code;
  324. return Gdn::Locale()->Translate($Code, $Default);
  325. }
  326. /**
  327. * Get a reference to the user model.
  328. *
  329. * @return UserModel
  330. */
  331. public static function UserModel() {
  332. return self::Factory(self::AliasUserModel);
  333. }
  334. /**
  335. * Set the object used as the factory for the api.
  336. *
  337. * @param Gdn_Factory $Factory The object used as the factory.
  338. * @param boolean $Override whether to override the property if it is already set.
  339. */
  340. public static function SetFactory($Factory, $Override = TRUE) {
  341. if ($Override || is_null(self::$_Factory))
  342. self::$_Factory = $Factory;
  343. }
  344. }