PhpStorageFactory.php 1.7 KB

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