Exemple #1
0
 /**
  * Register autoloader.
  * @return void
  */
 public function register()
 {
     $cache = $this->getCache();
     $key = $this->getKey();
     if (isset($cache[$key])) {
         $this->list = $cache[$key];
     } else {
         $this->rebuild();
     }
     if (isset($this->list[strtolower(__CLASS__)]) && class_exists('NetteLoader', FALSE)) {
         NetteLoader::getInstance()->unregister();
     }
     parent::register();
 }
Exemple #2
0
 /**
  * @return RobotLoader
  */
 public static function createRobotLoader($options)
 {
     $loader = new RobotLoader();
     $loader->autoRebuild = !Environment::isProduction();
     //$loader->setCache(Environment::getCache('Nette.RobotLoader'));
     $dirs = isset($options['directory']) ? $options['directory'] : array(Environment::getVariable('appDir'), Environment::getVariable('libsDir'));
     $loader->addDirectory($dirs);
     $loader->register();
     return $loader;
 }
}
if (!is_writable(Environment::getVariable('logDir'))) {
    die("Make directory '" . realpath(Environment::getVariable('logDir')) . "' writable!");
}
/** 2c) Setup Nette\Debug for better exception and error visualisation */
//Debug::$productionMode = $_SERVER['REMOTE_ADDR'] !== '127.0.0.1';  // admin's computer IP
$mode = !Environment::isProduction() && !Environment::getHttpRequest()->isAjax() ? Debug::DEVELOPMENT : Debug::PRODUCTION;
Debug::enable($mode, NULL);
Debug::$strictMode = TRUE;
Debug::$showLocation = TRUE;
/** 2d) enable RobotLoader - this allows load all classes automatically */
$loader = new RobotLoader();
$loader->addDirectory(explode(';', $config->scanDirs));
$loader->autoRebuild = Environment::isProduction() ? FALSE : TRUE;
// rebuild if class is not found?
$loader->register();
/** 2e) load extension methods */
if (is_file(APP_DIR . '/extensions.php')) {
    include_once APP_DIR . '/extensions.php';
}
/** 2f) enable DebugBar */
if ($mode == Debug::DEVELOPMENT) {
    Debug::$showBar = TRUE;
}
/** 2g) Session setup [optional] */
if (Environment::getVariable('sessionDir') !== NULL && !is_writable(Environment::getVariable('sessionDir'))) {
    die("Make directory '" . realpath(Environment::getVariable('sessionDir')) . "' writable!");
}
$session = Environment::getSession();
$session->setSavePath(Environment::getVariable('sessionDir'));
// Step 3: Configure application
 /**
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 public function generateClassCase(InputInterface $input, OutputInterface $output)
 {
     $src = $this->getSrc();
     $dest = $this->getDest();
     $configNeon = $this->getNeonData($dest);
     $config = Arrays::get($configNeon, 'tests', array());
     $configServices = Arrays::get($config, 'services', array());
     $configData = Arrays::get($config, 'case', array());
     $configCount = count($configData);
     if ($configCount == 0) {
         return;
     }
     $this->writeNotice($output, "Generating Class Specific Tests: " . $configCount);
     $robotLoader = new RobotLoader();
     $robotLoader->autoRebuild = TRUE;
     $robotLoader->setCacheStorage(new FileStorage($this->getCacheDirectory()));
     $robotLoader->addDirectory($dest);
     $robotLoader->rebuild();
     $robotLoader->register();
     $autoloaderClasses = $robotLoader->getIndexedClasses();
     $classReflections = array();
     foreach ($autoloaderClasses as $class) {
         $classReflections[] = $class;
     }
     foreach ($configServices as $configName => $configClass) {
         $classPrototype = Arrays::get($configClass, 'class');
         $classType = $classPrototype . "Test";
         $classFilename = Arrays::get($autoloaderClasses, $classType, NULL);
         $classGeneratorProperties = array();
         $classGeneratorMethods = array();
         $classGeneratorConst = array();
         $classGeneratorImplements = array();
         $classDependencies = array();
         if (NULL === $classFilename) {
             $classPrototypeReflection = ReflectionClassType::from($classPrototype);
             $classParent = "\\" . $classPrototypeReflection->getName() . "Base";
             $classPrototypeName = $classPrototypeReflection->getName();
             $classPrototypeGenerator = PhpGeneratorClassType::from($classPrototypeReflection);
             $className = $classPrototypeGenerator->getName() . "Test";
             $classReflection = $classPrototypeReflection;
             $classGenerator = new PhpGeneratorClassType($classReflection);
             $classGenerator->setName($className);
             $classGenerator->setExtends($classParent);
             $classGenerator->setImplements($classGeneratorImplements);
             $classGenerator->setProperties($classGeneratorProperties);
             $classGenerator->setMethods($classGeneratorMethods);
             $classGenerator->setConsts($classGeneratorConst);
             $classGenerator->addProperty("className", "\\" . $configName);
             $classFilename = dirname(str_replace($this->getRootDir(), $this->getDest(), $classReflection->getFileName())) . DIRECTORY_SEPARATOR . $classGenerator->getName() . ".php";
         } else {
             $classReflection = ReflectionClassType::from($classType);
             $classGenerator = PhpGeneratorClassType::from($classReflection);
             $classGeneratorConst = $classGenerator->getConsts();
             $classGeneratorProperties = $classGenerator->getProperties();
             $classGeneratorImplements = $classGenerator->getImplements();
             $classGeneratorMethods = $classGenerator->getMethods();
         }
         if ('interface' == $classGenerator->getType() || $classGenerator->isAbstract()) {
             continue;
         }
         $classGeneratorAnnotation = $classReflection->getAnnotations();
         $classAnnotationsSwitch = Arrays::get($classGeneratorAnnotation, 'no-test', FALSE);
         if (FALSE !== $classAnnotationsSwitch) {
             continue;
         }
         try {
             $instanceProvider = $classReflection->getMethod("getInstance");
         } catch (\Exception $e) {
             $instanceProvider = $classGenerator->addMethod("getInstance");
             $instanceProvider->setBody('return parent::getInstance();');
             $instanceProvider->setDocuments(array('return' => "@return \\" . $classPrototype));
         }
         $classDependency = "";
         foreach ($classDependencies as $dependency => $dependencyClass) {
             $classDependency .= "\nuse " . $dependency;
             if ('' == $dependencyClass) {
                 $classDependency .= " as " . $dependencyClass;
             }
             $classDependency .= ";";
         }
         $classCode = sprintf("<?php\nnamespace %s;\n%s\n%s", $classReflection->getNamespaceName(), $classDependency, $classGenerator);
         $this->saveClass($classCode, new \SplFileInfo($classFilename));
     }
 }