PharExtensionInterceptor.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace TYPO3\PharStreamWrapper\Interceptor;
  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\Assertable;
  13. use TYPO3\PharStreamWrapper\Helper;
  14. use TYPO3\PharStreamWrapper\Exception;
  15. class PharExtensionInterceptor implements Assertable
  16. {
  17. /**
  18. * Determines whether the base file name has a ".phar" suffix.
  19. *
  20. * @param string $path
  21. * @param string $command
  22. * @return bool
  23. * @throws Exception
  24. */
  25. public function assert($path, $command)
  26. {
  27. if ($this->baseFileContainsPharExtension($path)) {
  28. return true;
  29. }
  30. throw new Exception(
  31. sprintf(
  32. 'Unexpected file extension in "%s"',
  33. $path
  34. ),
  35. 1535198703
  36. );
  37. }
  38. /**
  39. * @param string $path
  40. * @return bool
  41. */
  42. private function baseFileContainsPharExtension($path)
  43. {
  44. $baseFile = Helper::determineBaseFile($path);
  45. if ($baseFile === null) {
  46. return false;
  47. }
  48. $fileExtension = pathinfo($baseFile, PATHINFO_EXTENSION);
  49. return strtolower($fileExtension) === 'phar';
  50. }
  51. }