[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/console/core/ -> DarkConsoleCore.php (source)

   1  <?php
   2  
   3  final class DarkConsoleCore {
   4  
   5    private $plugins = array();
   6    const STORAGE_VERSION = 1;
   7  
   8    public function __construct() {
   9      $symbols = id(new PhutilSymbolLoader())
  10        ->setType('class')
  11        ->setAncestorClass('DarkConsolePlugin')
  12        ->selectAndLoadSymbols();
  13  
  14      foreach ($symbols as $symbol) {
  15        $plugin = newv($symbol['name'], array());
  16        if (!$plugin->shouldStartup()) {
  17          continue;
  18        }
  19        $plugin->setConsoleCore($this);
  20        $plugin->didStartup();
  21        $this->plugins[$symbol['name']] = $plugin;
  22      }
  23    }
  24  
  25    public function getPlugins() {
  26      return $this->plugins;
  27    }
  28  
  29    public function getKey(AphrontRequest $request) {
  30      $plugins = $this->getPlugins();
  31  
  32      foreach ($plugins as $plugin) {
  33        $plugin->setRequest($request);
  34        $plugin->willShutdown();
  35      }
  36  
  37      foreach ($plugins as $plugin) {
  38        $plugin->didShutdown();
  39      }
  40  
  41      foreach ($plugins as $plugin) {
  42        $plugin->setData($plugin->generateData());
  43      }
  44  
  45      $plugins = msort($plugins, 'getOrderKey');
  46  
  47      $key = Filesystem::readRandomCharacters(24);
  48  
  49      $tabs = array();
  50      $data = array();
  51      foreach ($plugins as $plugin) {
  52        $class = get_class($plugin);
  53        $tabs[] = array(
  54          'class' => $class,
  55          'name'  => $plugin->getName(),
  56          'color' => $plugin->getColor(),
  57        );
  58        $data[$class] = $this->sanitizeForJSON($plugin->getData());
  59      }
  60  
  61      $storage = array(
  62        'vers' => self::STORAGE_VERSION,
  63        'tabs' => $tabs,
  64        'data' => $data,
  65        'user' => $request->getUser()
  66          ? $request->getUser()->getPHID()
  67          : null,
  68      );
  69  
  70      $cache = new PhabricatorKeyValueDatabaseCache();
  71      $cache = new PhutilKeyValueCacheProfiler($cache);
  72      $cache->setProfiler(PhutilServiceProfiler::getInstance());
  73  
  74      // This encoding may fail if there are, e.g., database queries which
  75      // include binary data. It would be a little cleaner to try to strip these,
  76      // but just do something non-broken here if we end up with unrepresentable
  77      // data.
  78      $json = @json_encode($storage);
  79      if (!$json) {
  80        $json = '{}';
  81      }
  82  
  83      $cache->setKeys(
  84        array(
  85          'darkconsole:'.$key => $json,
  86        ),
  87        $ttl = (60 * 60 * 6));
  88  
  89      return $key;
  90    }
  91  
  92    public function getColor() {
  93      foreach ($this->getPlugins() as $plugin) {
  94        if ($plugin->getColor()) {
  95          return $plugin->getColor();
  96        }
  97      }
  98    }
  99  
 100    public function render(AphrontRequest $request) {
 101      $user = $request->getUser();
 102      $visible = $user ? $user->getConsoleVisible() : true;
 103  
 104      return javelin_tag(
 105        'div',
 106        array(
 107          'id' => 'darkconsole',
 108          'class' => 'dark-console',
 109          'style' => $visible ? '' : 'display: none;',
 110          'data-console-key' => $this->getKey($request),
 111          'data-console-color' => $this->getColor(),
 112        ),
 113        '');
 114    }
 115  
 116    /**
 117     * Sometimes, tab data includes binary information (like INSERT queries which
 118     * write file data into the database). To successfully JSON encode it, we
 119     * need to convert it to UTF-8.
 120     */
 121    private function sanitizeForJSON($data) {
 122      if (is_object($data)) {
 123        return '<object:'.get_class($data).'>';
 124      } else if (is_array($data)) {
 125        foreach ($data as $key => $value) {
 126          $data[$key] = $this->sanitizeForJSON($value);
 127        }
 128        return $data;
 129      } else {
 130        return phutil_utf8ize($data);
 131      }
 132    }
 133  
 134  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1