PhpStorageFactory.php 1.67 KB
Newer Older
1 2
<?php

3
namespace Drupal\Core\PhpStorage;
4

5
use Drupal\Core\Site\Settings;
6
use Drupal\Core\StreamWrapper\PublicStream;
7 8 9 10 11 12 13

/**
 * Creates a php storage object
 */
class PhpStorageFactory {

  /**
14
   * Instantiates a storage for generated PHP code.
15 16 17 18 19 20 21 22 23
   *
   * By default, this returns an instance of the
   * \Drupal\Component\PhpStorage\MTimeProtectedFileStorage class.
   *
   * Classes implementing
   * \Drupal\Component\PhpStorage\PhpStorageInterface can be registered for a
   * specific bin or as a default implementation.
   *
   * @param string $name
24 25 26
   *   The name for which the storage should be returned. Defaults to 'default'
   *   The name is also used as the storage bin if one is not specified in the
   *   configuration.
27 28
   *
   * @return \Drupal\Component\PhpStorage\PhpStorageInterface
29
   *   An instantiated storage for the specified name.
30 31
   */
  static function get($name) {
32
    $configuration = array();
33
    $overrides = Settings::get('php_storage');
34 35
    if (isset($overrides[$name])) {
      $configuration = $overrides[$name];
36
    }
37 38
    elseif (isset($overrides['default'])) {
      $configuration = $overrides['default'];
39
    }
40
    // Make sure all the necessary configuration values are set.
41
    $class = isset($configuration['class']) ? $configuration['class'] : 'Drupal\Component\PhpStorage\MTimeProtectedFileStorage';
42 43 44
    if (!isset($configuration['secret'])) {
      $configuration['secret'] = Settings::getHashSalt();
    }
45 46 47 48
    if (!isset($configuration['bin'])) {
      $configuration['bin'] = $name;
    }
    if (!isset($configuration['directory'])) {
49
      $configuration['directory'] = PublicStream::basePath() . '/php';
50 51 52 53 54
    }
    return new $class($configuration);
  }

}