Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / PhpStorage / PhpStorageFactory.php
1 <?php
2
3 namespace Drupal\Core\PhpStorage;
4
5 use Drupal\Core\Site\Settings;
6 use Drupal\Core\StreamWrapper\PublicStream;
7
8 /**
9  * Creates a php storage object
10  */
11 class PhpStorageFactory {
12
13   /**
14    * Instantiates a storage for generated PHP code.
15    *
16    * By default, this returns an instance of the
17    * \Drupal\Component\PhpStorage\MTimeProtectedFileStorage class.
18    *
19    * Classes implementing
20    * \Drupal\Component\PhpStorage\PhpStorageInterface can be registered for a
21    * specific bin or as a default implementation.
22    *
23    * @param string $name
24    *   The name for which the storage should be returned. Defaults to 'default'
25    *   The name is also used as the storage bin if one is not specified in the
26    *   configuration.
27    *
28    * @return \Drupal\Component\PhpStorage\PhpStorageInterface
29    *   An instantiated storage for the specified name.
30    */
31   public static function get($name) {
32     $configuration = [];
33     $overrides = Settings::get('php_storage');
34     if (isset($overrides[$name])) {
35       $configuration = $overrides[$name];
36     }
37     elseif (isset($overrides['default'])) {
38       $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     if (!isset($configuration['secret'])) {
43       $configuration['secret'] = Settings::getHashSalt();
44     }
45     if (!isset($configuration['bin'])) {
46       $configuration['bin'] = $name;
47     }
48     if (!isset($configuration['directory'])) {
49       $configuration['directory'] = PublicStream::basePath() . '/php';
50     }
51     return new $class($configuration);
52   }
53
54 }