From b4f353e9ac81e451f69ec8131f343107c3400aff Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Fri, 6 Dec 2019 15:47:14 +1100 Subject: [PATCH] Added support for legacy deprecations. (#12) --- .circleci/build.sh | 23 ++++- .circleci/config.yml | 32 ++++++- minisite.theme.inc | 3 +- src/Asset.php | 4 +- src/LegacyWrapper.php | 98 +++++++++++++++++++++ src/Minisite.php | 7 +- src/UrlValidator.php | 2 +- tests/src/Functional/FieldCreationTrait.php | 8 +- tests/src/Functional/MinisiteTestBase.php | 11 +-- 9 files changed, 163 insertions(+), 25 deletions(-) create mode 100644 src/LegacyWrapper.php diff --git a/.circleci/build.sh b/.circleci/build.sh index 15ab8b5..ecb8c8d 100755 --- a/.circleci/build.sh +++ b/.circleci/build.sh @@ -11,8 +11,26 @@ composer validate --ansi --strict [ -d build ] && echo "==> Remove existing build directory" && chmod -Rf 777 build && rm -rf build -echo "==> Initialise Drupal site" -composer create-project drupal-composer/drupal-project:8.x-dev build --no-interaction +# Allow installing custom version of Drupal core, but only coupled with +# drupal-project SHA (required to get correct dependencies). +if [ -n "${DRUPAL_PROJECT_SHA}" ] && [ -n "${DRUPAL_VERSION}" ] ; then + echo "==> Initialise Drupal site from the scaffold commit $DRUPAL_PROJECT_SHA" + + git clone -n https://github.com/drupal-composer/drupal-project.git build + git --git-dir=build/.git --work-tree=build checkout "${DRUPAL_PROJECT_SHA}" + rm -rf build/.git > /dev/null + + echo "==> Pin Drupal to a specific version" + sed_opts=(-i) && [ "$(uname)" == "Darwin" ] && sed_opts=(-i '') + sed "${sed_opts[@]}" 's|\(.*"drupal\/core"\): "\(.*\)",.*|\1: '"\"$DRUPAL_VERSION\",|" build/composer.json + cat build/composer.json + + echo "==> Install dependencies" + composer --working-dir=build install +else + echo "==> Initialise Drupal site from the latest scaffold" + composer create-project drupal-composer/drupal-project:8.x-dev build --no-interaction +fi echo "==> Install additional dev dependencies" composer --working-dir=build require --dev dealerdirect/phpcodesniffer-composer-installer:^0.5 @@ -30,6 +48,7 @@ DB_FILE="${DB_FILE:-/tmp/site_${MODULE}.sqlite}" echo "==> Install Drupal into SQLite database ${DB_FILE}" build/vendor/bin/drush -r build/web si "${DRUPAL_PROFILE:-standard}" -y --db-url "sqlite://${DB_FILE}" --account-name=admin install_configure_form.enable_update_status_module=NULL install_configure_form.enable_update_status_emails=NULL +build/vendor/bin/drush -r "$(pwd)/build/web" status echo "==> Symlink module code" rm -rf build/web/modules/"${MODULE}"/* > /dev/null diff --git a/.circleci/config.yml b/.circleci/config.yml index b2b1c03..49c163a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -7,8 +7,6 @@ aliases: working_directory: ~/project docker: - image: circleci/php:7.3-cli-browsers - environment: - DEPLOY_SSH_FINGERPRINT: *deploy_ssh_fingerprint job-build: &job-build steps: @@ -38,6 +36,26 @@ jobs: - image: circleci/php:7.2-cli-browsers <<: *job-build + build-php-7.3-legacy: + <<: *container_config + environment: + DRUPAL_VERSION: 8.7.7 + # Drupal project commit before moving 8.8.0. + # https://github.com/drupal-composer/drupal-project/commit/53f6910c35db73d0b367d5b6f22be4af94dd1af3 + DRUPAL_PROJECT_SHA: 53f6910c35db73d0b367d5b6f22be4af94dd1af3 + <<: *job-build + + build-php-7.2-legacy: + <<: *container_config + docker: + - image: circleci/php:7.2-cli-browsers + environment: + DRUPAL_VERSION: 8.7.7 + # Drupal project commit before moving 8.8.0. + # https://github.com/drupal-composer/drupal-project/commit/53f6910c35db73d0b367d5b6f22be4af94dd1af3 + DRUPAL_PROJECT_SHA: 53f6910c35db73d0b367d5b6f22be4af94dd1af3 + <<: *job-build + deploy: <<: *container_config environment: @@ -61,10 +79,20 @@ workflows: filters: tags: only: /.*/ + - build-php-7.3-legacy: + filters: + tags: + only: /.*/ + - build-php-7.2-legacy: + filters: + tags: + only: /.*/ - deploy: requires: - build-php-7.3 - build-php-7.2 + - build-php-7.3-legacy + - build-php-7.2-legacy filters: tags: only: /.*/ diff --git a/minisite.theme.inc b/minisite.theme.inc index abba10b..b7d7564 100644 --- a/minisite.theme.inc +++ b/minisite.theme.inc @@ -10,6 +10,7 @@ use Drupal\Core\Render\Element; use Drupal\Core\Template\Attribute; use Drupal\Core\Url; use Drupal\file\Entity\File; +use Drupal\minisite\LegacyWrapper; /** * Implements hook_theme(). @@ -95,7 +96,7 @@ function template_preprocess_minisite_link(array &$variables) { // Asset path is provided. if (isset($variables['asset_path'])) { // Asset path is a file. - if (\Drupal::service('stream_wrapper_manager')->isValidUri($variables['asset_path'])) { + if (LegacyWrapper::isValidUri($variables['asset_path'])) { $url = file_create_url($variables['asset_path']); } // Asset path is an alias to a path. diff --git a/src/Asset.php b/src/Asset.php index 518bc72..cde802a 100644 --- a/src/Asset.php +++ b/src/Asset.php @@ -175,7 +175,7 @@ public static function load($id) { * {@inheritdoc} */ public static function loadByUri($uri) { - if (!\Drupal::service('stream_wrapper_manager')->isValidUri($uri)) { + if (!LegacyWrapper::isValidUri($uri)) { return NULL; } @@ -275,7 +275,7 @@ public function delete() { // assets directory. $dirname = $this->urlBag->getUri(); while (($dirname = $fs->dirname($dirname)) && $dirname != Minisite::getCommonAssetDir()) { - if (empty($fs->scanDirectory($dirname, '/.*/'))) { + if (empty(LegacyWrapper::scanDirectory($dirname, '/.*/'))) { $fs->deleteRecursive($dirname); } } diff --git a/src/LegacyWrapper.php b/src/LegacyWrapper.php new file mode 100644 index 0000000..a88e60e --- /dev/null +++ b/src/LegacyWrapper.php @@ -0,0 +1,98 @@ +scanDirectory($dir, $mask, $options); + } + + if (function_exists('file_scan_directory')) { + return file_scan_directory($dir, $mask, $options); + } + + throw new \RuntimeException('Unable to find compatible function'); + } + + /** + * Determines whether the URI has a valid scheme for file API operations. + * + * @throws \RuntimeException + * If compatible function is not found. + */ + public static function isValidUri($uri) { + if (method_exists(StreamWrapperManager::class, 'isValidUri')) { + return \Drupal::service('stream_wrapper_manager')->isValidUri($uri); + } + + if (function_exists('file_valid_uri')) { + return file_valid_uri($uri); + } + + throw new \RuntimeException('Unable to find compatible function'); + } + + /** + * Returns the entity view display associated with a bundle and view mode. + * + * @throws \RuntimeException + * If compatible function is not found. + */ + public static function getViewDisplay($entity_type, $bundle, $view_mode) { + $dr = \Drupal::service('entity_display.repository'); + + if (method_exists(get_class($dr), 'getViewDisplay')) { + return $dr->getViewDisplay($entity_type, $bundle, $view_mode); + } + + if (function_exists('entity_get_display')) { + return entity_get_display($entity_type, $bundle, $view_mode); + } + + throw new \RuntimeException('Unable to find compatible function'); + } + + /** + * Returns the entity form display associated with a bundle and form mode. + * + * @throws \RuntimeException + * If compatible function is not found. + */ + public static function getFormDisplay($entity_type, $bundle, $form_mode) { + $dr = \Drupal::service('entity_display.repository'); + + if (method_exists(get_class($dr), 'getFormDisplay')) { + return $dr->getFormDisplay($entity_type, $bundle, $form_mode); + } + + if (function_exists('entity_get_display')) { + return entity_get_form_display($entity_type, $bundle, $form_mode); + } + + throw new \RuntimeException('Unable to find compatible function'); + } + +} diff --git a/src/Minisite.php b/src/Minisite.php index 8598bf8..cd90aea 100644 --- a/src/Minisite.php +++ b/src/Minisite.php @@ -235,16 +235,13 @@ public function getIndexAssetUri() { * Process archive by extracting files and filling-in assets information. */ protected function processArchive() { - /** @var \Drupal\Core\File\FileSystemInterface $fs */ - $fs = \Drupal::service('file_system'); - $asset_directory = $this->prepareAssetDirectory(); // Scan directory for previously extracted files. // Note that we are not checking if _all_ files from archive exist: if any // were removed - the archive file would need to be re-uploaded to have // the files re-extracted. - $files = $fs->scanDirectory($asset_directory, '/.*/'); + $files = LegacyWrapper::scanDirectory($asset_directory, '/.*/'); if (!$files) { // Files do not exist - looks like this is a first time processing, so @@ -254,7 +251,7 @@ protected function processArchive() { $archiver->listContents(); $archiver->extract($asset_directory); // Re-scan files directory. - $files = $files = $fs->scanDirectory($asset_directory, '/.*/'); + $files = LegacyWrapper::scanDirectory($asset_directory, '/.*/'); } $this->assetContainer = isset($this->assetContainer) ? $this->assetContainer : new AssetContainer(); diff --git a/src/UrlValidator.php b/src/UrlValidator.php index dc377a4..238f556 100644 --- a/src/UrlValidator.php +++ b/src/UrlValidator.php @@ -91,7 +91,7 @@ public static function relativeToRoot($url, $parent) { // path. $url = str_replace('../', '', $url); $url = rtrim($parent, '/') . '/' . ltrim($url, '/'); - $url = \Drupal::service('stream_wrapper_manager')->isValidUri($url) ? file_url_transform_relative(file_create_url($url)) : $url; + $url = LegacyWrapper::isValidUri($url) ? file_url_transform_relative(file_create_url($url)) : $url; // Decode URL encoded in file_create_url(). $url = rawurldecode($url); $url = '/' . ltrim($url, '/'); diff --git a/tests/src/Functional/FieldCreationTrait.php b/tests/src/Functional/FieldCreationTrait.php index 13feceb..87bfd29 100644 --- a/tests/src/Functional/FieldCreationTrait.php +++ b/tests/src/Functional/FieldCreationTrait.php @@ -4,6 +4,7 @@ use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\minisite\LegacyWrapper; /** * Provides methods for creating minisite fields. @@ -70,16 +71,15 @@ public function attachMinisiteField($name, $entity_type, $bundle, array $field_s ]; FieldConfig::create($field)->save(); - \Drupal::service('entity_display.repository') - ->getFormDisplay($entity_type, $bundle, 'default') + LegacyWrapper::getFormDisplay($entity_type, $bundle, 'default') ->setComponent($name, [ 'type' => 'file_generic', 'settings' => $widget_settings, ]) ->save(); + // Assign display settings. - \Drupal::service('entity_display.repository') - ->getViewDisplay($entity_type, $bundle, 'default') + LegacyWrapper::getViewDisplay($entity_type, $bundle, 'default') ->setComponent($name, [ 'label' => 'hidden', 'type' => 'file_default', diff --git a/tests/src/Functional/MinisiteTestBase.php b/tests/src/Functional/MinisiteTestBase.php index fe14c9b..bf6caea 100644 --- a/tests/src/Functional/MinisiteTestBase.php +++ b/tests/src/Functional/MinisiteTestBase.php @@ -8,6 +8,7 @@ use Drupal\field\Entity\FieldStorageConfig; use Drupal\file\Entity\File; use Drupal\file\FileInterface; +use Drupal\minisite\LegacyWrapper; use Drupal\minisite\Minisite; use Drupal\Tests\BrowserTestBase; use Drupal\Tests\minisite\Traits\FixtureTrait; @@ -328,10 +329,7 @@ public function assertArchiveFileNotExist(FileInterface $file) { * Assert assets paths exist. */ public function assertAssetFilesExist($files) { - /** @var \Drupal\Core\File\FileSystemInterface $fs */ - $fs = \Drupal::service('file_system'); - - $actual_files = array_keys($fs->scanDirectory(Minisite::getCommonAssetDir(), '/.*/')); + $actual_files = array_keys(LegacyWrapper::scanDirectory(Minisite::getCommonAssetDir(), '/.*/')); $this->assertEquals(count($actual_files), count($files)); foreach ($files as $test_file) { @@ -347,10 +345,7 @@ public function assertAssetFilesExist($files) { * Assert assets paths not exist. */ public function assertAssetFilesNotExist($files) { - /** @var \Drupal\Core\File\FileSystemInterface $fs */ - $fs = \Drupal::service('file_system'); - - $actual_files = array_keys($fs->scanDirectory(Minisite::getCommonAssetDir(), '/.*/')); + $actual_files = array_keys(LegacyWrapper::scanDirectory(Minisite::getCommonAssetDir(), '/.*/')); foreach ($files as $test_file) { $found_files = array_filter($actual_files, function ($value) use ($test_file) { return substr($value, -strlen($test_file)) === $test_file;