Application.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * Provides a generic pattern for different mapping types within the application
  4. * directory.
  5. *
  6. * @author Bob Mckee <bmckee@bywires.com>
  7. * @author Chuck Hagenbuch <chuck@horde.org>
  8. * @category Horde
  9. * @package Autoloader
  10. */
  11. class Horde_Autoloader_ClassPathMapper_Application implements Horde_Autoloader_ClassPathMapper
  12. {
  13. protected $_appDir;
  14. protected $_mappings = [];
  15. /**
  16. * The following constants are for naming the positions in the regex for
  17. * easy readability later.
  18. */
  19. const APPLICATION_POS = 1;
  20. const ACTION_POS = 2;
  21. const SUFFIX_POS = 3;
  22. const NAME_SEGMENT = '([0-9A-Z][0-9A-Za-z]+)+';
  23. public function __construct($appDir)
  24. {
  25. $this->_appDir = rtrim($appDir, '/') . '/';
  26. }
  27. public function addMapping($classSuffix, $subDir)
  28. {
  29. $this->_mappings[$classSuffix] = $subDir;
  30. $this->_classMatchRegex = '/^' . self::NAME_SEGMENT . '_' . self::NAME_SEGMENT . '_' .
  31. '(' . implode('|', array_keys($this->_mappings)) . ')$/';
  32. }
  33. public function mapToPath($className)
  34. {
  35. if (preg_match($this->_classMatchRegex, $className, $matches)) {
  36. return $this->_appDir . $this->_mappings[$matches[self::SUFFIX_POS]] . '/' . $matches[self::ACTION_POS] . '.php';
  37. }
  38. }
  39. public function __toString()
  40. {
  41. return get_class($this) . ' ' . $this->_classMatchRegex . ' [' . $this->_appDir . ']';
  42. }
  43. }