NodeFactory.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * This class is responsible for creating Node objects
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Navigation;
  7. use PhpMyAdmin\Navigation\Nodes\Node;
  8. use const E_USER_ERROR;
  9. use function class_exists;
  10. use function preg_match;
  11. use function sprintf;
  12. use function trigger_error;
  13. /**
  14. * Node factory - instantiates Node objects or objects derived from the Node class
  15. */
  16. class NodeFactory
  17. {
  18. /** @var string */
  19. protected static $namespace = 'PhpMyAdmin\\Navigation\\Nodes\\%s';
  20. /**
  21. * Sanitizes the name of a Node class
  22. *
  23. * @param string $class The class name to be sanitized
  24. *
  25. * @return string
  26. */
  27. private static function sanitizeClass($class)
  28. {
  29. if (! preg_match('@^Node\w*$@', $class)) {
  30. $class = 'Node';
  31. trigger_error(
  32. sprintf(
  33. /* l10n: The word "Node" must not be translated here */
  34. __('Invalid class name "%1$s", using default of "Node"'),
  35. $class
  36. ),
  37. E_USER_ERROR
  38. );
  39. }
  40. return self::checkClass($class);
  41. }
  42. /**
  43. * Checks if a class exists and try to load it.
  44. * Will return the default class name back if the
  45. * file for some subclass is not available
  46. *
  47. * @param string $class The class name to check
  48. *
  49. * @return string
  50. */
  51. private static function checkClass($class)
  52. {
  53. $class = sprintf(self::$namespace, $class);
  54. if (! class_exists($class)) {
  55. $class = sprintf(self::$namespace, 'Node');
  56. trigger_error(
  57. sprintf(
  58. __('Could not load class "%1$s"'),
  59. $class
  60. ),
  61. E_USER_ERROR
  62. );
  63. }
  64. return $class;
  65. }
  66. /**
  67. * Instantiates a Node object
  68. *
  69. * @param string $class The name of the class to instantiate
  70. * @param string $name An identifier for the new node
  71. * @param int $type Type of node, may be one of CONTAINER or OBJECT
  72. * @param bool $isGroup Whether this object has been created
  73. * while grouping nodes
  74. */
  75. public static function getInstance(
  76. $class = 'Node',
  77. $name = 'default',
  78. $type = Node::OBJECT,
  79. $isGroup = false
  80. ): Node {
  81. $class = self::sanitizeClass($class);
  82. return new $class($name, $type, $isGroup);
  83. }
  84. /**
  85. * Instantiates a Node object that will be used only for "New db/table/etc.." objects
  86. *
  87. * @param string $name An identifier for the new node
  88. * @param string $classes Extra CSS classes for the node
  89. */
  90. public static function getInstanceForNewNode(
  91. string $name,
  92. string $classes
  93. ): Node {
  94. $node = new Node($name, Node::OBJECT, false);
  95. $node->title = $name;
  96. $node->isNew = true;
  97. $node->classes = $classes;
  98. return $node;
  99. }
  100. }