NodeFactory.class.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * This class is responsible for creating Node objects
  5. *
  6. * @package PhpMyAdmin-navigation
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. require_once 'libraries/navigation/Nodes/Node.class.php';
  12. /**
  13. * Node factory - instanciates Node objects or objects derived from the Node class
  14. *
  15. * @package PhpMyAdmin-Navigation
  16. */
  17. class PMA_NodeFactory
  18. {
  19. /**
  20. * @var string $_path A template for generating paths to files
  21. * that contain various Node classes
  22. * @access private
  23. */
  24. private static $_path = 'libraries/navigation/Nodes/%s.class.php';
  25. /**
  26. * Sanitizes the name of a Node class
  27. *
  28. * @param string $class The class name to be sanitized
  29. *
  30. * @return string
  31. */
  32. private static function _sanitizeClass($class)
  33. {
  34. if ($class !== 'Node' && ! preg_match('@^Node_\w+(_\w+)?$@', $class)) {
  35. $class = 'Node';
  36. trigger_error(
  37. sprintf(
  38. /* l10n: The word "Node" must not be translated here */
  39. __('Invalid class name "%1$s", using default of "Node"'),
  40. $class
  41. ),
  42. E_USER_ERROR
  43. );
  44. }
  45. return self::_checkFile($class);
  46. }
  47. /**
  48. * Checks if a file exists for a given class name
  49. * Will return the default class name back if the
  50. * file for some subclass is not available
  51. *
  52. * @param string $class The class name to check
  53. *
  54. * @return string
  55. */
  56. private static function _checkFile($class)
  57. {
  58. $path = sprintf(self::$_path, $class);
  59. if (! is_readable($path)) {
  60. $class = 'Node';
  61. trigger_error(
  62. sprintf(
  63. __('Could not include class "%1$s", file "%2$s" not found'),
  64. $class,
  65. 'Nodes/' . $class . '.class.php'
  66. ),
  67. E_USER_ERROR
  68. );
  69. }
  70. return $class;
  71. }
  72. /**
  73. * Instanciates a Node object
  74. *
  75. * @param string $class The name of the class to instanciate
  76. * @param string $name An identifier for the new node
  77. * @param int $type Type of node, may be one of CONTAINER or OBJECT
  78. * @param bool $is_group Whether this object has been created
  79. * while grouping nodes
  80. *
  81. * @return string
  82. */
  83. public static function getInstance(
  84. $class = 'Node',
  85. $name = 'default',
  86. $type = Node::OBJECT,
  87. $is_group = false
  88. ) {
  89. $class = self::_sanitizeClass($class);
  90. include_once sprintf(self::$_path, $class);
  91. return new $class($name, $type, $is_group);
  92. }
  93. }
  94. ?>