Problem/Motivation
This is a new thread to gather discussion, regarding the issue that it's currently problematic to bootstrap Drupal from a PHP script that lives outside of the Drupal root directory (without a chdir() call), due to instances in core and contrib where DRUPAL_ROOT is not used in file includes everywhere that it should / could be.
Background: I recently built a non-Drupal PHP app (a Silex / Symfony2 app), which bootstraps Drupal 7 and which utilises Drupal for various tasks (e.g. authentication, page rendering). This app's index.php file lives outside of the Drupal install directory, and it manually sets DRUPAL_ROOT and $base_url before including and running the Drupal bootstrapper.
For the most part, my attempts at this integration went smoothly. However, on several occasions I ran into something that wasn't working, due to an include_once / file_exists() / etc call, in core or contrib, that doesn't prepend DRUPAL_ROOT to a filepath.
Proposed resolution
I originally had no doubt that all of the DRUPAL_ROOT bugs I found were bugs, and that the correct solution is to slowly but surely patch and fix them. However, this recent issue comment has made me unsure:
@sun: If you are using a custom script to boot Drupal, which does not happen to live in Drupal's root directory, then it is the responsibility of your custom script to
chdir()to Drupal's root directory. All of the custom/complementary scripts in Drupal core are doing that, and so should yours. I'm actually very surprised that your ride into bugs starts in wysiwyg.module. I'm relatively confident that I can guarantee you that your ride won't end here. What you're trying to do is known to not work in D8 even, despite a massive move towards OO-namespaced code and a class loader that looks up files based on the Drupal root.
So, the reason for this new thread, is that I'd like to open up the question: should we even be trying to fix this? Am I doing something worthwhile, in submitting these DRUPAL_ROOT patches; and are they worth reviewing and committing? Or am I wasting my time, because Drupal flat-out does not support bootstrapping from a custom script without a chdir() call into the Drupal root dir? And if Drupal doesn't support this, should we make Drupal support it; or would our efforts be better invested elsewhere?
Original reports
The bug reports that I filed are:
Core:
#1891708: Need DRUPAL_ROOT in include of template.php
#1891802: Need DRUPAL_ROOT when rendering CSS include links
#1924212: Need DRUPAL_ROOT in file_scan_directory
Contrib:
#1890872: Need DRUPAL_ROOT in require of include files
#1890866: Need DRUPAL_ROOT in require of og.field.inc (fixed)
#1924236: Need DRUPAL_ROOT in various places
If anyone finds and patches more DRUPAL_ROOT bugs, please create a new issue for that specific bug, post patches there, and add a link to the issue in this thread. Please do not add individual DRUPAL_ROOT bug reports / patches to this thread, which is to be only for discussion of the wider issue.
Comments
Comment #1
sunThe issue summary only links to individual spots that are lacking DRUPAL_ROOT.
The main question of bootstrapping Drupal without a
chdir()is discussed in:#1540136: Simplify index.php by moving DRUPAL_ROOT to bootstrap.inc
Also related:
#1792310: Wrong DRUPAL_ROOT with non-standard code structure
#1827448: Use __DIR__ instead of DRUPAL_ROOT where possible/sensible
Comment #5
JvE commentedI think it's a worthwhile goal to be able to bootstrap a Drupal kernel from anywhere.
Why else would we have the
$app_rootargument in the DrupalKernel constructor?From #2631362: Inject DRUPAL_ROOT into DrupalKernel
Comment #10
volegerNow that can be achieved if the Drupal core will define a separate composer template for that purpose.
Comment #13
joachim commentedPossibly a duplicate of #1792310: Wrong DRUPAL_ROOT with non-standard code structure?
Comment #16
joachim commentedThis will probably get fixed by #1792310: Wrong DRUPAL_ROOT with non-standard code structure. The latest MR there uses absolute paths for defining Drupal's locations.