/classes/Kohana/Dependency/Definition.php

https://github.com/Zeelot/kohana-dependencies · PHP · 177 lines · 135 code · 37 blank · 5 comment · 6 complexity · ea75e9067e0f48ecb35e0d8f5f410085 MD5 · raw file

  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. class Kohana_Dependency_Definition {
  3. public static function factory()
  4. {
  5. return new Dependency_Definition;
  6. }
  7. protected $_class = NULL;
  8. protected $_path = NULL;
  9. protected $_constructor = NULL;
  10. protected $_arguments = array();
  11. protected $_shared = FALSE;
  12. protected $_methods = array();
  13. public function from_array(array $settings)
  14. {
  15. // Remove all unneeded items
  16. $allowed_keys = array('class', 'path', 'constructor', 'arguments', 'shared', 'methods');
  17. $settings = array_filter(Arr::extract($settings, $allowed_keys));
  18. // Loop through and use the class's setter methods
  19. foreach ($settings as $key => $value)
  20. {
  21. // Decide which setter method to use
  22. $set = 'set_'.$key;
  23. $this->$set($value);
  24. }
  25. return $this;
  26. }
  27. public function set_class($class)
  28. {
  29. $this->_class = $class;
  30. return $this;
  31. }
  32. public function set_path($path)
  33. {
  34. // Make sure the path is a string
  35. if ( ! is_string($path))
  36. {
  37. $path = '';
  38. }
  39. // Make sure the path exists
  40. $file_path = NULL;
  41. if (strpos($path, '/') !== FALSE)
  42. {
  43. list($directory, $file) = explode('/', $path, 2);
  44. $file_path = Kohana::find_file($directory, $file);
  45. if (empty($file_path))
  46. throw new Dependency_Exception('Could not construct the dependency definition. An invalid path was provided.');
  47. }
  48. $this->_path = $file_path;
  49. return $this;
  50. }
  51. public function set_constructor($method)
  52. {
  53. $this->_constructor = $method;
  54. return $this;
  55. }
  56. public function set_arguments(array $arguments)
  57. {
  58. $this->_arguments = array();
  59. foreach ($arguments as $argument)
  60. {
  61. $this->add_argument($argument);
  62. }
  63. return $this;
  64. }
  65. public function set_shared($shared)
  66. {
  67. $this->_shared = (bool) $shared;
  68. return $this;
  69. }
  70. public function set_methods(array $methods)
  71. {
  72. $this->_methods = array();
  73. foreach ($methods as $method)
  74. {
  75. $method_name = Arr::get($method, 0);
  76. $arguments = Arr::get($method, 1, array());
  77. $this->add_method($method_name, $arguments);
  78. }
  79. return $this;
  80. }
  81. public function add_argument($argument)
  82. {
  83. $this->_arguments[] = $this->_handle_reference($argument);
  84. return $this;
  85. }
  86. public function add_method($method, array $arguments = array())
  87. {
  88. foreach ($arguments as & $argument)
  89. {
  90. $argument = $this->_handle_reference($argument);
  91. }
  92. $this->_methods[$method] = $arguments;
  93. return $this;
  94. }
  95. public function is_shared()
  96. {
  97. return (bool) $this->_shared;
  98. }
  99. public function merge_with(Dependency_Definition $right)
  100. {
  101. $left = clone $this;
  102. foreach (get_object_vars($this) as $key => $value)
  103. {
  104. if ( ! empty($right->$key))
  105. {
  106. $left->$key = $right->$key;
  107. }
  108. }
  109. return $left;
  110. }
  111. public function __get($property)
  112. {
  113. if (property_exists($this, '_'.$property))
  114. return $this->{'_'.$property};
  115. else
  116. return NULL;
  117. }
  118. public function __isset($property)
  119. {
  120. return (bool) property_exists($this, '_'.$property);
  121. }
  122. public function as_array()
  123. {
  124. $properties = array();
  125. foreach(get_object_vars($this) as $key => $value)
  126. {
  127. $key = ltrim($key, '_');
  128. $properties[$key] = $value;
  129. }
  130. return $properties;
  131. }
  132. protected function _handle_reference($argument)
  133. {
  134. if ( ! empty($argument) AND is_string($argument) AND in_array($argument[0], array('%', '@')))
  135. {
  136. $argument = Dependency_Reference::factory($argument);
  137. }
  138. return $argument;
  139. }
  140. }