PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/craft/app/etc/console/ConsoleApp.php

https://gitlab.com/madebycloud/derekman
PHP | 283 lines | 124 code | 40 blank | 119 comment | 11 complexity | 3ee7a30f42ceed24326c859fced8b77c MD5 | raw file
  1. <?php
  2. namespace Craft;
  3. /**
  4. * Class ConsoleApp
  5. *
  6. * @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
  7. * @copyright Copyright (c) 2014, Pixel & Tonic, Inc.
  8. * @license http://buildwithcraft.com/license Craft License Agreement
  9. * @see http://buildwithcraft.com
  10. * @package craft.app.etc.console
  11. * @since 1.0
  12. */
  13. class ConsoleApp extends \CConsoleApplication
  14. {
  15. // Properties
  16. // =========================================================================
  17. /**
  18. * @var
  19. */
  20. public $componentAliases;
  21. /**
  22. * @var
  23. */
  24. private $_pendingEvents;
  25. /**
  26. * @var
  27. */
  28. private $_editionComponents;
  29. // Public Methods
  30. // =========================================================================
  31. /**
  32. * Initializes the console app by creating the command runner.
  33. *
  34. * @return null
  35. */
  36. public function init()
  37. {
  38. // Set default timezone to UTC
  39. date_default_timezone_set('UTC');
  40. // Import all the built-in components
  41. foreach ($this->componentAliases as $alias)
  42. {
  43. Craft::import($alias);
  44. }
  45. // Attach our Craft app behavior.
  46. $this->attachBehavior('AppBehavior', new AppBehavior());
  47. // Initialize Cache and LogRouter right away (order is important)
  48. $this->getComponent('cache');
  49. $this->getComponent('log');
  50. // So we can try to translate Yii framework strings
  51. $this->coreMessages->attachEventHandler('onMissingTranslation', array('Craft\LocalizationHelper', 'findMissingTranslation'));
  52. // Set our own custom runtime path.
  53. $this->setRuntimePath(craft()->path->getRuntimePath());
  54. // Attach our own custom Logger
  55. Craft::setLogger(new Logger());
  56. // No need for these.
  57. craft()->log->removeRoute('WebLogRoute');
  58. craft()->log->removeRoute('ProfileLogRoute');
  59. // Set the edition components
  60. $this->_setEditionComponents();
  61. // Call parent::init() before the plugin console command logic so the command runner gets initialized
  62. parent::init();
  63. // Load the plugins
  64. craft()->plugins->loadPlugins();
  65. // Validate some basics on the database configuration file.
  66. craft()->validateDbConfigFile();
  67. foreach (craft()->plugins->getPlugins() as $plugin)
  68. {
  69. $commandsPath = craft()->path->getPluginsPath().StringHelper::toLowerCase($plugin->getClassHandle()).'/consolecommands/';
  70. if (IOHelper::folderExists($commandsPath))
  71. {
  72. craft()->commandRunner->addCommands(rtrim($commandsPath, '/'));
  73. }
  74. }
  75. }
  76. /**
  77. * Returns the target application language.
  78. *
  79. * @return string
  80. */
  81. public function getLanguage()
  82. {
  83. return $this->asa('AppBehavior')->getLanguage();
  84. }
  85. /**
  86. * Sets the target application language.
  87. *
  88. * @param string $language
  89. *
  90. * @return null
  91. */
  92. public function setLanguage($language)
  93. {
  94. $this->asa('AppBehavior')->setLanguage($language);
  95. }
  96. /**
  97. * Attaches an event handler, or remembers it for later if the component has not been initialized yet.
  98. *
  99. * The event should be identified in a `serviceHandle.eventName` format. For example, if you want to add an event
  100. * handler for {@link EntriesService::onSaveEntry()}, you would do this:
  101. *
  102. * ```php
  103. * craft()->on('entries.saveEntry', function(Event $event) {
  104. * // ...
  105. * });
  106. * ```
  107. *
  108. * Note that the actual event name (`saveEntry`) does not need to include the “`on`”.
  109. *
  110. * By default, event handlers will not get attached if Craft is current in the middle of updating itself or a
  111. * plugin. If you want the event to fire even in that condition, pass `true` to the $evenDuringUpdates argument.
  112. *
  113. * @param string $event The event to listen for.
  114. * @param mixed $handler The event handler.
  115. * @param bool $evenDuringUpdates Whether the event handler should be attached when Craft’s updater is running.
  116. * Default is `false`.
  117. *
  118. * @return null
  119. */
  120. public function on($event, $handler, $evenDuringUpdates = false)
  121. {
  122. if (
  123. !$evenDuringUpdates &&
  124. ($this->getCommandRunner()->getCommand() instanceof \MigrateCommand)
  125. )
  126. {
  127. return;
  128. }
  129. list($componentId, $eventName) = explode('.', $event, 2);
  130. $component = $this->getComponent($componentId);
  131. // Normalize the event name
  132. if (strncmp($eventName, 'on', 2) !== 0)
  133. {
  134. $eventName = 'on'.ucfirst($eventName);
  135. }
  136. $component->$eventName = $handler;
  137. }
  138. /**
  139. * Returns whether we are executing in the context on a console app.
  140. *
  141. * @return bool
  142. */
  143. public function isConsole()
  144. {
  145. return true;
  146. }
  147. /**
  148. * Override getComponent() so we can attach any pending events if the component is getting initialized as well as
  149. * do some special logic around creating the `craft()->db` application component.
  150. *
  151. * @param string $id
  152. * @param bool $createIfNull
  153. *
  154. * @return mixed
  155. */
  156. public function getComponent($id, $createIfNull = true)
  157. {
  158. $component = parent::getComponent($id, false);
  159. if (!$component && $createIfNull)
  160. {
  161. if ($id === 'db')
  162. {
  163. $dbConnection = $this->asa('AppBehavior')->createDbConnection();
  164. $this->setComponent('db', $dbConnection);
  165. }
  166. $component = parent::getComponent($id, true);
  167. $this->_attachEventListeners($id);
  168. }
  169. return $component;
  170. }
  171. /**
  172. * Sets the application components.
  173. *
  174. * @param $components
  175. * @param bool $merge
  176. *
  177. * @return null
  178. */
  179. public function setComponents($components, $merge = true)
  180. {
  181. if (isset($components['editionComponents']))
  182. {
  183. $this->_editionComponents = $components['editionComponents'];
  184. unset($components['editionComponents']);
  185. }
  186. parent::setComponents($components, $merge);
  187. }
  188. // Protected Methods
  189. // =========================================================================
  190. /**
  191. * @return ConsoleCommandRunner
  192. */
  193. protected function createCommandRunner()
  194. {
  195. return new ConsoleCommandRunner();
  196. }
  197. // Private Methods
  198. // =========================================================================
  199. /**
  200. * Attaches any pending event listeners to the newly-initialized component.
  201. *
  202. * @param string $componentId
  203. *
  204. * @return null
  205. */
  206. private function _attachEventListeners($componentId)
  207. {
  208. if (isset($this->_pendingEvents[$componentId]))
  209. {
  210. $component = $this->getComponent($componentId, false);
  211. if ($component)
  212. {
  213. foreach ($this->_pendingEvents[$componentId] as $eventName => $handlers)
  214. {
  215. foreach ($handlers as $handler)
  216. {
  217. $component->$eventName = $handler;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. /**
  224. * Sets the edition components.
  225. *
  226. * @return null
  227. */
  228. private function _setEditionComponents()
  229. {
  230. // Set the appropriate edition components
  231. if (isset($this->_editionComponents))
  232. {
  233. foreach ($this->_editionComponents as $edition => $editionComponents)
  234. {
  235. if ($this->getEdition() >= $edition)
  236. {
  237. $this->setComponents($editionComponents);
  238. }
  239. }
  240. unset($this->_editionComponents);
  241. }
  242. }
  243. }