예제 #1
0
파일: File.php 프로젝트: namesco/Docblox
  public function __construct($file, $validate)
  {
    parent::__construct();

    if (!is_string($file) || (!is_readable($file)))
    {
      throw new DocBlox_Reflection_Exception('The given file should be a string, should exist on the filesystem and should be readable');
    }

    if ($validate)
    {
      exec('php -l '.escapeshellarg($file), $output, $result);
      if ($result != 0)
      {
        throw new DocBlox_Reflection_Exception('The given file could not be interpreted as it contains errors: '.implode(PHP_EOL, $output));
      }
    }

    $this->filename = $file;
    $this->name = $this->filename;
    $contents = file_get_contents($file);

    // detect encoding and transform to UTF-8
    $info = new finfo();
    $mime = $info->file($file, FILEINFO_MIME);
    $mime_info = explode('=', $mime);
    if (strtolower($mime_info[1]) != 'utf-8')
    {
        $contents = iconv($mime_info[1], 'UTF-8', $contents);
    }

    $this->contents = $contents;
    $this->setHash(filemtime($file));
  }
예제 #2
0
 /**
  * Opens the file and retrieves its contents.
  *
  * During construction the given file is checked whether it is readable and
  * if the $validate argument is true a PHP Lint action is executed to
  * check whether the there are no parse errors.
  *
  * By default the Lint check is disable because of the performance hit
  * introduced by this action.
  *
  * If the validation checks out the file's contents are read; converted to
  * UTF-8 and the has is created from those contents.
  *
  * @param string  $file     Name of the file.
  * @param boolean $validate Whether to check the file using PHP Lint.
  *
  * @throws DocBlox_Reflection_Exception when the filename is incorrect or
  *   the file can not be opened
  *
  * @return void
  */
 public function __construct($file, $validate = false)
 {
     parent::__construct();
     if (!is_string($file) || !is_readable($file)) {
         throw new DocBlox_Reflection_Exception('The given file should be a string, should exist on the ' . 'filesystem and should be readable');
     }
     if ($validate) {
         exec('php -l ' . escapeshellarg($file), $output, $result);
         if ($result != 0) {
             throw new DocBlox_Reflection_Exception('The given file could not be interpreted as it contains ' . 'errors: ' . implode(PHP_EOL, $output));
         }
     }
     $this->setFilename($file);
     $this->name = $this->filename;
     $this->contents = $this->convertToUtf8($file, file_get_contents($file));
     // filemtime($file) is sometimes between 0.00001 and 0.00005 seconds
     // faster but md5 is more accurate
     // real world tests with larger code bases should determine how much
     // it really matters
     $this->setHash(md5($this->contents));
 }