You are here

public function RequireOnce::requireOnce in Ludwig 8

The helper function for Ludwig integration.

Parameters

string $package_name: The package name.

string $file_to_require: The file to require.

string $dir_name: The caller module directory name.

File

src/RequireOnce.php, line 30

Class

RequireOnce
Provides service for ludwig require_once calls.

Namespace

Drupal\ludwig

Code

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,
    ]));
  }
}