You are here

class RequireOnce in Ludwig 8

Provides service for ludwig require_once calls.

'classmap' and 'files' libraries are not supported by Ludwig automatically. Contrib modules can take advantage of this Ludwig service to require_once necessary files inside their scripts (.module and/or .install files usually).

Hierarchy

Expanded class hierarchy of RequireOnce

1 string reference to 'RequireOnce'
ludwig.services.yml in ./ludwig.services.yml
ludwig.services.yml
1 service uses RequireOnce
ludwig.require_once in ./ludwig.services.yml
Drupal\ludwig\RequireOnce

File

src/RequireOnce.php, line 16

Namespace

Drupal\ludwig
View source
class RequireOnce {
  use StringTranslationTrait;

  /**
   * The helper function for Ludwig integration.
   *
   * @param string $package_name
   *   The package name.
   * @param string $file_to_require
   *   The file to require.
   * @param string $dir_name
   *   The caller module directory name.
   */
  public function requireOnce($package_name, $file_to_require, $dir_name) {
    $ludwig_json = $dir_name . '/ludwig.json';
    if (file_exists($ludwig_json)) {
      $packages = file_get_contents($ludwig_json);
      $packages = json_decode($packages, TRUE);
    }
    else {
      throw new \Exception(sprintf('File not found: %s.', $ludwig_json));
    }
    if (!empty($packages['require'][$package_name]['version'])) {
      $version = $packages['require'][$package_name]['version'];
    }
    else {
      throw new \Exception(sprintf('The %s library "version" argument in %s file is empty.', $package_name, $ludwig_json));
    }
    $require = $dir_name . '/lib/' . str_replace('/', '-', $package_name) . '/' . $version . '/' . $file_to_require;
    if (file_exists($require)) {
      require_once $require;
    }
    else {
      \Drupal::logger('ludwig')
        ->error($this
        ->t('File not found: @require.', [
        '@require' => $require,
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RequireOnce::requireOnce public function The helper function for Ludwig integration.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.