Search code examples
lithium

Loading a lithium library only in certain environments


I have added li3_docs (https://github.com/UnionOfRAD/li3_docs) to my application but I don't want this library to load in production environment. What is the best way to prevent the docs from being available in production environment? Initially I thought of adding this line to my config/bootstrap/libraries.php:

if(!Environment::is('production')) {
   Libraries::add('li3_docs');
}

This doesn't work because Environment class hasn't been loaded yet and I feel like it isn't wise to load this before the libraries loader. So what is the best way to do this?


Solution

  • The hacky way would be add Environment::set(new \lithium\net\http\Request()) before adding libraries.

    Another way:

    1. Add a configuration value when adding the library

      Libraries::add('li3_docs', array('devOnly' => true));
      
    2. Update the default Dispatcher::run filter in app\bootstrap\action.php to something like this

      Dispatcher::applyFilter('run', function($self, $params, $chain) {
        Environment::set($params['request']);
      
        foreach (array_reverse(Libraries::get()) as $name => $config) {
          $devOnly = isset($config['devOnly']) && $config['devOnly'];
          $devOnly = $devOnly && Environment::is('development');
          if ($name === 'lithium' || $devOnly) {
            continue;
          }
          $file = "{$config['path']}/config/routes.php";
          file_exists($file) ? include $file : null;
        }
        return $chain->next($self, $params, $chain);
      });