예제 #1
0
 /**
  * Build and deploy the index
  *
  * @return void
  * @throws AutoloaderException_Builder_NoClassPath
  * @throws AutoloaderException_Builder_NoDeployPath
  * @throws AutoloaderException_Builder_IO
  */
 public function build()
 {
     if (empty($this->_deployPath)) {
         throw new AutoloaderException_Builder_NoDeployPath($this->_locale->sprintf("NO_DEPLOY_PATH"));
     }
     if (empty($this->_classPaths)) {
         throw new AutoloaderException_Builder_NoClassPath($this->_locale->sprintf("NO_CLASS_PATH"));
     }
     $indexDirectory = $this->_deployPath . DIRECTORY_SEPARATOR . "index";
     // Create directories
     $this->_mkdir($this->_deployPath);
     $this->_mkdir($indexDirectory);
     // Build indexes
     foreach ($this->_classPaths as $i => $classPath) {
         $autoloader = new Autoloader($classPath);
         // Setup the deployed index
         $indexFile = "{$i}.php";
         $index = new AutoloaderIndex_PHPArrayCode();
         $index->setIndexPath($indexDirectory . DIRECTORY_SEPARATOR . $indexFile);
         $index->addFilter(new AutoloaderIndexFilter_RelativePath($this->_deployPath));
         // Don't index our own classes
         $autoloader->getFileIterator()->addSkipPattern("/InstantAutoloader\\.php\$/");
         // Build index
         $autoloader->setIndex($index);
         $autoloader->buildIndex();
     }
     // Copy InstantAutoloader
     $isCopied = copy(__DIR__ . DIRECTORY_SEPARATOR . "InstantAutoloader.php", $this->_deployPath . DIRECTORY_SEPARATOR . "InstantAutoloader.php");
     if (!$isCopied) {
         throw new AutoloaderException_Builder_IO($this->_locale->sprintf("FAILED_COPY_INSTANTAUTOLOADER"));
     }
     // Code for autoloading
     $autoloadMethod = $this->_classLoaderMode == self::MODE_AUTOLOAD ? "register" : "requireAll";
     $code = "<?php\n\n" . "/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */\n\n" . "/**\n" . " * Autoloader\n" . " * \n" . " * This code was generated automatically.\n" . " * Don't edit this file. Changes will get lost when\n" . " * building a new autoloader.\n" . " *\n" . " * @see  AutoloaderBuilder::build()\n" . " * @link http://php-autoloader.malkusch.de/en/\n" . " */\n\n" . "namespace " . __NAMESPACE__ . ";\n\n" . "require_once __DIR__ . '/InstantAutoloader.php';\n";
     foreach ($this->_classPaths as $i => $classPath) {
         $indexPath = "__DIR__ . '/index/{$i}.php'";
         $code .= "\n\$_autoloader = new InstantAutoloader({$indexPath});\n" . "\$_autoloader->setBasePath(__DIR__);\n" . "\$_autoloader->{$autoloadMethod}();\n";
     }
     $code .= "unset(\$_autoloader);";
     $isPut = @\file_put_contents($this->getAutoloaderFile(), $code);
     if (!$isPut) {
         $error = \error_get_last();
         throw new AutoloaderException_Builder_IO($this->_locale->sprintf("FAILED_GENERATING_CODE", $error["message"]));
     }
     \chmod($this->getAutoloaderFile(), 0644);
 }