FixtureManager.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\SampleData;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\Filesystem\Directory\ReadInterface;
  10. class FixtureManager
  11. {
  12. /**
  13. * @var ComponentRegistrar
  14. */
  15. private $componentRegistrar;
  16. /**
  17. * Modules root directory
  18. *
  19. * @var ReadInterface
  20. */
  21. protected $_modulesDirectory;
  22. /**
  23. * @var \Magento\Framework\Stdlib\StringUtils
  24. */
  25. protected $_string;
  26. /**
  27. * @param ComponentRegistrar $componentRegistrar
  28. * @param \Magento\Framework\Stdlib\StringUtils $string
  29. */
  30. public function __construct(ComponentRegistrar $componentRegistrar, \Magento\Framework\Stdlib\StringUtils $string)
  31. {
  32. $this->componentRegistrar = $componentRegistrar;
  33. $this->_string = $string;
  34. }
  35. /**
  36. * @param string $fileId
  37. * @return string
  38. * @throws \Magento\Framework\Exception\LocalizedException
  39. */
  40. public function getFixture($fileId)
  41. {
  42. list($moduleName, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
  43. $this->normalizePath($fileId)
  44. );
  45. return $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName) . '/' . $filePath;
  46. }
  47. /**
  48. * Remove excessive "." and ".." parts from a path
  49. *
  50. * For example foo/bar/../file.ext -> foo/file.ext
  51. *
  52. * @param string $path
  53. * @return string
  54. */
  55. public static function normalizePath($path)
  56. {
  57. $parts = explode('/', $path);
  58. $result = [];
  59. foreach ($parts as $part) {
  60. if ('..' === $part) {
  61. if (!count($result) || ($result[count($result) - 1] == '..')) {
  62. $result[] = $part;
  63. } else {
  64. array_pop($result);
  65. }
  66. } elseif ('.' !== $part) {
  67. $result[] = $part;
  68. }
  69. }
  70. return implode('/', $result);
  71. }
  72. }