Exemplo n.º 1
0
 /**
  * Includes script in a limited scope.
  * @param  string  file to include
  * @param  array   local variables
  * @return mixed   the return value of the included file
  */
 public static function load()
 {
     if (func_num_args() > 1) {
         self::$vars = func_get_arg(1);
         extract(self::$vars);
     }
     return include func_get_arg(0);
 }
Exemplo n.º 2
0
 /**
  * Handles autoloading of classes or interfaces.
  * @param  string
  * @return void
  */
 public function tryLoad($type)
 {
     $type = ltrim($type, '\\');
     if (isset($this->list[$type])) {
         LimitedScope::load(NETTE_DIR . $this->list[$type] . '.php', TRUE);
         self::$count++;
     }
 }
Exemplo n.º 3
0
 /**
  * Handles autoloading of classes or interfaces.
  * @param  string
  * @return void
  */
 public function tryLoad($type)
 {
     $type = ltrim(strtolower($type), '\\');
     if (isset($this->list[$type])) {
         LimitedScope::load(NETTE_DIR . $this->list[$type]);
         self::$count++;
     }
 }
Exemplo n.º 4
0
 /**
  * Handles autoloading of classes or interfaces.
  * @param  string
  * @return void
  */
 public function tryLoad($type)
 {
     $type = strtolower($type);
     if (isset($this->list[$type])) {
         LimitedScope::load($this->base . $this->list[$type]);
         self::$count++;
     }
 }
Exemplo n.º 5
0
 /**
  * Handles autoloading of classes or interfaces.
  * @param  string
  * @return void
  */
 public function tryLoad($type)
 {
     if (strpbrk($type, './;|') !== FALSE) {
         throw new InvalidArgumentException("Invalid class/interface name '{$type}'.");
     }
     $file = strtr($type, '\\', '/') . '.php';
     /*
     if (strncmp($type, 'Nette\\', 6) === 0) {
     	$file = dirname(dirname(dirname(__FILE__))) . '/' . $file;
     }
     */
     @LimitedScope::load($file);
     self::$count++;
 }
Exemplo n.º 6
0
 /**
  * Renders template to output.
  * @return void
  */
 public function render()
 {
     $cache = new Cache($storage = $this->getCacheStorage(), 'Nette.Template');
     $cached = $compiled = $cache->load($this->source);
     if ($compiled === NULL) {
         $compiled = $this->compile();
         $cache->save($this->source, $compiled, array(Cache::CONSTS => 'Framework::REVISION'));
         $cached = $cache->load($this->source);
     }
     if ($cached !== NULL && $storage instanceof PhpFileStorage) {
         LimitedScope::load($cached['file'], $this->getParameters());
     } else {
         LimitedScope::evaluate($compiled, $this->getParameters());
     }
 }
Exemplo n.º 7
0
 /**
  * Renders template to output.
  * @return void
  */
 public function render()
 {
     $cache = Environment::getCache('StringTemplate');
     $key = md5($this->content);
     $content = $cache[$key];
     if ($content === NULL) {
         // not cached
         if (!$this->getFilters()) {
             $this->onPrepareFilters($this);
         }
         $cache[$key] = $content = $this->compile($this->content);
     }
     $this->__set('template', $this);
     /*Nette\Loaders\*/
     LimitedScope::evaluate($content, $this->getParams());
 }
Exemplo n.º 8
0
 /**
  * @param  string  presenter name
  * @return string  class name
  * @throws InvalidPresenterException
  */
 public function getPresenterClass(&$name)
 {
     if (isset($this->cache[$name])) {
         list($class, $name) = $this->cache[$name];
         return $class;
     }
     if (!is_string($name) || !preg_match("#^[a-zA-Z-ÿ][a-zA-Z0-9-ÿ:]*\$#", $name)) {
         throw new InvalidPresenterException("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)) {
             /*Nette\Loaders\*/
             LimitedScope::load($file);
         }
         if (!class_exists($class)) {
             throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' was not found in '{$file}'.");
         }
     }
     $reflection = new ReflectionClass($class);
     $class = $reflection->getName();
     if (!$reflection->implementsInterface('IPresenter')) {
         throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' is not Nette\\Application\\IPresenter implementor.");
     }
     if ($reflection->isAbstract()) {
         throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' is abstract.");
     }
     // canonicalize presenter name
     $realName = $this->unformatPresenterClass($class);
     if ($name !== $realName) {
         if ($this->caseSensitive) {
             throw new InvalidPresenterException("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;
 }
Exemplo n.º 9
0
 /**
  * Handles autoloading of classes or interfaces.
  * @param  string
  * @return void
  */
 public function tryLoad($type)
 {
     if ($this->list === NULL) {
         $this->list = array();
         // prevents cycling
         $cache = $this->getCache();
         $data = $cache['data'];
         $opt = array($this->scanDirs, $this->ignoreDirs, $this->acceptFiles);
         if ($data['opt'] === $opt) {
             $this->list = $data['list'];
         } else {
             $this->rebuild();
             $cache['data'] = array('list' => $this->list, 'opt' => $opt);
         }
         if (isset($this->list[strtolower(__CLASS__)]) && class_exists('NetteLoader', FALSE)) {
             NetteLoader::getInstance()->unregister();
         }
     }
     $type = strtolower($type);
     if (isset($this->list[$type])) {
         if ($this->list[$type] !== FALSE) {
             LimitedScope::load($this->list[$type]);
             self::$count++;
         }
     } else {
         if ($this->autoRebuild === NULL) {
             $this->autoRebuild = !$this->isProduction();
         }
         if ($this->autoRebuild) {
             if (!$this->rebuilded) {
                 $this->rebuild();
             }
             if (isset($this->list[$type])) {
                 LimitedScope::load($this->list[$type]);
                 self::$count++;
             } else {
                 $this->list[$type] = FALSE;
             }
             $cache = $this->getCache();
             $cache['data'] = array('list' => $this->list, 'opt' => array($this->scanDirs, $this->ignoreDirs, $this->acceptFiles));
         }
     }
 }
Exemplo n.º 10
0
 /**
  * Handles autoloading of classes or interfaces.
  * @param  string
  * @return void
  */
 public function tryLoad($type)
 {
     $type = strtolower($type);
     if (isset($this->list[$type])) {
         if ($this->list[$type] !== FALSE) {
             LimitedScope::load($this->list[$type]);
             self::$count++;
         }
     } else {
         $this->list[$type] = FALSE;
         if ($this->autoRebuild === NULL) {
             $this->autoRebuild = !$this->isProduction();
         }
         if ($this->autoRebuild) {
             $this->rebuild(FALSE);
         }
         if ($this->list[$type] !== FALSE) {
             LimitedScope::load($this->list[$type]);
             self::$count++;
         }
     }
 }
Exemplo n.º 11
0
 /**
  * Reads configuration from PHP file.
  * @param  string  file name
  * @return array
  */
 public function load($file)
 {
     return LimitedScope::load($file);
 }
Exemplo n.º 12
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(Cache::CONSTS => 'Framework::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(Cache::CONSTS => 'Framework::REVISION'));
         }
     }
     if (isset($this->classes[$type]['file'])) {
         LimitedScope::load($this->classes[$type]['file'], TRUE);
         self::$count++;
     } else {
         $this->missing[$type] = TRUE;
     }
 }
Exemplo n.º 13
0
 /**
  * Renders template to output.
  * @return void
  */
 public function render()
 {
     if ($this->file == NULL) {
         // intentionally ==
         throw new InvalidStateException("Template file name was not specified.");
     } elseif (!is_file($this->file) || !is_readable($this->file)) {
         throw new FileNotFoundException("Missing template file '{$this->file}'.");
     }
     $this->__set('template', $this);
     $cache = new Cache($this->getCacheStorage(), 'Nette.Template');
     $key = md5($this->file) . '.' . basename($this->file);
     $cached = $content = $cache[$key];
     if ($content === NULL) {
         if (!$this->getFilters()) {
             $this->onPrepareFilters($this);
         }
         if (!$this->getFilters()) {
             /*Nette\Loaders\*/
             LimitedScope::load($this->file, $this->getParams());
             return;
         }
         try {
             $shortName = $this->file;
             $shortName = str_replace(Environment::getVariable('templatesDir'), "…", $shortName);
         } catch (Exception $foo) {
         }
         $content = $this->compile(file_get_contents($this->file), "file {$shortName}");
         $cache->save($key, $content, array(Cache::FILES => $this->file, Cache::EXPIRE => self::$cacheExpire));
         $cached = $cache[$key];
     }
     if ($cached !== NULL && self::$cacheStorage instanceof TemplateCacheStorage) {
         /*Nette\Loaders\*/
         LimitedScope::load($cached['file'], $this->getParams());
         fclose($cached['handle']);
     } else {
         /*Nette\Loaders\*/
         LimitedScope::evaluate($content, $this->getParams());
     }
 }
Exemplo n.º 14
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 Cache($storage = $this->getCacheStorage(), 'Nette.FileTemplate');
     if ($storage instanceof PhpFileStorage) {
         $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 (TemplateException $e) {
             $e->setSourceFile($this->file);
             throw $e;
         }
         $cache->save($this->file, $compiled, array(Cache::FILES => $this->file, Cache::CONSTS => 'Framework::REVISION'));
         $cached = $cache->load($this->file);
     }
     if ($cached !== NULL && $storage instanceof PhpFileStorage) {
         LimitedScope::load($cached['file'], $this->getParameters());
     } else {
         LimitedScope::evaluate($compiled, $this->getParameters());
     }
 }
Exemplo n.º 15
0
 /**
  * Renders template to output.
  * @return void
  */
 public function render()
 {
     if ($this->file == NULL) {
         // intentionally ==
         throw new InvalidStateException("Template file name was not specified.");
     }
     $this->__set('template', $this);
     $shortName = str_replace(Environment::getVariable('appDir'), '', $this->file);
     $cache = new Cache($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()) {
             LimitedScope::load($this->file, $this->getParams());
             return;
         }
         $content = $this->compile(file_get_contents($this->file), "file …{$shortName}");
         $cache->save($key, $content, array(Cache::FILES => $this->file, Cache::EXPIRE => self::$cacheExpire));
         $cache->release();
         $cached = $cache[$key];
     }
     if ($cached !== NULL && self::$cacheStorage instanceof TemplateCacheStorage) {
         LimitedScope::load($cached['file'], $this->getParams());
         fclose($cached['handle']);
     } else {
         LimitedScope::evaluate($content, $this->getParams());
     }
 }
Exemplo n.º 16
0
 /**
  * Handles autoloading of classes or interfaces.
  * @param  string
  * @return void
  */
 public function tryLoad($type)
 {
     $type = ltrim(strtolower($type), '\\');
     // PHP namespace bug #49143
     if (isset($this->list[$type])) {
         if ($this->list[$type] !== FALSE) {
             LimitedScope::load($this->list[$type][0]);
             self::$count++;
         }
     } else {
         $this->list[$type] = FALSE;
         if ($this->autoRebuild === NULL) {
             $this->autoRebuild = !$this->isProduction();
         }
         if ($this->autoRebuild) {
             if ($this->rebuilded) {
                 $this->getCache()->save($this->getKey(), $this->list);
             } else {
                 $this->rebuild();
             }
         }
         if ($this->list[$type] !== FALSE) {
             LimitedScope::load($this->list[$type][0]);
             self::$count++;
         }
     }
 }
Exemplo n.º 17
0
 /**
  * Renders template to output.
  * @return void
  */
 public function render()
 {
     if ($this->file == NULL) {
         // intentionally ==
         throw new InvalidStateException("Template file name was not specified.");
     } elseif (!is_file($this->file) || !is_readable($this->file)) {
         throw new FileNotFoundException("Missing template file '{$this->file}'.");
     }
     $this->params['template'] = $this;
     if (!count($this->filters)) {
         LimitedScope::load($this->file, $this->params);
         return;
     }
     $cache = new Cache($this->getCacheStorage(), 'Nette.Template');
     $key = md5($this->file) . count($this->filters) . '.' . basename($this->file);
     $cached = $content = $cache[$key];
     if ($content === NULL) {
         $content = file_get_contents($this->file);
         foreach ($this->filters as $filter) {
             if (!is_callable($filter)) {
                 $able = is_callable($filter, TRUE, $textual);
                 throw new InvalidStateException("Filter '{$textual}' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
             }
             // remove PHP code
             $res = '';
             $blocks = array();
             unset($php);
             foreach (token_get_all($content) as $token) {
                 if (is_array($token)) {
                     if ($token[0] === T_INLINE_HTML) {
                         $res .= $token[1];
                         unset($php);
                     } else {
                         if (!isset($php)) {
                             $res .= $php = "@php:p" . count($blocks) . "@";
                             $php =& $blocks[$php];
                         }
                         $php .= $token[1];
                     }
                 } else {
                     $php .= $token;
                 }
             }
             try {
                 $content = call_user_func($filter, $res);
             } catch (Exception $e) {
                 is_callable($filter, TRUE, $textual);
                 throw new InvalidStateException("Filter {$textual}: " . $e->getMessage() . " (in file {$this->file})", 0, $e);
             }
             $content = strtr($content, $blocks);
             // put PHP code back
         }
         $content = "<?php\n// template {$this->file}\n?>{$content}";
         $cache->save($key, $content, array(Cache::FILES => $this->file, Cache::EXPIRE => self::$cacheExpire));
         $cached = $cache[$key];
     }
     if ($cached !== NULL && self::$cacheStorage instanceof TemplateCacheStorage) {
         LimitedScope::load($cached['file'], $this->params);
         fclose($cached['handle']);
     } else {
         LimitedScope::evaluate($content, $this->params);
     }
 }
Exemplo n.º 18
0
 /**
  * Handles autoloading of classes or interfaces.
  * @param  string
  * @return void
  */
 public function tryLoad($type)
 {
     $type = strtolower($type);
     /*$type = ltrim($type, '\\'); // PHP namespace bug #49143 */
     if (isset($this->list[$type])) {
         if ($this->list[$type] !== FALSE) {
             LimitedScope::load($this->list[$type]);
             self::$count++;
         }
     } else {
         $this->list[$type] = FALSE;
         if ($this->autoRebuild === NULL) {
             $this->autoRebuild = !$this->isProduction();
         }
         if ($this->autoRebuild) {
             $this->rebuild(FALSE);
         }
         if ($this->list[$type] !== FALSE) {
             LimitedScope::load($this->list[$type]);
             self::$count++;
         }
     }
 }
Exemplo n.º 19
0
 /**
  * Returns system DI container.
  * @return SystemContainer
  */
 public function createContainer()
 {
     if ($cacheDir = $this->getCacheDirectory()) {
         $cache = new Cache(new PhpFileStorage($cacheDir), 'Nette.Configurator');
         $cacheKey = array($this->parameters, $this->files);
         $cached = $cache->load($cacheKey);
         if (!$cached) {
             $code = $this->buildContainer($dependencies);
             $cache->save($cacheKey, $code, array(Cache::FILES => $dependencies));
             $cached = $cache->load($cacheKey);
         }
         LimitedScope::load($cached['file'], TRUE);
     } elseif ($this->files) {
         throw new InvalidStateException("Set path to temporary directory using setTempDirectory().");
     } else {
         LimitedScope::evaluate($this->buildContainer());
         // back compatibility with Environment
     }
     $container = new $this->parameters['container']['class']();
     $container->initialize();
     Environment::setContext($container);
     // back compatibility
     return $container;
 }