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

/library/deprecated/class.applicationmanager.php

http://github.com/vanillaforums/Garden
PHP | 351 lines | 181 code | 49 blank | 121 comment | 22 complexity | c4f787e89e6f49a18a693935b1389f7d MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Application Manager
  4. *
  5. * @author Mark O'Sullivan <mark@vanillaforums.com>
  6. * @author Tim Gunter <tim@vanillaforums.com>
  7. * @copyright 2009-2019 Vanilla Forums Inc.
  8. * @license GPL-2.0-only
  9. * @package Core
  10. * @since 2.0
  11. */
  12. use Vanilla\Addon;
  13. use Vanilla\AddonManager;
  14. /**
  15. * Manages available applications, enabling and disabling them.
  16. *
  17. * @deprecated 3.0 Use Vanilla\AddonManager.
  18. */
  19. class Gdn_ApplicationManager {
  20. /** @var array Available applications. Never access this directly, instead use $this->availableApplications(); */
  21. private $availableApplications = null;
  22. /** @var array Enabled applications. Never access this directly, instead use $this->enabledApplications(); */
  23. private $enabledApplications = null;
  24. /** @var array The valid paths to search for applications. */
  25. public $Paths = [PATH_APPLICATIONS];
  26. /**
  27. * @var AddonManager
  28. */
  29. private $addonManager;
  30. /**
  31. *
  32. */
  33. public function __construct(AddonManager $addonManager = null) {
  34. $this->addonManager = $addonManager;
  35. }
  36. /**
  37. * Get a list of the available applications.
  38. *
  39. * Looks through the root Garden directory for valid applications and
  40. * returns them as an associative array of "Application Name" =>
  41. * "Application Info Array". It also adds a "Folder" definition to the
  42. * Application Info Array for each application.
  43. */
  44. public function availableApplications() {
  45. if (!is_array($this->availableApplications)) {
  46. $applications = [];
  47. $addons = $this->addonManager->lookupAllByType(Addon::TYPE_ADDON);
  48. foreach ($addons as $addon) {
  49. /* @var Addon $addon */
  50. if ($addon->getInfoValue('oldType') !== 'application') {
  51. continue;
  52. }
  53. $info = $this->calcOldInfoArray($addon);
  54. $applications[$info['Index']] = $info;
  55. }
  56. $this->availableApplications = $applications;
  57. }
  58. return $this->availableApplications;
  59. }
  60. /**
  61. * Gets an array of all of the enabled applications.
  62. *
  63. * @return array
  64. */
  65. public function enabledApplications() {
  66. if (!is_array($this->enabledApplications)) {
  67. $applications = [];
  68. $addons = $this->addonManager->getEnabled();
  69. foreach ($addons as $addon) {
  70. /* @var Addon $addon */
  71. if ($addon->getInfoValue('oldType') !== 'application') {
  72. continue;
  73. }
  74. $info = $this->calcOldInfoArray($addon);
  75. $applications[$info['Index']] = $info;
  76. }
  77. $this->enabledApplications = $applications;
  78. }
  79. return $this->enabledApplications;
  80. }
  81. /**
  82. * Calculate old application's info.
  83. *
  84. * @param Addon $addon
  85. * @return array the old information.
  86. */
  87. private function calcOldInfoArray(Addon $addon) {
  88. $info = Gdn_pluginManager::calcOldInfoArray($addon);
  89. $directories = explode('/', $addon->getSubdir());
  90. $info['Folder'] = $directories[count($directories) - 1];
  91. return $info;
  92. }
  93. /**
  94. * Check to see if an application is enabled.
  95. *
  96. * @param string $applicationName The name of the application to check.
  97. * @return bool Returns true if the application is enabled, otherwise false.
  98. */
  99. public function checkApplication($applicationName) {
  100. if (array_key_exists($applicationName, $this->enabledApplications())) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. /**
  106. * Get the information about an application.
  107. *
  108. * @param string $applicationName The name of the application to lookup.
  109. * @param string $key The key of a field in the application info to return.
  110. * @return bool|mixed Returns the application's info, a specific value, or false if the application cannot be found.
  111. */
  112. public function getApplicationInfo($applicationName, $key = null) {
  113. $applicationInfo = val($applicationName, $this->availableApplications(), null);
  114. if (is_null($applicationInfo)) {
  115. return false;
  116. }
  117. if (!is_null($key)) {
  118. return getValueR($key, $applicationInfo, false);
  119. }
  120. return $applicationInfo;
  121. }
  122. /**
  123. * Get a list of applications that are not marked as invisible.
  124. *
  125. * @return array Returns an array of application info arrays.
  126. */
  127. public function availableVisibleApplications() {
  128. $availableApplications = $this->availableApplications();
  129. foreach ($availableApplications as $applicationName => $info) {
  130. if (!val('AllowEnable', $info, true) || !val('AllowDisable', $info, true)) {
  131. unset($availableApplications[$applicationName]);
  132. }
  133. }
  134. return $availableApplications;
  135. }
  136. /**
  137. * Get a list of applications that are enabled and not marked as invisible.
  138. *
  139. * @return array Returns an array of application info arrays.
  140. */
  141. public function enabledVisibleApplications() {
  142. $availableApplications = $this->availableApplications();
  143. $enabledApplications = $this->enabledApplications();
  144. foreach ($availableApplications as $applicationName => $info) {
  145. if (array_key_exists($applicationName, $enabledApplications)) {
  146. if (!val('AllowEnable', $info, true) || !val('AllowDisable', $info, true)) {
  147. unset($availableApplications[$applicationName]);
  148. }
  149. } else {
  150. unset($availableApplications[$applicationName]);
  151. }
  152. }
  153. return $availableApplications;
  154. }
  155. /**
  156. * Get an list of enabled application folders.
  157. *
  158. * @return array Returns an array of all of the enabled application folders.
  159. * @deprecated
  160. */
  161. public function enabledApplicationFolders() {
  162. deprecated('Gdn_ApplicationManager->enabledApplicationFolders()');
  163. $addons = $this->addonManager->getEnabled();
  164. $applications = array_filter($addons, Addon::makeFilterCallback(['oldType' => 'application']));
  165. $result = ['dashboard'];
  166. /* @var Addon $application */
  167. foreach ($applications as $application) {
  168. $result[] = $application->getKey();
  169. }
  170. return array_unique($result);
  171. }
  172. /**
  173. * Check that the requirements for an application have been enabled.
  174. *
  175. * @param string $applicationName The name of the application to check.
  176. */
  177. public function checkRequirements($applicationName) {
  178. $availableApplications = $this->availableApplications();
  179. $requiredApplications = val(
  180. 'RequiredApplications',
  181. val($applicationName, $availableApplications, []),
  182. false
  183. );
  184. $enabledApplications = $this->enabledApplications();
  185. checkRequirements($applicationName, $requiredApplications, $enabledApplications, 'application');
  186. }
  187. /**
  188. * Enable an application.
  189. *
  190. * @param string $applicationName The name of the application to enable.
  191. * @return bool Returns true if the application was enabled or false otherwise.
  192. */
  193. public function enableApplication($applicationName) {
  194. $this->testApplication($applicationName);
  195. $applicationInfo = arrayValueI($applicationName, $this->availableApplications(), []);
  196. $applicationName = $applicationInfo['Index'];
  197. $applicationFolder = val('Folder', $applicationInfo, '');
  198. saveToConfig('EnabledApplications'.'.'.$applicationName, $applicationFolder);
  199. Logger::event(
  200. 'addon_enabled',
  201. Logger::NOTICE,
  202. 'The {addonName} application was enabled.',
  203. ['addonName' => $applicationName]
  204. );
  205. $this->EventArguments['AddonName'] = $applicationName;
  206. Gdn::pluginManager()->callEventHandlers($this, 'ApplicationManager', 'AddonEnabled');
  207. return true;
  208. }
  209. /**
  210. * Test if an application can be enabled.
  211. *
  212. * @param string $applicationName The name of the application to test.
  213. * @return bool Returns true if the application can be enabled or false otherwise.
  214. * @throws Exception Throws an exception if the application is not in the correct format.
  215. */
  216. public function testApplication($applicationName) {
  217. // Add the application to the $EnabledApplications array in conf/applications.php
  218. $ApplicationInfo = arrayValueI($applicationName, $this->availableApplications(), []);
  219. $applicationName = $ApplicationInfo['Index'];
  220. $ApplicationFolder = val('Folder', $ApplicationInfo, '');
  221. if ($ApplicationFolder == '') {
  222. throw new Exception(t('The application folder was not properly defined.'));
  223. }
  224. // Hook directly into the autoloader and force it to load the newly tested application
  225. $this->addonManager->startAddonsByKey([$applicationName], \Vanilla\Addon::TYPE_ADDON);
  226. // Call the application's setup method
  227. $hooks = $applicationName.'Hooks';
  228. if (!class_exists($hooks)) {
  229. $hooksPath = PATH_APPLICATIONS.DS.$ApplicationFolder.'/settings/class.hooks.php';
  230. if (file_exists($hooksPath)) {
  231. include_once $hooksPath;
  232. }
  233. }
  234. if (class_exists($hooks)) {
  235. /* @var Gdn_IPlugin $hooks The hooks object should be a plugin. */
  236. $hooks = Gdn::getContainer()->get($hooks);
  237. if (method_exists($hooks, 'setup')) {
  238. $hooks->setup();
  239. }
  240. }
  241. return true;
  242. }
  243. /**
  244. * Disable an application.
  245. *
  246. * @param string $applicationName The name of the application to disable.
  247. * @throws \Exception Throws an exception if the application can't be disabled.
  248. */
  249. public function disableApplication($applicationName) {
  250. $addon = $this->addonManager->lookupAddon($applicationName);
  251. if (!$addon) {
  252. throw notFoundException('Application');
  253. }
  254. $applicationName = $addon->getRawKey();
  255. // 1. Check to make sure that this application is allowed to be disabled
  256. if (!$addon->getInfoValue('allowDisable', true)) {
  257. throw new Exception(sprintf(t('You cannot disable the %s application.'), $applicationName));
  258. }
  259. // 2. Check to make sure that no other enabled applications rely on this one.
  260. try {
  261. $this->addonManager->checkDependents($addon, true);
  262. } catch (Exception $ex) {
  263. throw new Gdn_UserException($ex->getMessage(), $ex->getCode());
  264. }
  265. // 2. Disable it
  266. removeFromConfig("EnabledApplications.{$applicationName}");
  267. Logger::event(
  268. 'addon_disabled',
  269. Logger::NOTICE,
  270. 'The {addonName} application was disabled.',
  271. ['addonName' => $applicationName]
  272. );
  273. // Clear the object caches.
  274. $this->addonManager->stopAddonsByKey([$applicationName], \Vanilla\Addon::TYPE_ADDON);
  275. /** @var \Garden\EventManager $eventManager */
  276. $eventManager = Gdn::getContainer()->get(\Garden\EventManager::class);
  277. $this->addonManager->unbindAddonEvents($addon, $eventManager);
  278. }
  279. /**
  280. * Check whether or not an application is enabled.
  281. *
  282. * @param string $name The name of the application.
  283. * @return bool Whether or not the application is enabled.
  284. * @since 2.2
  285. * @deprecated
  286. */
  287. public function isEnabled($name) {
  288. deprecated('Gdn_ApplicationManager->isEnabled()', 'AddonManager->isEnabled()');
  289. return $this->addonManager->isEnabled($name, Addon::TYPE_ADDON);
  290. }
  291. /**
  292. * Define the permissions for an application.
  293. *
  294. * @param string $applicationName The name of the application.
  295. */
  296. public function registerPermissions($applicationName) {
  297. $addon = $this->addonManager->lookupAddon($applicationName);
  298. if ($permissions = $addon->getInfoValue('registerPermissions')) {
  299. Gdn::permissionModel()->define($permissions);
  300. }
  301. }
  302. }