<commit_before><?php

require_once(dmOs::join(sfConfig::get('dm_core_dir'), 'lib/vendor/pChart/pChart/pChart.class.php'));

abstract class dmChart extends pChart
{
  protected static
  $colors = array(
    'grey1' => array(220, 220, 220),
    'grey2' => array(140, 140, 140)
  );

  protected
  $serviceContainer,
  $cacheKey = '',
  $data,
  $name,
  $credentials;

  function dmChart(dmBaseServiceContainer $serviceContainer, array $options = array())
  {
    $this->serviceContainer = $serviceContainer;

    $this->options = array_merge($this->getDefaultOptions(), $options);
    
    parent::pChart($this->getWidth(), $this->getHeight());

    $this->data = $this->getData();

    $this->addToCacheKey(array($this->options, $this->data));

    $this->setup();
  }

  protected function setup()
  {
    $this->setFontProperties("Fonts/tahoma.ttf", 10);
    
    if (sfConfig::get('sf_debug'))
    {
      $reflection = new ReflectionClass(get_class($this));
      $this->addToCacheKey(filemtime($reflection->getFilename()));
    }
  }
  
  protected function choosePalette($number)
  {
    return $this->loadColorPalette(dmOs::join(sfConfig::get('dm_admin_dir'), 'lib/view/chart/palettes/tones-'.$number.'.txt'));
  }

  protected function addToCacheKey($data)
  {
    $this->cacheKey .= serialize($data);
  }

  public function getImage()
  {
    $cacheKey = md5($this->cacheKey);

    $image = sprintf('%s_%s.png', get_class($this), $cacheKey);

    $imageFullPath = dmOs::join(sfConfig::get('sf_cache_dir'), 'web', $image);

    if (!file_exists($imageFullPath))
    {
      if (!$this->serviceContainer->getService('filesystem')->mkdir(dirname($imageFullPath)))
      {
        throw new dmException(sprintf('Can not mkdir %s', dirname($imageFullPath)));
      }
      
      $this->serviceContainer->getService('logger')->notice('Refresh chart '.get_class($this));

      $this->configure();

      $this->render($imageFullPath);
    }

    return dmMediaTag::build('/cache/'.$image);
  }<commit_msg>[Diem] made dmChart base class more robust<commit_after><?php

require_once(dmOs::join(sfConfig::get('dm_core_dir'), 'lib/vendor/pChart/pChart/pChart.class.php'));

abstract class dmChart extends pChart
{
  protected static
  $colors = array(
    'grey1' => array(220, 220, 220),
    'grey2' => array(140, 140, 140)
  );

  protected
  $serviceContainer,
  $cacheKey = '',
  $data,
  $name,
  $credentials;

  function dmChart(dmBaseServiceContainer $serviceContainer, array $options = array())
  {
    $this->serviceContainer = $serviceContainer;

    $this->options = array_merge($this->getDefaultOptions(), $options);
    
    parent::pChart($this->getWidth(), $this->getHeight());

    $this->addToCacheKey($this->options);

    $this->setup();
  }

  protected function setup()
  {
    $this->setFontProperties("Fonts/tahoma.ttf", 10);
    
    if (sfConfig::get('sf_debug'))
    {
      $reflection = new ReflectionClass(get_class($this));
      $this->addToCacheKey(filemtime($reflection->getFilename()));
    }
  }
  
  protected function choosePalette($number)
  {
    return $this->loadColorPalette(dmOs::join(sfConfig::get('dm_admin_dir'), 'lib/view/chart/palettes/tones-'.$number.'.txt'));
  }

  protected function addToCacheKey($data)
  {
    $this->cacheKey .= serialize($data);
  }

  public function getImage()
  {
    $this->data = $this->getData();

    $this->addToCacheKey($this->data);
    
    $cacheKey = md5($this->cacheKey);

    $image = sprintf('%s_%s.png', get_class($this), $cacheKey);

    $imageFullPath = dmOs::join(sfConfig::get('sf_cache_dir'), 'web', $image);

    if (!file_exists($imageFullPath))
    {
      if (!$this->serviceContainer->getService('filesystem')->mkdir(dirname($imageFullPath)))
      {
        throw new dmException(sprintf('Can not mkdir %s', dirname($imageFullPath)));
      }
      
      $this->serviceContainer->getService('logger')->notice('Refresh chart '.get_class($this));

      $this->configure();

      $this->render($imageFullPath);
    }

    return dmMediaTag::build('/cache/'.$image);
  }
{'fraction_non_alphanumeric': 0.18103660034389585, 'fraction_numerical': 0.011299435028248588, 'mean_word_length': 3.9840881272949815, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6264175', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1327, 'n_words': 200}