PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/php/Arbit/Periodic/Command/FileSystem/Copy.php

https://github.com/Arbitracker/Periodic
PHP | 151 lines | 78 code | 14 blank | 59 comment | 13 complexity | 15aca117cd731470cc33448956a734ec MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Command
  4. *
  5. * This file is part of periodic
  6. *
  7. * periodic is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU Lesser General Public License as published by the Free
  9. * Software Foundation; version 3 of the License.
  10. *
  11. * periodic is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with periodic; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * @package Core
  21. * @version $Revision: 999 $
  22. * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
  23. */
  24. namespace Arbit\Periodic\Command\FileSystem;
  25. use Arbit\Periodic\Command,
  26. Arbit\Periodic\Executor,
  27. Arbit\Periodic\Logger,
  28. Arbit\XML;
  29. /**
  30. * Command
  31. *
  32. * Command to (recursively) copy files.
  33. */
  34. class Copy extends Command
  35. {
  36. /**
  37. * Run command
  38. *
  39. * Execute the actual bits.
  40. *
  41. * Should return one of the status constant values, defined as class
  42. * constants in Executor.
  43. *
  44. * @param XML\Node $configuration
  45. * @param Logger $logger
  46. * @return int
  47. */
  48. public function run( XML\Node $configuration, Logger $logger )
  49. {
  50. if ( !isset( $configuration->src ) )
  51. {
  52. $logger->log( 'No source provided.', Logger::ERROR );
  53. return Executor::ERROR;
  54. }
  55. $src = (string) $configuration->src;
  56. if ( !isset( $configuration->dst ) )
  57. {
  58. $logger->log( 'No destination provided.', Logger::ERROR );
  59. return Executor::ERROR;
  60. }
  61. $dst = (string) $configuration->dst;
  62. $depth = -1;
  63. if ( isset( $configuration->depth ) &&
  64. is_numeric( (string) $configuration->depth ) )
  65. {
  66. $depth = (int) (string) $configuration->depth;
  67. }
  68. $this->copyRecursive( $src, $dst, $depth, $logger );
  69. return Executor::SUCCESS;
  70. }
  71. /**
  72. * Copy files and directories recursively
  73. *
  74. * Copy files and directories recursively. I fone operation fails, a
  75. * warning will be issued and the operation will be continued.
  76. *
  77. * A negative depth means infinite recursion. A depth of 1 means that the
  78. * current files and directories are created, but no recursion is applied.
  79. *
  80. * @param string $src
  81. * @param string $dst
  82. * @param int $depth
  83. * @param Logger $logger
  84. * @return void
  85. */
  86. protected function copyRecursive( $src, $dst, $depth, Logger $logger )
  87. {
  88. if ( $depth == 0 )
  89. {
  90. return;
  91. }
  92. // Check if source file exists at all.
  93. if ( !is_file( $src ) && !is_dir( $src ) )
  94. {
  95. $logger->log( "$src is not a valid source.", Logger::WARNING );
  96. return;
  97. }
  98. // Skip non readable files in src directory
  99. if ( !is_readable( $src ) )
  100. {
  101. $logger->log( "$src is not readable, skipping.", Logger::WARNING );
  102. return;
  103. }
  104. // Destination file should not exist
  105. if ( is_file( $dst ) || is_dir( $dst ) )
  106. {
  107. $logger->log( "$dst already exists, and cannot be overwritten.", Logger::WARNING );
  108. return;
  109. }
  110. // Actually copy
  111. if ( is_dir( $src ) )
  112. {
  113. mkdir( $dst );
  114. }
  115. elseif ( is_file( $src ) )
  116. {
  117. copy( $src, $dst );
  118. return;
  119. }
  120. // Recurse into directory
  121. $dh = opendir( $src );
  122. while ( ( $file = readdir( $dh ) ) !== false )
  123. {
  124. if ( ( $file === '.' ) ||
  125. ( $file === '..' ) )
  126. {
  127. continue;
  128. }
  129. $this->copyRecursive(
  130. $src . '/' . $file,
  131. $dst . '/' . $file,
  132. $depth - 1,
  133. $logger
  134. );
  135. }
  136. }
  137. }