summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/update/src/UpdateRoot.php
blob: 604e9ff0f52972c483df349b8bdc31afccb4e2ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

namespace Drupal\update;

use Drupal\Core\DrupalKernelInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Gets the root path used by the legacy Update Manager.
 *
 * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. There is no
 *   replacement. Use composer to manage the code for your site.
 *
 * @see https://www.drupal.org/node/3522119
 */
class UpdateRoot {

  /**
   * The Drupal kernel.
   *
   * @var \Drupal\Core\DrupalKernelInterface
   */
  protected $drupalKernel;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The update root.
   *
   * @var string
   */
  protected $updateRoot;

  /**
   * Constructs an UpdateRoot instance.
   *
   * @param \Drupal\Core\DrupalKernelInterface $drupal_kernel
   *   The Drupal kernel.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(DrupalKernelInterface $drupal_kernel, RequestStack $request_stack) {
    @trigger_error(__CLASS__ . ' is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. There is no replacement. Use composer to manage the code for your site. See https://www.drupal.org/node/3522119', E_USER_DEPRECATED);
    $this->drupalKernel = $drupal_kernel;
    $this->requestStack = $request_stack;
  }

  /**
   * Sets the root path under which projects are installed or updated.
   *
   * @param string $update_root
   *   The update root.
   */
  public function set(string $update_root): void {
    $this->updateRoot = $update_root;
  }

  /**
   * Gets the root path under which projects are installed or updated.
   *
   * The Update Manager will ensure that project files can only be copied to
   * specific subdirectories of this root path.
   *
   * @return string
   *   The root path for project installation or update.
   */
  public function __toString(): string {
    // Return the $updateRoot when it is set.
    if (isset($this->updateRoot)) {
      return $this->updateRoot;
    }

    // Normally the Update Manager's root path is the same as the app root (the
    // directory in which the Drupal site is installed).
    $root_path = $this->drupalKernel->getAppRoot();

    // When running in a test site, change the root path to be the testing site
    // directory. This ensures that it will always be writable by the webserver
    // (thereby allowing the actual extraction and installation of projects by
    // the Update Manager to be tested) and also ensures that new project files
    // added there won't be visible to the parent site and will be properly
    // cleaned up once the test finishes running. This is done here (rather
    // than having the tests install a module which overrides the update root
    // factory service) to ensure that the parent site is automatically kept
    // clean without relying on test authors to take any explicit steps. See
    // also \Drupal\update\Tests\Functional\UpdateTestBase::setUp().
    if (DRUPAL_TEST_IN_CHILD_SITE) {
      $kernel = $this->drupalKernel;
      $request = $this->requestStack->getCurrentRequest();
      $root_path .= '/' . $kernel::findSitePath($request);
    }

    return $root_path;
  }

}