Brak opisu

FactoryClass.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. Copyright (c) 2009 hamcrest.org
  4. */
  5. class FactoryClass
  6. {
  7. /**
  8. * @var string
  9. */
  10. private $file;
  11. /**
  12. * @var ReflectionClass
  13. */
  14. private $reflector;
  15. /**
  16. * @var array
  17. */
  18. private $methods;
  19. public function __construct($file, ReflectionClass $class)
  20. {
  21. $this->file = $file;
  22. $this->reflector = $class;
  23. $this->extractFactoryMethods();
  24. }
  25. public function extractFactoryMethods()
  26. {
  27. $this->methods = array();
  28. foreach ($this->getPublicStaticMethods() as $method) {
  29. if ($method->isFactory()) {
  30. $this->methods[] = $method;
  31. }
  32. }
  33. }
  34. public function getPublicStaticMethods()
  35. {
  36. $methods = array();
  37. foreach ($this->reflector->getMethods(ReflectionMethod::IS_STATIC) as $method) {
  38. if ($method->isPublic() && $method->getDeclaringClass() == $this->reflector) {
  39. $methods[] = new FactoryMethod($this, $method);
  40. }
  41. }
  42. return $methods;
  43. }
  44. public function getFile()
  45. {
  46. return $this->file;
  47. }
  48. public function getName()
  49. {
  50. return $this->reflector->name;
  51. }
  52. public function isFactory()
  53. {
  54. return !empty($this->methods);
  55. }
  56. public function getMethods()
  57. {
  58. return $this->methods;
  59. }
  60. }