PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MagentoHackathon/Composer/Magento/Deploystrategy/Copy.php

https://github.com/diglin/magento-composer-installer
PHP | 116 lines | 74 code | 15 blank | 27 comment | 22 complexity | f66cec3610e96a89d0340ec36321466d MD5 | raw file
  1. <?php
  2. /**
  3. * Composer Magento Installer
  4. */
  5. namespace MagentoHackathon\Composer\Magento\Deploystrategy;
  6. /**
  7. * Symlink deploy strategy
  8. */
  9. class Copy extends DeploystrategyAbstract
  10. {
  11. /**
  12. * copy files
  13. *
  14. * @param string $source
  15. * @param string $dest
  16. * @return bool
  17. * @throws \ErrorException
  18. */
  19. public function createDelegate($source, $dest)
  20. {
  21. list($mapSource, $mapDest) = $this->getCurrentMapping();
  22. $mapSource = $this->removeTrailingSlash($mapSource);
  23. $mapDest = $this->removeTrailingSlash($mapDest);
  24. $cleanDest = $this->removeTrailingSlash($dest);
  25. $sourcePath = $this->getSourceDir() . '/' . $this->removeTrailingSlash($source);
  26. $destPath = $this->getDestDir() . '/' . $this->removeTrailingSlash($dest);
  27. // Create all directories up to one below the target if they don't exist
  28. $destDir = dirname($destPath);
  29. if (!file_exists($destDir)) {
  30. mkdir($destDir, 0777, true);
  31. }
  32. // Handle source to dir copy,
  33. // e.g. Namespace_Module.csv => app/locale/de_DE/
  34. // Namespace/ModuleDir => Namespace/
  35. // Namespace/ModuleDir => Namespace/, but Namespace/ModuleDir may exist
  36. // Namespace/ModuleDir => Namespace/ModuleDir, but ModuleDir may exist
  37. // first iteration through, we need to update the mappings to correctly handle mismatch globs
  38. if ($mapSource == $this->removeTrailingSlash($source) && $mapDest == $this->removeTrailingSlash($dest)) {
  39. if (basename($sourcePath) !== basename($destPath)) {
  40. $this->setCurrentMapping(array($mapSource, $mapDest . '/' . basename($source)));
  41. $cleanDest = $cleanDest . '/' . basename($source);
  42. }
  43. }
  44. if (file_exists($destPath) && is_dir($destPath)) {
  45. $mapSource = rtrim($mapSource,'*');
  46. if (strcmp(substr($cleanDest, strlen($mapDest)+1), substr($source, strlen($mapSource)+1)) === 0) {
  47. // copy each child of $sourcePath into $destPath
  48. foreach (new \DirectoryIterator($sourcePath) as $item) {
  49. $item = (string) $item;
  50. if (!strcmp($item, '.') || !strcmp($item, '..')) {
  51. continue;
  52. }
  53. $childSource = $this->removeTrailingSlash($source) . '/' . $item;
  54. $this->create($childSource, substr($destPath, strlen($this->getDestDir())+1));
  55. }
  56. return true;
  57. } else {
  58. $destPath = $this->removeTrailingSlash($destPath) . '/' . basename($source);
  59. return $this->create($source, substr($destPath, strlen($this->getDestDir())+1));
  60. }
  61. }
  62. // From now on $destPath can't be a directory, that case is already handled
  63. // If file exists and force is not specified, throw exception unless FORCE is set
  64. if (file_exists($destPath)) {
  65. if ($this->isForced()) {
  66. unlink($destPath);
  67. } else {
  68. throw new \ErrorException("Target $dest already exists (set extra.magento-force to override)");
  69. }
  70. }
  71. // File to file
  72. if (!is_dir($sourcePath)) {
  73. if (is_dir($destPath)) {
  74. $destPath .= '/' . basename($sourcePath);
  75. }
  76. return copy($sourcePath, $destPath);
  77. }
  78. // Copy dir to dir
  79. // First create destination folder if it doesn't exist
  80. if (file_exists($destPath)) {
  81. $destPath .= '/' . basename($sourcePath);
  82. }
  83. mkdir($destPath, 0777, true);
  84. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($sourcePath),
  85. \RecursiveIteratorIterator::SELF_FIRST);
  86. foreach ($iterator as $item) {
  87. $subDestPath = $destPath . '/' . $iterator->getSubPathName();
  88. if ($item->isDir()) {
  89. if (! file_exists($subDestPath)) {
  90. mkdir($subDestPath, 0777, true);
  91. }
  92. } else {
  93. copy($item, $subDestPath);
  94. }
  95. if (!is_readable($subDestPath)) {
  96. throw new \ErrorException("Could not create $subDestPath");
  97. }
  98. }
  99. return true;
  100. }
  101. }