CCodeFile.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * CCodeFile class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CCodeFile represents a code file being generated.
  12. *
  13. * @property string $relativePath The code file path relative to the application base path.
  14. * @property string $type The code file extension (e.g. php, txt).
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @package system.gii
  18. * @since 1.1.2
  19. */
  20. class CCodeFile extends CComponent
  21. {
  22. const OP_NEW='new';
  23. const OP_OVERWRITE='overwrite';
  24. const OP_SKIP='skip';
  25. /**
  26. * @var string the file path that the new code should be saved to.
  27. */
  28. public $path;
  29. /**
  30. * @var mixed the newly generated code. If this is null, it means {@link path}
  31. * should be treated as a directory.
  32. */
  33. public $content;
  34. /**
  35. * @var string the operation to be performed
  36. */
  37. public $operation;
  38. /**
  39. * @var string the error occurred when saving the code into a file
  40. */
  41. public $error;
  42. /**
  43. * Constructor.
  44. * @param string $path the file path that the new code should be saved to.
  45. * @param string $content the newly generated code
  46. */
  47. public function __construct($path,$content)
  48. {
  49. $this->path=strtr($path,array('/'=>DIRECTORY_SEPARATOR,'\\'=>DIRECTORY_SEPARATOR));
  50. $this->content=$content;
  51. if(is_file($path))
  52. $this->operation=file_get_contents($path)===$content ? self::OP_SKIP : self::OP_OVERWRITE;
  53. elseif($content===null) // is dir
  54. $this->operation=is_dir($path) ? self::OP_SKIP : self::OP_NEW;
  55. else
  56. $this->operation=self::OP_NEW;
  57. }
  58. /**
  59. * Saves the code into the file {@link path}.
  60. */
  61. public function save()
  62. {
  63. $module=Yii::app()->controller->module;
  64. if($this->content===null) // a directory
  65. {
  66. if(!is_dir($this->path))
  67. {
  68. $oldmask=@umask(0);
  69. $result=@mkdir($this->path,$module->newDirMode,true);
  70. @umask($oldmask);
  71. if(!$result)
  72. {
  73. $this->error="Unable to create the directory '{$this->path}'.";
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. if($this->operation===self::OP_NEW)
  80. {
  81. $dir=dirname($this->path);
  82. if(!is_dir($dir))
  83. {
  84. $oldmask=@umask(0);
  85. $result=@mkdir($dir,$module->newDirMode,true);
  86. @umask($oldmask);
  87. if(!$result)
  88. {
  89. $this->error="Unable to create the directory '$dir'.";
  90. return false;
  91. }
  92. }
  93. }
  94. if(@file_put_contents($this->path,$this->content)===false)
  95. {
  96. $this->error="Unable to write the file '{$this->path}'.";
  97. return false;
  98. }
  99. else
  100. {
  101. $oldmask=@umask(0);
  102. @chmod($this->path,$module->newFileMode);
  103. @umask($oldmask);
  104. }
  105. return true;
  106. }
  107. /**
  108. * @return string the code file path relative to the application base path.
  109. */
  110. public function getRelativePath()
  111. {
  112. if(strpos($this->path,Yii::app()->basePath)===0)
  113. return substr($this->path,strlen(Yii::app()->basePath)+1);
  114. else
  115. return $this->path;
  116. }
  117. /**
  118. * @return string the code file extension (e.g. php, txt)
  119. */
  120. public function getType()
  121. {
  122. if(($pos=strrpos($this->path,'.'))!==false)
  123. return substr($this->path,$pos+1);
  124. else
  125. return 'unknown';
  126. }
  127. }