/libraries/koowa/service/locator/module.php

https://github.com/raeldc/nooku-server · PHP · 121 lines · 54 code · 10 blank · 57 comment · 7 complexity · d7d3f5aacb857ba5b8b17360cc3a42da MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id$
  4. * @category Koowa
  5. * @package Koowa_Service
  6. * @subpackage Locator
  7. * @copyright Copyright (C) 2007 - 2010 Johan Janssens. All rights reserved.
  8. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  9. */
  10. /**
  11. * Service Locator for a plugin
  12. *
  13. * @author Johan Janssens <johan@nooku.org>
  14. * @category Koowa
  15. * @package Koowa_Service
  16. * @subpackage Locator
  17. */
  18. class KServiceLocatorModule extends KServiceLocatorAbstract
  19. {
  20. /**
  21. * The type
  22. *
  23. * @var string
  24. */
  25. protected $_type = 'mod';
  26. /**
  27. * Get the classname based on an identifier
  28. *
  29. * This locator will try to create an generic or default classname on the identifier information
  30. * if the actual class cannot be found using a predefined fallback sequence.
  31. *
  32. * Fallback sequence : -> Named Module Specific
  33. * -> Named Module Default
  34. * -> Default Module Specific
  35. * -> Default Module Default
  36. * -> Framework Specific
  37. * -> Framework Default
  38. *
  39. * @param mixed An identifier object - mod:[//application/]module.[.path].name
  40. * @return string|false Return object on success, returns FALSE on failure
  41. */
  42. public function findClass(KServiceIdentifier $identifier)
  43. {
  44. $path = KInflector::camelize(implode('_', $identifier->path));
  45. $classname = 'Mod'.ucfirst($identifier->package).$path.ucfirst($identifier->name);
  46. //Don't allow the auto-loader to load module classes if they don't exists yet
  47. if (!$this->getService('koowa:loader')->loadClass($classname, $identifier->basepath))
  48. {
  49. $classpath = $identifier->path;
  50. $classtype = !empty($classpath) ? array_shift($classpath) : $identifier->name;
  51. //Create the fallback path and make an exception for views
  52. $path = ($classtype != 'view') ? KInflector::camelize(implode('_', $classpath)) : '';
  53. /*
  54. * Find the classname to fallback too and auto-load the class
  55. *
  56. * Fallback sequence : -> Named Module Specific
  57. * -> Named Module Default
  58. * -> Default Module Specific
  59. * -> Default Module Default
  60. * -> Default Component Specific
  61. * -> Default Component Default
  62. * -> Framework Specific
  63. * -> Framework Default
  64. */
  65. if(class_exists('Mod'.ucfirst($identifier->package).ucfirst($classtype).$path.ucfirst($identifier->name))) {
  66. $classname = 'Mod'.ucfirst($identifier->package).ucfirst($classtype).$path.ucfirst($identifier->name);
  67. } elseif(class_exists('Mod'.ucfirst($identifier->package).ucfirst($classtype).$path.'Default')) {
  68. $classname = 'Mod'.ucfirst($identifier->package).ucfirst($classtype).$path.'Default';
  69. } elseif(class_exists('ModDefault'.ucfirst($classtype).$path.ucfirst($identifier->name))) {
  70. $classname = 'ModDefault'.ucfirst($classtype).$path.ucfirst($identifier->name);
  71. } elseif(class_exists('ModDefault'.ucfirst($classtype).$path.'Default')) {
  72. $classname = 'ModDefault'.ucfirst($classtype).$path.'Default';
  73. } elseif(class_exists('ComDefault'.ucfirst($classtype).$path.ucfirst($identifier->name))) {
  74. $classname = 'ComDefault'.ucfirst($classtype).$path.ucfirst($identifier->name);
  75. } elseif(class_exists('ComDefault'.ucfirst($classtype).$path.'Default')) {
  76. $classname = 'ComDefault'.ucfirst($classtype).$path.'Default';
  77. } elseif(class_exists( 'K'.ucfirst($classtype).$path.ucfirst($identifier->name))) {
  78. $classname = 'K'.ucfirst($classtype).$path.ucfirst($identifier->name);
  79. } elseif(class_exists('K'.ucfirst($classtype).$path.'Default')) {
  80. $classname = 'K'.ucfirst($classtype).$path.'Default';
  81. } else {
  82. $classname = false;
  83. }
  84. }
  85. return $classname;
  86. }
  87. /**
  88. * Get the path based on an identifier
  89. *
  90. * @param object An identifier object - mod:[//application/]module.[.path].name
  91. * @return string Returns the path
  92. */
  93. public function findPath(KServiceIdentifier $identifier)
  94. {
  95. $path = '';
  96. $parts = $identifier->path;
  97. $name = $identifier->package;
  98. if(!empty($identifier->name))
  99. {
  100. if(count($parts))
  101. {
  102. $path = KInflector::pluralize(array_shift($parts)).
  103. $path .= count($parts) ? '/'.implode('/', $parts) : '';
  104. $path .= '/'.strtolower($identifier->name);
  105. }
  106. else $path = strtolower($identifier->name);
  107. }
  108. $path = $identifier->basepath.'/modules/mod_'.$name.'/'.$path.'.php';
  109. return $path;
  110. }
  111. }