Skip to content

Commit

Permalink
Added support for legacy deprecations. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Skrypnyk authored Dec 6, 2019
1 parent 6c80287 commit b4f353e
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 25 deletions.
23 changes: 21 additions & 2 deletions .circleci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 30 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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: /.*/
Expand Down
3 changes: 2 additions & 1 deletion minisite.theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
}
Expand Down
98 changes: 98 additions & 0 deletions src/LegacyWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Drupal\minisite;

use Drupal\Core\File\FileSystem;
use Drupal\Core\StreamWrapper\StreamWrapperManager;

/**
* Class LegacyWrapper.
*
* Helper class to resolve compatibility between Drupal core versions.
*
* @todo: Remove this file once 8.7.x is no longer supported.
*
* @package Drupal\minisite
*/
class LegacyWrapper {

/**
* Finds all files that match a given mask in a given directory.
*
* @throws \RuntimeException
* If compatible function is not found.
*/
public static function scanDirectory($dir, $mask, array $options = []) {
if (method_exists(FileSystem::class, 'scanDirectory')) {
/** @var \Drupal\Core\File\FileSystemInterface $fs */
$fs = \Drupal::service('file_system');

return $fs->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');
}

}
7 changes: 2 additions & 5 deletions src/Minisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/UrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
Expand Down
8 changes: 4 additions & 4 deletions tests/src/Functional/FieldCreationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\minisite\LegacyWrapper;

/**
* Provides methods for creating minisite fields.
Expand Down Expand Up @@ -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',
Expand Down
11 changes: 3 additions & 8 deletions tests/src/Functional/MinisiteTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit b4f353e

Please sign in to comment.