PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/plugins/DbFinderPlugin/lib/DbFinderAdapterUtils.php

http://opac-sbweb.googlecode.com/
PHP | 117 lines | 82 code | 8 blank | 27 comment | 7 complexity | b9ef7d1018b315f5d95c0d05cd00ddcb MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. class DbFinderAdapterUtils
  3. {
  4. const
  5. DOCTRINE = "Doctrine",
  6. PROPEL = "Propel";
  7. /**
  8. * Possible DbFinder adapters
  9. * Allow addition of more adapters by way of app.yml
  10. */
  11. protected static function getPossibleAdapters()
  12. {
  13. return sfConfig::get('app_DbFinder_adapters', array(
  14. self::PROPEL => array(
  15. 'model_class' => 'BaseObject',
  16. 'adapter_class' => 'sfPropelFinder',
  17. 'generator_class' => 'sfPropelAdminGenerator',
  18. 'column_class' => 'sfPropelFinderColumn'
  19. ),
  20. self::DOCTRINE => array(
  21. 'model_class' => 'Doctrine_Record',
  22. 'adapter_class' => 'sfDoctrineFinder',
  23. 'generator_class' => 'sfDoctrineAdminGenerator',
  24. 'column_class' => 'sfDoctrineFinderColumn'
  25. )
  26. ));
  27. }
  28. static $adaptersParamsFor = array();
  29. /**
  30. * Find a DbFinder adapter parameters for the provided class
  31. * Uses an internal static cache for speed
  32. *
  33. * @param Mixed $baseClass a model object or class name
  34. *
  35. * @return Array A list of properties ('name', 'adapter_class', 'generator_class')
  36. * @throws Exception if the provided parameter doesn't correspond to any supported model
  37. * @see getPossibleAdapters()
  38. */
  39. public static function getParams($baseClass)
  40. {
  41. $baseClassName = is_string($baseClass) ? $baseClass : get_class($baseClass);
  42. if(!array_key_exists($baseClassName, self::$adaptersParamsFor))
  43. {
  44. $found = false;
  45. $baseObject = is_object($baseClass) ? $baseClass : new $baseClass;
  46. foreach(self::getPossibleAdapters() as $type => $adapterParams)
  47. {
  48. if($baseObject instanceof $adapterParams['model_class'])
  49. {
  50. self::$adaptersParamsFor[$baseClassName] = array($adapterParams['adapter_class'], $type);
  51. $found = true;
  52. continue;
  53. }
  54. }
  55. if(!$found)
  56. {
  57. // no adapter found
  58. throw new Exception(sprintf('DbFinder has no adapter for a model object of class %s', $baseClassName));
  59. }
  60. }
  61. return self::$adaptersParamsFor[$baseClassName];
  62. }
  63. /**
  64. * Finds the adapter type of a finder based on its class and the adapters configuration
  65. *
  66. * @param sfModelFinder $adapter A finder instance
  67. *
  68. * @return String an adapter type (self::PROPEL, self::DOCTRINE, or more depending on the configuration)
  69. * @throws Exception if the provided finder is not supported
  70. * @see getPossibleAdapters()
  71. */
  72. public static function getType($adapter)
  73. {
  74. foreach(self::getPossibleAdapters() as $type => $adapterParams)
  75. {
  76. if($adapter instanceof $adapterParams['adapter_class'])
  77. {
  78. return $type;
  79. }
  80. }
  81. // no adapter found
  82. throw new Exception(sprintf('DbFinder has no parameters for a finder object of class %s', get_class($adapter)));
  83. }
  84. public static function getGenerator($modelClass)
  85. {
  86. $tmp = new $modelClass();
  87. foreach(self::getPossibleAdapters() as $type => $adapterParams)
  88. {
  89. if($tmp instanceof $adapterParams['model_class'])
  90. {
  91. return array($adapterParams['generator_class'], $type);
  92. }
  93. }
  94. // no adapter found
  95. throw new Exception(sprintf('DbFinder has no generator for a model object of class %s', $modelClass));
  96. }
  97. public static function getColumn($type)
  98. {
  99. foreach (self::getPossibleAdapters() as $adapterType => $adapterParams)
  100. {
  101. if($adapterType == $type)
  102. {
  103. return $adapterParams['column_class'];
  104. }
  105. }
  106. // no adapter found
  107. throw new Exception(sprintf('DbFinder has no column finder defined for ORM %s', $ype));
  108. }
  109. }