Example #1
0
	/**
	 * Includes script in a limited scope.
	 * @param  string  file to include
	 * @param  array   local variables or TRUE meaning include once
	 * @return mixed   the return value of the included file
	 */
	public static function load(/*$file, array $vars = NULL*/)
	{
		if (func_num_args() > 1) {
			self::$vars = func_get_arg(1);
			if (self::$vars === TRUE) {
				return include_once func_get_arg(0);
			}
			extract(self::$vars);
		}
		return include func_get_arg(0);
	}
	/**
	 * @param  string  presenter name
	 * @return string  class name
	 * @throws NInvalidPresenterException
	 */
	public function getPresenterClass(& $name)
	{
		if (isset($this->cache[$name])) {
			list($class, $name) = $this->cache[$name];
			return $class;
		}

		if (!is_string($name) || !NStrings::match($name, "#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#")) {
			throw new NInvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid.");
		}

		$class = $this->formatPresenterClass($name);

		if (!class_exists($class)) {
			// internal autoloading
			$file = $this->formatPresenterFile($name);
			if (is_file($file) && is_readable($file)) {
				NLimitedScope::load($file, TRUE);
			}

			if (!class_exists($class)) {
				throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'.");
			}
		}

		$reflection = new NClassReflection($class);
		$class = $reflection->getName();

		if (!$reflection->implementsInterface('IPresenter')) {
			throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is not IPresenter implementor.");
		}

		if ($reflection->isAbstract()) {
			throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
		}

		// canonicalize presenter name
		$realName = $this->unformatPresenterClass($class);
		if ($name !== $realName) {
			if ($this->caseSensitive) {
				throw new NInvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'.");
			} else {
				$this->cache[$name] = array($class, $realName);
				$name = $realName;
			}
		} else {
			$this->cache[$name] = array($class, $realName);
		}

		return $class;
	}
Example #3
0
	/**
	 * Handles autoloading of classes or interfaces.
	 * @param  string
	 * @return void
	 */
	public function tryLoad($type)
	{
		$type = ltrim($type, '\\');
		if (isset($this->list[$type])) {
			NLimitedScope::load(NETTE_DIR . $this->list[$type] . '.php', TRUE);
			self::$count++;

		}}
Example #4
0
 function render()
 {
     if ($this->getFile() == NULL) {
         throw new InvalidStateException("Template file name was not specified.");
     }
     $cache = new NCache($storage = $this->getCacheStorage(), 'wplatte');
     if ($storage instanceof NPhpFileStorage) {
         $storage->hint = str_replace(dirname(dirname($this->getFile())), '', $this->getFile());
     }
     $cached = $compiled = $cache->load($this->getFile());
     if ($compiled === NULL) {
         try {
             $compiled = "<?php\n\n// source file: {$this->getFile()}\n\n?>" . $this->compile();
         } catch (NTemplateException $e) {
             $e->setSourceFile($this->getFile());
             throw $e;
         }
         $cache->save($this->getFile(), $compiled, array(NCache::FILES => $this->getFile(), NCache::CONSTS => array('NFramework::REVISION', 'WPLATTE_CACHE_VERSION')));
         $cache->release();
         $cached = $cache->load($this->getFile());
     }
     if ($cached !== NULL && $storage instanceof NPhpFileStorage) {
         NLimitedScope::load($cached['file'], $this->getParams());
     } else {
         NLimitedScope::evaluate($compiled, $this->getParams());
     }
 }
Example #5
0
	/**
	 * Reads configuration from PHP file.
	 * @param  string  file name
	 * @return array
	 */
	public function load($file)
	{
		return NLimitedScope::load($file);
	}
Example #6
0
 function render()
 {
     if ($this->file == NULL) {
         throw new InvalidStateException("Template file name was not specified.");
     }
     $this->__set('template', $this);
     $cache = new NCache($storage = $this->getCacheStorage(), 'Nette.FileTemplate');
     if ($storage instanceof NTemplateCacheStorage) {
         $storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file);
     }
     $cached = $content = $cache[$this->file];
     if ($content === NULL) {
         try {
             $content = $this->compile(file_get_contents($this->file));
             $content = "<?php\n\n// source file: {$this->file}\n\n?>{$content}";
         } catch (NTemplateException $e) {
             $e->setSourceFile($this->file);
             throw $e;
         }
         $cache->save($this->file, $content, array(NCache::FILES => $this->file, NCache::EXPIRATION => self::$cacheExpire, NCache::CONSTS => 'NFramework::REVISION'));
         $cache->release();
         $cached = $cache[$this->file];
     }
     if ($cached !== NULL && $storage instanceof NTemplateCacheStorage) {
         NLimitedScope::load($cached['file'], $this->getParams());
         fclose($cached['handle']);
     } else {
         NLimitedScope::evaluate($content, $this->getParams());
     }
 }
Example #7
0
	/**
	 * Renders template to output.
	 * @return void
	 */
	public function render()
	{
		$cache = new NCache($storage = $this->getCacheStorage(), 'Nette.Template');
		$cached = $compiled = $cache->load($this->source);

		if ($compiled === NULL) {
			$compiled = $this->compile();
			$cache->save($this->source, $compiled, array(NCache::CONSTS => 'NFramework::REVISION'));
			$cached = $cache->load($this->source);
		}

		if ($cached !== NULL && $storage instanceof NPhpFileStorage) {
			NLimitedScope::load($cached['file'], $this->getParameters());
		} else {
			NLimitedScope::evaluate($compiled, $this->getParameters());
		}
	}
Example #8
0
	/**
	 * Returns system DI container.
	 * @return SystemContainer
	 */
	public function createContainer()
	{
		if ($cacheDir = $this->getCacheDirectory()) {
			$cache = new NCache(new NPhpFileStorage($cacheDir), 'Nette.Configurator');
			$cacheKey = array($this->params, $this->files);
			$cached = $cache->load($cacheKey);
			if (!$cached) {
				$code = $this->buildContainer($dependencies);
				$cache->save($cacheKey, $code, array(
					NCache::FILES => $this->params['productionMode'] ? NULL : $dependencies,
				));
				$cached = $cache->load($cacheKey);
			}
			NLimitedScope::load($cached['file'], TRUE);

		} elseif ($this->files) {
			throw new InvalidStateException("Set path to temporary directory using setTempDirectory().");

		} else {
			NLimitedScope::evaluate($this->buildContainer()); // back compatibility with Environment
		}

		$container = new $this->params['container']['class'];
		$container->initialize();
		NEnvironment::setContext($container); // back compatibility
		return $container;
	}
Example #9
0
	/**
	 * Handles autoloading of classes, interfaces or traits.
	 * @param  string
	 * @return void
	 */
	public function tryLoad($type)
	{
		$type = ltrim(strtolower($type), '\\'); // PHP namespace bug #49143
		$info = & $this->list[$type];

		if ($this->autoRebuild && empty($this->checked[$type]) && (is_array($info) ? !is_file($info[0]) : $info < self::RETRY_LIMIT)) {
			$info = is_int($info) ? $info + 1 : 0;
			$this->checked[$type] = TRUE;
			if ($this->rebuilt) {
				$this->getCache()->save($this->getKey(), $this->list, array(
					NCache::CONSTS => 'NFramework::REVISION',
				));
			} else {
				$this->rebuild();
			}
		}

		if (isset($info[0])) {
			NLimitedScope::load($info[0], TRUE);

			if ($this->autoRebuild && !class_exists($type, FALSE) && !interface_exists($type, FALSE) && (PHP_VERSION_ID < 50400 || !trait_exists($type, FALSE))) {
				$info = 0;
				$this->checked[$type] = TRUE;
				if ($this->rebuilt) {
					$this->getCache()->save($this->getKey(), $this->list, array(
						NCache::CONSTS => 'NFramework::REVISION',
					));
				} else {
					$this->rebuild();
				}
			}
			self::$count++;
		}
	}
Example #10
0
	/**
	 * Renders template to output.
	 * @return void
	 */
	public function render()
	{
		if ($this->file == NULL) { // intentionally ==
			throw new InvalidStateException("Template file name was not specified.");
		}

		$cache = new NCache($storage = $this->getCacheStorage(), 'Nette.FileTemplate');
		if ($storage instanceof NPhpFileStorage) {
			$storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file);
		}
		$cached = $compiled = $cache->load($this->file);

		if ($compiled === NULL) {
			try {
				$compiled = "<?php\n\n// source file: $this->file\n\n?>" . $this->compile();

			} catch (NTemplateException $e) {
				$e->setSourceFile($this->file);
				throw $e;
			}

			$cache->save($this->file, $compiled, array(
				NCache::FILES => $this->file,
				NCache::CONSTS => 'NFramework::REVISION',
			));
			$cached = $cache->load($this->file);
		}

		if ($cached !== NULL && $storage instanceof NPhpFileStorage) {
			NLimitedScope::load($cached['file'], $this->getParameters());
		} else {
			NLimitedScope::evaluate($compiled, $this->getParameters());
		}
	}
Example #11
0
	/**
	 * Handles autoloading of classes, interfaces or traits.
	 * @param  string
	 * @return void
	 */
	public function tryLoad($type)
	{
		$type = ltrim(strtolower($type), '\\'); // PHP namespace bug #49143

		$info = & $this->classes[$type];
		if (isset($this->missing[$type]) || (is_int($info) && $info >= self::RETRY_LIMIT)) {
			return;
		}

		if ($this->autoRebuild) {
			if (!is_array($info) || !is_file($info['file'])) {
				$info = is_int($info) ? $info + 1 : 0;
				if ($this->rebuilt) {
					$this->getCache()->save($this->getKey(), $this->classes, array(
						NCache::CONSTS => 'NFramework::REVISION',
					));
				} else {
					$this->rebuild();
				}
			} elseif (!$this->rebuilt && filemtime($info['file']) !== $info['time']) {
				$this->updateFile($info['file']);
				if (!isset($this->classes[$type])) {
					$this->classes[$type] = 0;
				}
				$this->getCache()->save($this->getKey(), $this->classes, array(
					NCache::CONSTS => 'NFramework::REVISION',
				));
			}
		}

		if (isset($this->classes[$type]['file'])) {
			NLimitedScope::load($this->classes[$type]['file'], TRUE);
			self::$count++;
		} else {
			$this->missing[$type] = TRUE;
		}
	}
Example #12
0
File: loader.php Project: GE3/GE3
 function render()
 {
     if ($this->file == NULL) {
         throw new InvalidStateException("Template file name was not specified.");
     }
     $this->__set('template', $this);
     $shortName = str_replace(NEnvironment::getVariable('appDir'), '', $this->file);
     $cache = new NCache($this->getCacheStorage(), 'Nette.Template');
     $key = trim(strtr($shortName, '\\/@', '.._'), '.') . '-' . md5($this->file);
     $cached = $content = $cache[$key];
     if ($content === NULL) {
         if (!$this->getFilters()) {
             $this->onPrepareFilters($this);
         }
         if (!$this->getFilters()) {
             NLimitedScope::load($this->file, $this->getParams());
             return;
         }
         $content = $this->compile(file_get_contents($this->file), "file …{$shortName}");
         $cache->save($key, $content, array(NCache::FILES => $this->file, NCache::EXPIRE => self::$cacheExpire));
         $cache->release();
         $cached = $cache[$key];
     }
     if ($cached !== NULL && self::$cacheStorage instanceof NTemplateCacheStorage) {
         NLimitedScope::load($cached['file'], $this->getParams());
         fclose($cached['handle']);
     } else {
         NLimitedScope::evaluate($content, $this->getParams());
     }
 }