Behavior.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. class Behavior implements Assertable
  13. {
  14. const COMMAND_DIR_OPENDIR = 'dir_opendir';
  15. const COMMAND_MKDIR = 'mkdir';
  16. const COMMAND_RENAME = 'rename';
  17. const COMMAND_RMDIR = 'rmdir';
  18. const COMMAND_STEAM_METADATA = 'stream_metadata';
  19. const COMMAND_STREAM_OPEN = 'stream_open';
  20. const COMMAND_UNLINK = 'unlink';
  21. const COMMAND_URL_STAT = 'url_stat';
  22. /**
  23. * @var string[]
  24. */
  25. private $availableCommands = array(
  26. self::COMMAND_DIR_OPENDIR,
  27. self::COMMAND_MKDIR,
  28. self::COMMAND_RENAME,
  29. self::COMMAND_RMDIR,
  30. self::COMMAND_STEAM_METADATA,
  31. self::COMMAND_STREAM_OPEN,
  32. self::COMMAND_UNLINK,
  33. self::COMMAND_URL_STAT,
  34. );
  35. /**
  36. * @var Assertable[]
  37. */
  38. private $assertions;
  39. /**
  40. * @param Assertable $assertable
  41. * @return static
  42. */
  43. public function withAssertion(Assertable $assertable)
  44. {
  45. $commands = func_get_args();
  46. array_shift($commands);
  47. $this->assertCommands($commands);
  48. $commands = $commands ?: $this->availableCommands;
  49. $target = clone $this;
  50. foreach ($commands as $command) {
  51. $target->assertions[$command] = $assertable;
  52. }
  53. return $target;
  54. }
  55. /**
  56. * @param string $path
  57. * @param string $command
  58. * @return bool
  59. */
  60. public function assert($path, $command)
  61. {
  62. $this->assertCommand($command);
  63. $this->assertAssertionCompleteness();
  64. return $this->assertions[$command]->assert($path, $command);
  65. }
  66. /**
  67. * @param array $commands
  68. */
  69. private function assertCommands(array $commands)
  70. {
  71. $unknownCommands = array_diff($commands, $this->availableCommands);
  72. if (empty($unknownCommands)) {
  73. return;
  74. }
  75. throw new \LogicException(
  76. sprintf(
  77. 'Unknown commands: %s',
  78. implode(', ', $unknownCommands)
  79. ),
  80. 1535189881
  81. );
  82. }
  83. private function assertCommand($command)
  84. {
  85. if (in_array($command, $this->availableCommands, true)) {
  86. return;
  87. }
  88. throw new \LogicException(
  89. sprintf(
  90. 'Unknown command "%s"',
  91. $command
  92. ),
  93. 1535189882
  94. );
  95. }
  96. private function assertAssertionCompleteness()
  97. {
  98. $undefinedAssertions = array_diff(
  99. $this->availableCommands,
  100. array_keys($this->assertions)
  101. );
  102. if (empty($undefinedAssertions)) {
  103. return;
  104. }
  105. throw new \LogicException(
  106. sprintf(
  107. 'Missing assertions for commands: %s',
  108. implode(', ', $undefinedAssertions)
  109. ),
  110. 1535189883
  111. );
  112. }
  113. }