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

/application/Bootstrap.php

http://github.com/astorm/Job-Queue
PHP | 176 lines | 110 code | 15 blank | 51 comment | 6 complexity | 52c5caf5d870c05c4fb21b03c6409a23 MD5 | raw file
  1. <?php
  2. /*
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2009-2013 Alan Storm
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  26. {
  27. /**
  28. * Initilizes autoloader so Alanstormdotcom classes from the library load
  29. */
  30. protected function _initAutoload()
  31. {
  32. $autoloader = new Zend_Application_Module_Alanstormdotcom_Autoloader(array(
  33. 'namespace' => 'Alanstormdotcom',
  34. 'basePath' => dirname(__FILE__) . '/../library',
  35. ));
  36. return $autoloader;
  37. }
  38. /**
  39. * Tries to guess the path to the ZendFramework files based on include path
  40. */
  41. private function guessFrameworkIncludePath()
  42. {
  43. $paths = explode(':',get_include_path());
  44. foreach($paths as $path)
  45. {
  46. if(strpos($path,'ZendFramework') !== false)
  47. {
  48. return $path;
  49. }
  50. }
  51. #return '/path/to/ZendFramework';
  52. throw new Exception('Could not Find Path to ZendFramework');
  53. }
  54. /**
  55. * Checks for existence of message queue tables
  56. *
  57. * Should probably examine the contents as well, but this
  58. * will do for now
  59. * @param stdClass $db
  60. * @return boolean
  61. */
  62. private function hasQueueTables($db)
  63. {
  64. try{
  65. $db->describeTable('message');
  66. }
  67. catch (Exception $e)
  68. {
  69. return false;
  70. }
  71. try{
  72. $db->describeTable('queue');
  73. }
  74. catch (Exception $e)
  75. {
  76. return false;
  77. }
  78. return true;
  79. }
  80. private function getMysqlFileContents($path)
  81. {
  82. $path = $this->guessFrameworkIncludePath();
  83. $full_path = $path .
  84. '/' .
  85. 'Zend/Queue/Adapter/Db/queue.sql';
  86. if(file_exists($full_path))
  87. {
  88. return file_get_contents($full_path);
  89. }
  90. $full_path = $path .
  91. '/' .
  92. 'Zend/Queue/Adapter/Db/mysql.sql';
  93. if(file_exists($full_path))
  94. {
  95. return file_get_contents($full_path);
  96. }
  97. //if we're still here we couldn't find a file
  98. throw new Exception('Could not find sql file to create message queue');
  99. }
  100. /**
  101. * Creates the message queue mysql tables if they don't exist already
  102. *
  103. * This can be removed/commented out if the tables are already there
  104. */
  105. protected function _initMessageQueueTables()
  106. {
  107. $sql = $this->getMysqlFileContents($path);
  108. if ($this->hasPluginResource("db"))
  109. {
  110. $dbResource = $this->getPluginResource("db");
  111. $db = $dbResource->getDbAdapter();
  112. if(!$this->hasQueueTables($db))
  113. {
  114. $db->query($sql);
  115. }
  116. }
  117. else
  118. {
  119. throw new Exception('No db Plugin Resource');
  120. }
  121. }
  122. /**
  123. * Inits a db resource handler. Needed for prerequisites
  124. */
  125. const DB_PREREQUISITES_NAME = 'db';
  126. protected function _initDb()
  127. {
  128. if ($this->hasPluginResource("db"))
  129. {
  130. $dbResource = $this->getPluginResource("db");
  131. $db = $dbResource->getDbAdapter();
  132. Zend_Registry::set(self::DB_PREREQUISITES_NAME, $db);
  133. }
  134. else
  135. {
  136. throw new Exception('No db Plugin Resource');
  137. }
  138. }
  139. /**
  140. * Creates the job queue with a mysql message queue adapter, using
  141. * using the default database driver from the application config
  142. */
  143. const NAME_ORDERQUEUE = 'job_queue';
  144. protected function _initOrdersQueue()
  145. {
  146. $config = $this->getOptions();
  147. $options = array(
  148. 'name' => self::NAME_ORDERQUEUE,
  149. 'driverOptions' => array(
  150. 'host' => $config['resources']['db']['params']['host'],
  151. 'port' => '3306',
  152. 'username' => $config['resources']['db']['params']['username'],
  153. 'password' => $config['resources']['db']['params']['password'],
  154. 'dbname' => $config['resources']['db']['params']['dbname'],
  155. 'type' => 'pdo_mysql'
  156. )
  157. );
  158. $queue = new Alanstormdotcom_Job_Queue('Db', $options);
  159. Zend_Registry::set(self::NAME_ORDERQUEUE,$queue);
  160. }
  161. }