Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Merge requests
!1922
Add a Composer plugin to core for writing the locations of Drupal files - 9.5.x
Code
Review changes
Check out branch
Download
Patches
Plain diff
Closed
Add a Composer plugin to core for writing the locations of Drupal files - 9.5.x
issue/drupal-1792310:1792310-locations-class-writer-plugin
into
9.5.x
Overview
0
Commits
38
Pipelines
0
Changes
36
Closed
Joachim Noreiko
requested to merge
issue/drupal-1792310:1792310-locations-class-writer-plugin
into
9.5.x
3 years ago
Overview
0
Commits
38
Pipelines
0
Changes
36
Expand
Closes
#1792310
Edited
2 years ago
by
Joachim Noreiko
0
0
Merge request reports
Compare
9.5.x
version 16
bec58c29
2 years ago
version 15
1c18b365
2 years ago
version 14
429f1240
2 years ago
version 13
5164b114
2 years ago
version 12
a6d9abf6
2 years ago
version 11
a6d9abf6
2 years ago
version 10
2fb261b1
3 years ago
version 9
346c67d9
3 years ago
version 8
5d81c827
3 years ago
version 7
d7b821b3
3 years ago
version 6
9b429d4e
3 years ago
version 5
0b9968ae
3 years ago
version 4
1d6fbafc
3 years ago
version 3
16debb86
3 years ago
version 2
9a6232ff
3 years ago
version 1
751d8359
3 years ago
9.5.x (base)
and
latest version
latest version
1ecf63d1
38 commits,
2 years ago
version 16
bec58c29
37 commits,
2 years ago
version 15
1c18b365
36 commits,
2 years ago
version 14
429f1240
35 commits,
2 years ago
version 13
5164b114
33 commits,
2 years ago
version 12
a6d9abf6
30 commits,
2 years ago
version 11
a6d9abf6
243 commits,
2 years ago
version 10
2fb261b1
31 commits,
3 years ago
version 9
346c67d9
28 commits,
3 years ago
version 8
5d81c827
27 commits,
3 years ago
version 7
d7b821b3
26 commits,
3 years ago
version 6
9b429d4e
25 commits,
3 years ago
version 5
0b9968ae
25 commits,
3 years ago
version 4
1d6fbafc
21 commits,
3 years ago
version 3
16debb86
21 commits,
3 years ago
version 2
9a6232ff
21 commits,
3 years ago
version 1
751d8359
21 commits,
3 years ago
36 files
+
680
−
65
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
36
Search (e.g. *.vue) (Ctrl+P)
composer/Plugin/Locations/GenerateLocationsClass.php
0 → 100644
+
182
−
0
Options
<?php
namespace
Drupal\Composer\Plugin\Locations
;
use
Composer\IO\IOInterface
;
use
Composer\Composer
;
use
Composer\Factory
;
/**
* Generates the DrupalLocation file which defines the Drupal app root constant.
*
* Drupal and the app root may be installed in various locations, and at runtime
* it is not easy to determine these. Part of the difficulty is that Composer
* can symlink packages in, and PHP's file location constants such as __DIR__
* resolve symlinks.
*
* The authority on the location of packages is Composer, since it puts them in
* their locations, and the authority on the location of the app root is this
* scaffolding plugin, since it reads it from the composer.json file.
*
* Reading the locations from composer.json is possible at runtime, but is
* undesirable for performance. Therefore, during the Composer install process
* when we have access this data, this plugin writes a PHP class file which
* defines the locations as class constants.
*
* The class file is written into the Composer project root. To be loadable, it
* must be defined to the autoloader in the project's composer.json:
* @code
* "autoload": {
* "psr-4": {
* "Drupal\\Locations\\": ""
* }
* },
* @endcode
*
* To ensure the project's codebase is portable, the generated class must not
* use absolute paths. It should instead use the __DIR__ constant, which in the
* generated class will give the project root.
*
* @internal
*/
class
GenerateLocationsClass
{
/**
* The template code for the DrupalLocation class file which is written.
*
* This contains the token '%app_root' which is replaced when the file is
* written.
*
* @var string
*/
private
static
$generatedFileTemplate
=
<<<'EOF'
<?php
namespace Drupal\Locations;
/**
* Defines the Drupal app root.
*
* This allows Drupal to find the directory in which Drupal files are
* located, such as Drupal core, the sites directory, and the extension
* directories.
*
* This is defined in a class so it is available as soon as the Composer
* autoloader has been included.
*
* This file was generated by
* \Drupal\Composer\Plugin\Locations\GenerateLocationsClass.
*/
class DrupalLocation {
/**
* Defines the root directory of the Drupal installation.
*
* This is the directory containing the scaffolded index.php file which is
* Drupal's web entry point.
*/
public const APP_ROOT = __DIR__ . '%app_root';
}
EOF;
/**
* This class provides only static methods.
*/
private
function
__construct
()
{
}
/**
* Writes the DrupalLocations class into the same directory as this file.
*
* @param \Composer\Composer $composer
* The Composer object.
* @param \Composer\IO\IOInterface $io
* The Composer I/O object.
*/
public
static
function
generate
(
Composer
$composer
,
IOInterface
$io
)
{
$extra
=
$composer
->
getPackage
()
->
getExtra
();
// Composer changes the current directory to the project root, even if it
// run in a subdirectory.
$absolute_project_root
=
getcwd
();
if
(
isset
(
$extra
[
'drupal-scaffold'
][
'locations'
][
'web-root'
]))
{
// The root composer.json defines a scaffold location for Drupal. We
// therefore know this is the app root.
$web_root
=
$extra
[
'drupal-scaffold'
][
'locations'
][
'web-root'
];
$absolute_app_root
=
$absolute_project_root
.
'/'
.
$web_root
;
$absolute_app_root
=
realpath
(
$absolute_app_root
);
$io
->
write
(
"Drupal app root defined as
{
$absolute_app_root
}
."
);
$relative_web_root
=
'/'
.
trim
(
$web_root
,
'/'
);
static
::
generateLocations
(
$composer
,
$io
,
$absolute_project_root
,
$absolute_app_root
,
$relative_web_root
);
}
else
{
// Get the project root's absolute path from the root composer file path.
// This is given as a relative path.
$composer_file_path
=
Factory
::
getComposerFile
();
$absolute_app_root
=
realpath
(
dirname
(
$composer_file_path
));
$io
->
write
(
"Drupal app root assumed to be the Composer project root,
{
$absolute_app_root
}
."
);
static
::
generateLocations
(
$composer
,
$io
,
$absolute_project_root
,
$absolute_app_root
,
''
);
}
}
/**
* Writes the DrupalLocations class with the given value of the app root.
*
* @param \Composer\Composer $composer
* The Composer object.
* @param \Composer\IO\IOInterface $io
* The Composer I/O object.
* @param string $absolute_project_root
* The absolute path to the Composer project root, without a trailing slash.
* @param string $absolute_app_root
* The absolute path to the Drupal app root, without a trailing slash.
* @param string $relative_app_root
* The relative path to the Drupal app root, relative to the Composer
* project root, with an initial slash, and without a trailing slash.
*/
protected
static
function
generateLocations
(
Composer
$composer
,
IOInterface
$io
,
$absolute_project_root
,
$absolute_app_root
,
$relative_app_root
)
{
$class_php
=
str_replace
(
'%app_root'
,
$relative_app_root
,
static
::
$generatedFileTemplate
);
$locations_class_directory
=
static
::
getLocationsClassDirectory
(
$absolute_project_root
,
$absolute_app_root
);
if
(
!
is_writable
(
$locations_class_directory
))
{
throw
new
\Exception
(
sprintf
(
"The directory %s is not writable."
,
$locations_class_directory
));
}
$file_location
=
$locations_class_directory
.
'/DrupalLocation.php'
;
$result
=
file_put_contents
(
$file_location
,
$class_php
);
if
(
$result
!==
FALSE
)
{
$io
->
write
(
"Writing Drupal locations class to
$file_location
."
);
}
else
{
$io
->
writeError
(
"There was a problem writing the Drupal locations class to
$file_location
."
);
}
}
/**
* Gets the directory to write the locations class to.
*
* @param string $absolute_project_root
* The absolute path to the Composer project root, without a trailing slash.
* @param string $absolute_app_root
* The absolute path to the Drupal app root, without a trailing slash.
*
* @return string
* The absolute path of the directory to write to.
*/
protected
static
function
getLocationsClassDirectory
(
string
$absolute_project_root
,
string
$absolute_app_root
):
string
{
return
$absolute_project_root
;
}
}
Loading