View source
<?php
namespace Drupal\ludwig;
use GuzzleHttp\Exception\RequestException;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class RequireOnce {
use StringTranslationTrait;
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,
]));
}
}
}