Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Access grav instance in cli

Started by Muut Archive 10 years ago · 3 replies · 471 views
10 years ago

How can I access the equivalent of $this->grav in a plugin, in a cli script?
I tried self::getGrav(), but I get a weird error if I try to use this object:

Fatal error: Call to a member function get() on a non-object in /Volumes/FILES/mygrav/system/src/Grav/Common/Debugger.php on line 42

10 years ago

I'm using ConsoleTrait now, but the error persists. This line in cache.php seems to be the problem, when I comment it out, the problem goes away:

// Dump Cache state
$grav['debugger']->addMessage('Cache: [' . ($this->enabled ? 'true' : 'false') . '] Driver: [' . $this->driver_name . ']');

My cache is overridden though, I've extended cache.php, this is the code:

namespace Grav\Plugin;

use \Doctrine\Common\Cache\Cache as DoctrineCache;
use Grav\Common\Config\Config;
use Grav\Common\Filesystem\Folder;

/*

*/
class OverrideCache extends \Grav\Common\Cache
{

PHP
/** 
 *
 * Set static variables in parent class cache.php
 *
 */
public function setCacheRemove($caches)
{

    if(!empty($caches)){
      parent::$standard_remove = $caches;
    }
    else{

      parent::$standard_remove = [
          'cache://twig/',
          'cache://doctrine/',
          'cache://compiled/',
          'cache://validated-',
          'asset://',
      ];
    }
}

/**
 *
 * Prevent images from being flushed on any cache flush
 *
 */
public function overrideAllCaches($caches)
{
    if(!empty($caches)){
      parent::$standard_remove =
      parent::$all_remove =
      parent::$assets_remove =
      parent::$images_remove =
      parent::$cache_remove =

      $caches;

    }
    else{

      parent::$standard_remove =
      parent::$all_remove =
      parent::$assets_remove =
      parent::$images_remove =
      parent::$cache_remove =

      [
          'cache://twig/',
          'cache://doctrine/',
          'cache://compiled/',
          'cache://validated-',
          'asset://',
      ];
    }

}

}

Seems to be the get method on the config object in Grav\Common\Debugger.php that spawns this error. So in debugger.php my grav object doesn't seem to have the config object, but when I dump it in my console it's present.

makes no difference how I use getGrav(), same error:
$grav = self::getGrav();
$grav = $this->getGrav();

10 years ago

It would be easier to read your code if you pasted it in a markdown code block (see the ? for help in the input box on the forum)

Anyway this issue is probably because of the order things are initialized within Grav. The cache is initialized and created pretty early on in the order of things.

By the time any plugin is even loaded. Cache has been created with the core Grav one. You may be replacing it, but only after onPluginsInitialized() has run. Before that point, the regular cache will be used.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1355 9 years ago
Archive · by Muut Archive, 9 years ago
2 935 9 years ago
Archive · by Muut Archive, 9 years ago
2 4065 9 years ago
Archive · by Muut Archive, 9 years ago
1 2953 9 years ago
Archive · by Muut Archive, 9 years ago
3 1119 9 years ago