Exemplo n.º 1
0
 /**
  * Creates the Loops Context
  *
  * Values from the passed config object will be used when creating services on the fly (see getService).
  * The configuration should be stored in keys that are identical to service names.
  *
  * The config file will be made accessible as a service under the name 'config'.
  *
  * @param Loops\ArrayObject $serviceconfig The configuration will be used when creating services.
  */
 public function __construct(ArrayObject $config, $debug = TRUE)
 {
     $this->config = $config;
     $this->debug = $debug;
     Loops::$current_loops = $this;
     parent::__construct($this);
     $this->registerService("config", $config);
 }
Exemplo n.º 2
0
 /**
  * A class internal way of iterating over class properties.
  *
  * Wrapper to the AccessTrait getGenerator method.
  *
  * @param bool $include_readonly Include values that have been marked with the {\@}ReadOnly or {\@}ReadWrite annotations.
  * @param bool $include_protected Also include protected values without annotations.
  * @param array Only include values with keys that are specified in this array.
  * @param array Exclude values with keys that are specified in this array.
  * @return Generator A generator that traverses over the requested values of this object.
  */
 protected function getGenerator($include_readonly = FALSE, $include_protected = FALSE, $include = FALSE, $exclude = FALSE)
 {
     foreach (parent::getGenerator($include_readonly, $include_protected, $include, $exclude) as $key => $value) {
         (yield $key => $key === "context" ? $value : $this->initChild($key, $value));
     }
 }
Exemplo n.º 3
0
 /**
  * Setups a Loops application.
  *
  * This constructor will take a Loops context (via the $config parameter) or create a new one.
  *
  * The application will register itself as a service named 'application'.
  * Therefore, the application instance can be quickly accessed in a Loops\Object by $this->application.
  *
  * In Debug mode, changes in the application folder are attempted to be detected and if so, the complete cache (according to service 'cache') and renderer cache files (according to service 'renderer') will be deleted.
  *
  * The application will also speed up autoloading classes by remembering the filenames of class definitions in the cache module.
  * This behaviour can be disabled by setting the config value ->application->enable_cached_autoload to FALSE
  *
  * Include Dir:
  * The value ->application->include_dir is read from the config and defines paths that are consided for autoloading in the PSR-4 format.
  * This value can be an absolute path or relative to $app_dir. If not specified, [ 'inc' ] is used by default which specifies the 'inc' directory insied the application directory.
  *
  * Boot:
  * The value ->application->boot can hold a name of a php file that is executed after application creation.
  * This value can be an absolute path or relative to $app_dir. By default the file 'boot.php' inside the application directory is used.
  * Inside the boot script only the variable $loops is set.
  *
  * @param string $app_dir The application directory
  * @param string $cache_dir The directory for temporary files (as full paths or relative to $appdir)
  * @param string|Loops|Loops\ArrayObject $config A Loops context, a Loops\ArrayObject that is used to create a Loops context or the location of a php file that returns a Loops\ArrayObject or Loops context.
  * @param bool $boot Specifies if the boot script should be executed
  */
 public function __construct($app_dir, $cache_dir = "/tmp", $config = "config.php", $boot = TRUE)
 {
     //setup directories
     $app_dir = realpath($app_dir);
     $cache_dir = Misc::fullPath($cache_dir, $app_dir);
     //make a context from config file
     if (is_string($config)) {
         $config = (include Misc::fullPath($config, $app_dir));
     }
     if ($config instanceof ArrayObject) {
         $loops = new Loops($config, @$config->loops->debug === NULL ? TRUE : (bool) @$config->loops->debug);
     } elseif ($config instanceof Loops) {
         $loops = $config;
     } else {
         throw new Exception("Failed to create Loops Context.");
     }
     //register application service
     $loops->registerService("application", $this);
     $this->app_dir = $app_dir;
     $this->cache_dir = $cache_dir;
     $this->include_dir = (array) (@$loops->config->application->include_dir === NULL ? ["inc"] : $loops->config->application->include_dir);
     $this->boot = @$loops->config->application->boot ?: "boot.php";
     $this->enable_cached_autoload = @$loops->config->application->enable_cached_autoload === NULL ? TRUE : $loops->config->application->enable_cached_autoload;
     $this->enable_cache_flush = @$loops->config->application->enable_cache_flush === NULL ? TRUE : $loops->config->application->enable_cache_flush;
     if (substr($this->boot, 0, 1) != "/") {
         $this->boot = "{$app_dir}/" . $this->boot;
     }
     foreach ($this->include_dir as $key => $include_dir) {
         if (substr($include_dir, 0, 1) != "/") {
             $this->include_dir[$key] = "{$app_dir}/{$include_dir}";
         }
     }
     //register autoload
     foreach ($this->include_dir as $path) {
         spl_autoload_register(function ($classname) use($path) {
             $filename = $path . "/" . str_replace("\\", "/", $classname) . ".php";
             if (!file_exists($filename)) {
                 return;
             }
             require_once $filename;
         });
     }
     if ($this->enable_cached_autoload) {
         $this->setupCachedAutoload($loops);
     }
     //delete cache if files have changed - do this check only in debugmode
     if ($this->enable_cache_flush && $loops->debug) {
         $cache = $loops->getService("cache");
         $renderer = $loops->getService("renderer");
         $dirs = [];
         if ($this->enable_cache_flush & 0x1) {
             $dirs[] = $this->app_dir;
         }
         if ($this->enable_cache_flush & 0x2) {
             $dirs = array_merge($dirs, $this->include_dir);
         }
         if ($this->enable_cache_flush & 0x4) {
             $dirs = array_merge($dirs, $renderer->view_dir);
         }
         if ($file = Misc::lastChange($dirs, $cache, $key)) {
             Misc::recursiveUnlink("{$this->cache_dir}/renderer_cache");
             $cache->flushAll();
             $cache->save($key, $file->getMTime());
         }
     }
     parent::__construct($loops);
     //boot if requested
     if ($boot) {
         $this->boot();
     }
 }
Exemplo n.º 4
0
 public function __construct($level = LogLevel::WARNING, Loops $loops = NULL)
 {
     parent::__construct($loops);
     $this->setLogLevel($level);
 }