/libraries/rokcommon/RokCommon/Doctrine.php

https://bitbucket.org/nlabyt/bcf-ball-4eb2 · PHP · 198 lines · 108 code · 27 blank · 63 comment · 8 complexity · 8b3ddb1250c9dd54d102ddc7a7510e72 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: Doctrine.php 48966 2012-02-13 23:48:42Z steph $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. defined('ROKCOMMON') or die;
  9. /**
  10. *
  11. */
  12. class RokCommon_Doctrine
  13. {
  14. /** @var RokCommon_Doctrine */
  15. protected static $_instance;
  16. /** @var Doctrine_Manager */
  17. protected $manager;
  18. /** @var RokCommon_Doctrine_Platform */
  19. protected $platform_instance;
  20. /** @var Doctrine_Connection */
  21. protected $connection;
  22. /** @var Doctrine_Cache_Db */
  23. protected $cacheDriver;
  24. /**
  25. * @static
  26. * @return RokCommon_Doctrine
  27. */
  28. public static function getInstance()
  29. {
  30. if (!isset(self::$_instance)) {
  31. self::$_instance = new RokCommon_Doctrine();
  32. }
  33. return self::$_instance;
  34. }
  35. /**
  36. *
  37. */
  38. protected function __construct()
  39. {
  40. if (!ini_get('date.timezone')) {
  41. date_default_timezone_set('UTC');
  42. }
  43. $this->loadPlatformInstance();
  44. // load the doctrine class loader
  45. spl_autoload_register(array('Doctrine', 'autoload'));
  46. // Load up the manager and put base settings
  47. $this->manager = Doctrine_Manager::getInstance();
  48. $this->manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR);
  49. $this->manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);
  50. $this->_initializeExtensions();
  51. $this->connection = Doctrine_Manager::connection($this->platform_instance->getConnectionUrl(), 'default');
  52. }
  53. /**
  54. * Initialize all the custom Doctrime Extensions
  55. */
  56. protected function _initializeExtensions()
  57. {
  58. spl_autoload_register(array('Doctrine', 'extensionsAutoload'));
  59. $extention_dir = dirname(__FILE__) . '/Doctrine/Extensions';
  60. if ($handle = opendir($extention_dir)) {
  61. while (false !== ($file = readdir($handle))) {
  62. if (!preg_match('/^\./', $file) && is_dir($extention_dir . '/' . $file)) {
  63. $this->manager->registerExtension($file, $extention_dir . '/' . $file);
  64. }
  65. }
  66. closedir($handle);
  67. }
  68. }
  69. /**
  70. * @throws RokCommon_Loader_Exception
  71. * @return RokCommon_Doctrine_Platform
  72. */
  73. protected function loadPlatformInstance()
  74. {
  75. if (!isset($this->platform_instance)) {
  76. $container = RokCommon_Service::getContainer();
  77. $this->platform_instance = $container->doctrine_platform;
  78. }
  79. }
  80. /**
  81. * @static
  82. *
  83. * @param $path
  84. *
  85. * @return void
  86. */
  87. public static function addModelPath($path)
  88. {
  89. $self = self::getInstance();
  90. RokCommon_ClassLoader::addPath($path);
  91. Doctrine_Core::loadModels($path);
  92. }
  93. /**
  94. * @static
  95. * @return Doctrine_Connection
  96. */
  97. public static function getConnection()
  98. {
  99. $self = self::getInstance();
  100. return $self->connection;
  101. }
  102. /**
  103. * @return Doctrine_Manager
  104. */
  105. public static function &getManager()
  106. {
  107. $self = self::getInstance();
  108. return $self->manager;
  109. }
  110. /**
  111. * @return RokCommon_Doctrine_Platform
  112. */
  113. public static function &getPlatformInstance()
  114. {
  115. $self = self::getInstance();
  116. return $self->platform_instance;
  117. }
  118. /**
  119. * @static
  120. *
  121. */
  122. public static function useApcCache()
  123. {
  124. $self = self::getInstance();
  125. $self->cacheDriver = new Doctrine_Cache_Apc();
  126. $self->manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $self->cacheDriver);
  127. $self->manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, $self->cacheDriver);
  128. }
  129. /**
  130. * @static
  131. *
  132. */
  133. public static function useMemDBCache($identifier = 'rokcommon')
  134. {
  135. if (phpversion('pdo_sqlite')) {
  136. $self = self::getInstance();
  137. $cacheConn = Doctrine_Manager::connection(new PDO('sqlite::memory:'));
  138. $self->cacheDriver = new Doctrine_Cache_Db(array(
  139. 'connection' => $cacheConn,
  140. 'tableName' => $identifier
  141. ));
  142. $self->cacheDriver->createTable();
  143. $self->manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $self->cacheDriver);
  144. $self->manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, $self->cacheDriver);
  145. $self->manager->setCurrentConnection($self->connection->getName());
  146. }
  147. }
  148. /**
  149. * @static
  150. *
  151. * @param string $host
  152. * @param int $port
  153. * @param bool $persistent
  154. * @param bool $compression
  155. */
  156. public static function useMemcacheCache($host = 'localhost', $port = 11211, $persistent = false, $compression = false)
  157. {
  158. $self = self::getInstance();
  159. $servers = array(
  160. 'host' => $host,
  161. 'port' => $port,
  162. 'persistent' => $persistent
  163. );
  164. $self->cacheDriver = new Doctrine_Cache_Memcache(array(
  165. 'servers' => $servers,
  166. 'compression' => $compression
  167. ));
  168. $self->manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $self->cacheDriver);
  169. $self->manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, $self->cacheDriver);
  170. }
  171. }