RootDirPlugin.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace cmf\composer;
  3. use Composer\Composer;
  4. use Composer\IO\IOInterface;
  5. use Composer\Plugin\PluginInterface;
  6. class RootDirPlugin implements PluginInterface
  7. {
  8. public function activate(Composer $composer, IOInterface $io)
  9. {
  10. $vendorDir = $composer->getConfig()->get('vendor-dir');
  11. $cmfRootDir = dirname($vendorDir) . DIRECTORY_SEPARATOR;
  12. $rootDir = $vendorDir . DIRECTORY_SEPARATOR . 'thinkcmf' . DIRECTORY_SEPARATOR . 'cmf-root' . DIRECTORY_SEPARATOR . 'root';
  13. if (is_dir($rootDir)) {
  14. echo "copy start\n";
  15. $this->copyDir($rootDir, $cmfRootDir);
  16. echo "copy done\n";
  17. echo $rootDir . "\n";
  18. $this->deleteDir($rootDir);
  19. echo "delete done\n";
  20. }
  21. }
  22. private function copyDir($strSrcDir, $strDstDir)
  23. {
  24. $dir = opendir($strSrcDir);
  25. if (!$dir) {
  26. return false;
  27. }
  28. if (!is_dir($strDstDir)) {
  29. if (!mkdir($strDstDir)) {
  30. return false;
  31. }
  32. }
  33. while (false !== ($file = readdir($dir))) {
  34. echo $file . "\n";
  35. if (($file != '.') && ($file != '..')) {
  36. if (is_dir($strSrcDir . DIRECTORY_SEPARATOR . $file)) {
  37. if (!$this->copyDir($strSrcDir . DIRECTORY_SEPARATOR . $file, $strDstDir . DIRECTORY_SEPARATOR . $file)) {
  38. return false;
  39. }
  40. } else {
  41. if (!copy($strSrcDir . DIRECTORY_SEPARATOR . $file, $strDstDir . DIRECTORY_SEPARATOR . $file)) {
  42. return false;
  43. }
  44. }
  45. }
  46. }
  47. closedir($dir);
  48. return true;
  49. }
  50. private function deleteDir($dir)
  51. {
  52. if (is_dir($dir)) {
  53. if ($dp = opendir($dir)) {
  54. while (($file = readdir($dp)) != false) {
  55. if ($file != '.' && $file != '..') {
  56. $file = $dir . DIRECTORY_SEPARATOR . $file;
  57. if (is_dir($file)) {
  58. echo "deleting dir:" . $file . "\n";
  59. $this->deleteDir($file);
  60. } else {
  61. try {
  62. echo "deleting file:" . $file . "\n";
  63. unlink($file);
  64. } catch (\Exception $e) {
  65. }
  66. }
  67. }
  68. }
  69. if (readdir($dp) == false) {
  70. closedir($dp);
  71. rmdir($dir);
  72. }
  73. } else {
  74. echo 'Not permission' . "\n";
  75. }
  76. }
  77. }
  78. public function deactivate(Composer $composer, IOInterface $io)
  79. {
  80. }
  81. public function uninstall(Composer $composer, IOInterface $io)
  82. {
  83. }
  84. }