/www/framework/gii/CCodeFile.php

https://bitbucket.org/kananina/testsite2 · PHP · 135 lines · 82 code · 7 blank · 46 comment · 12 complexity · 3c29ee310feacb1ad62743686b5e4c51 MD5 · raw file

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