Manager.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace TYPO3\PharStreamWrapper;
  3. /*
  4. * This file is part of the TYPO3 project.
  5. *
  6. * It is free software; you can redistribute it and/or modify it under the terms
  7. * of the MIT License (MIT). For the full copyright and license information,
  8. * please read the LICENSE file that was distributed with this source code.
  9. *
  10. * The TYPO3 project - inspiring people to share!
  11. */
  12. use TYPO3\PharStreamWrapper\Resolver\PharInvocation;
  13. use TYPO3\PharStreamWrapper\Resolver\PharInvocationCollection;
  14. use TYPO3\PharStreamWrapper\Resolver\PharInvocationResolver;
  15. class Manager
  16. {
  17. /**
  18. * @var self
  19. */
  20. private static $instance;
  21. /**
  22. * @var Behavior
  23. */
  24. private $behavior;
  25. /**
  26. * @var Resolvable
  27. */
  28. private $resolver;
  29. /**
  30. * @var Collectable
  31. */
  32. private $collection;
  33. /**
  34. * @param Behavior $behaviour
  35. * @param Resolvable $resolver
  36. * @param Collectable $collection
  37. * @return self
  38. */
  39. public static function initialize(
  40. Behavior $behaviour,
  41. Resolvable $resolver = null,
  42. Collectable $collection = null
  43. ) {
  44. if (self::$instance === null) {
  45. self::$instance = new self($behaviour, $resolver, $collection);
  46. return self::$instance;
  47. }
  48. throw new \LogicException(
  49. 'Manager can only be initialized once',
  50. 1535189871
  51. );
  52. }
  53. /**
  54. * @return self
  55. */
  56. public static function instance()
  57. {
  58. if (self::$instance !== null) {
  59. return self::$instance;
  60. }
  61. throw new \LogicException(
  62. 'Manager needs to be initialized first',
  63. 1535189872
  64. );
  65. }
  66. /**
  67. * @return bool
  68. */
  69. public static function destroy()
  70. {
  71. if (self::$instance === null) {
  72. return false;
  73. }
  74. self::$instance = null;
  75. return true;
  76. }
  77. /**
  78. * @param Behavior $behaviour
  79. * @param Resolvable $resolver
  80. * @param Collectable $collection
  81. */
  82. private function __construct(
  83. Behavior $behaviour,
  84. Resolvable $resolver = null,
  85. Collectable $collection = null
  86. ) {
  87. if ($collection === null) {
  88. $collection = new PharInvocationCollection();
  89. }
  90. if ($resolver === null) {
  91. $resolver = new PharInvocationResolver();
  92. }
  93. $this->collection = $collection;
  94. $this->resolver = $resolver;
  95. $this->behavior = $behaviour;
  96. }
  97. /**
  98. * @param string $path
  99. * @param string $command
  100. * @return bool
  101. */
  102. public function assert($path, $command)
  103. {
  104. return $this->behavior->assert($path, $command);
  105. }
  106. /**
  107. * @param string $path
  108. * @param null|int $flags
  109. * @return null|PharInvocation
  110. */
  111. public function resolve($path, $flags = null)
  112. {
  113. return $this->resolver->resolve($path, $flags);
  114. }
  115. /**
  116. * @return Collectable
  117. */
  118. public function getCollection()
  119. {
  120. return $this->collection;
  121. }
  122. }