Elgg  Version 7.1
Locator.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrade;
4 
7 use Elgg\Includer;
9 
16 class Locator {
17 
23  public function __construct(protected Plugins $plugins) {
24  }
25 
31  public function locate() {
32  $pending_upgrades = [];
33 
34  // Check for core upgrades
35  $core_upgrades = Includer::includeFile(Paths::elgg() . 'engine/upgrades.php');
36 
37  foreach ($core_upgrades as $class) {
38  $upgrade = $this->getUpgrade($class, 'core');
39  if ($upgrade) {
40  $pending_upgrades[] = $upgrade;
41  }
42  }
43 
44  $plugins = $this->plugins->find('active');
45 
46  // Check for plugin upgrades
47  foreach ($plugins as $plugin) {
48  $batches = $plugin->getStaticConfig('upgrades');
49 
50  if (empty($batches)) {
51  continue;
52  }
53 
54  $plugin_id = $plugin->getID();
55 
56  foreach ($batches as $class) {
57  $upgrade = $this->getUpgrade($class, $plugin_id);
58  if ($upgrade) {
59  $pending_upgrades[] = $upgrade;
60  }
61  }
62  }
63 
64  return $pending_upgrades;
65  }
66 
75  public function getUpgrade(string $class, string $component_id): \ElggUpgrade {
76  $batch = $this->getBatch($class);
77 
78  $version = $batch->getVersion();
79  $upgrade_id = "{$component_id}:{$version}";
80 
81  $upgrade = $this->upgradeExists($upgrade_id);
82 
83  if (!$upgrade) {
84  $upgrade = elgg_call(ELGG_IGNORE_ACCESS, function () use ($upgrade_id, $class, $component_id, $version) {
85  // Create a new ElggUpgrade to represent the upgrade in the database
86  $upgrade = new \ElggUpgrade();
87 
88  $upgrade->id = $upgrade_id;
89  $upgrade->class = $class;
90  $upgrade->save();
91 
92  return $upgrade;
93  });
94  }
95 
96  return $upgrade;
97  }
98 
108  public function getBatch(string $class, ?\ElggUpgrade $upgrade = null): Batch {
109  if (!class_exists($class)) {
110  throw new InvalidArgumentException("Upgrade class {$class} was not found");
111  }
112 
113  if (!is_subclass_of($class, Batch::class)) {
114  throw new InvalidArgumentException("Upgrade class {$class} should implement " . Batch::class);
115  }
116 
117  return new $class($upgrade);
118  }
119 
127  public function upgradeExists($upgrade_id) {
128  return elgg_call(ELGG_IGNORE_ACCESS, function () use ($upgrade_id) {
129  $upgrades = \Elgg\Database\Entities::find([
130  'type' => 'object',
131  'subtype' => 'elgg_upgrade',
132  'metadata_name_value_pairs' => [
133  [
134  'name' => 'id',
135  'value' => (string) $upgrade_id,
136  ],
137  ],
138  ]);
139 
140  return $upgrades ? $upgrades[0] : false;
141  });
142  }
143 
153  public function getUpgradeByClass(string $class_name) {
154  return elgg_call(ELGG_IGNORE_ACCESS, function () use ($class_name) {
155  $upgrades = \Elgg\Database\Entities::find([
156  'type' => 'object',
157  'subtype' => 'elgg_upgrade',
158  'metadata_name_value_pairs' => [
159  [
160  'name' => 'class',
161  'value' => $class_name,
162  ],
163  ],
164  ]);
165 
166  return $upgrades ? $upgrades[0] : false;
167  });
168  }
169 }
if(empty($guid)) $upgrade
Definition: upgrade.php:11
$plugin_id
Remove all user and plugin settings from the give plugin ID.
Definition: remove.php:8
$class
Definition: summary.php:44
Represents an upgrade that runs outside the upgrade.php script.
Definition: ElggUpgrade.php:26
Persistent, installation-wide key-value storage.
Definition: Plugins.php:27
Exception thrown if an argument is not of the expected type.
Allow executing scripts without $this context or local vars.
Definition: Includer.php:10
static includeFile($file)
Include a file with as little context as possible.
Definition: Includer.php:18
Find Elgg and project paths.
Definition: Paths.php:8
static elgg()
Get the Elgg codebase path with "/".
Definition: Paths.php:44
Long running upgrades should implement this interface.
Definition: Batch.php:10
Locates and registers both core and plugin upgrades.
Definition: Locator.php:16
__construct(protected Plugins $plugins)
Constructor.
Definition: Locator.php:23
locate()
Looks for upgrades and saves them as ElggUpgrade entities.
Definition: Locator.php:31
upgradeExists($upgrade_id)
Check if there already is an ElggUpgrade for this upgrade.
Definition: Locator.php:127
getUpgrade(string $class, string $component_id)
Gets instance of an ElggUpgrade based on the given class and id.
Definition: Locator.php:75
getUpgradeByClass(string $class_name)
Check if there already is an ElggUpgrade for this upgrade class.
Definition: Locator.php:153
getBatch(string $class, ?\ElggUpgrade $upgrade=null)
Validates class and returns an instance of batch.
Definition: Locator.php:108
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
$version
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags.
Definition: elgglib.php:300
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$plugin
$upgrades
Lists pending upgrades.
Definition: upgrades.php:11