Elgg  Version master
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($class, $component_id) {
76 
77  $batch = $this->getBatch($class);
78 
79  $version = $batch->getVersion();
80  $upgrade_id = "{$component_id}:{$version}";
81 
82  $upgrade = $this->upgradeExists($upgrade_id);
83 
84  if (!$upgrade) {
85  $upgrade = elgg_call(ELGG_IGNORE_ACCESS, function () use ($upgrade_id, $class, $component_id, $version) {
87 
88  // Create a new ElggUpgrade to represent the upgrade in the database
89  $upgrade = new \ElggUpgrade();
90  $upgrade->owner_guid = $site->guid;
91  $upgrade->container_guid = $site->guid;
92 
93  $upgrade->setId($upgrade_id);
94  $upgrade->setClass($class);
95  $upgrade->title = "{$component_id}:upgrade:{$version}:title";
96  $upgrade->description = "{$component_id}:upgrade:{$version}:description";
97  $upgrade->offset = 0;
98  $upgrade->save();
99 
100  return $upgrade;
101  });
102  }
103 
104  return $upgrade;
105  }
106 
116  public function getBatch(string $class, \ElggUpgrade $upgrade = null) {
117  if (!class_exists($class)) {
118  throw new InvalidArgumentException("Upgrade class $class was not found");
119  }
120 
121  if (!is_subclass_of($class, Batch::class)) {
122  throw new InvalidArgumentException("Upgrade class $class should implement " . Batch::class);
123  }
124 
125  return new $class($upgrade);
126  }
127 
135  public function upgradeExists($upgrade_id) {
136  return elgg_call(ELGG_IGNORE_ACCESS, function () use ($upgrade_id) {
137  $upgrades = \Elgg\Database\Entities::find([
138  'type' => 'object',
139  'subtype' => 'elgg_upgrade',
140  'metadata_name_value_pairs' => [
141  [
142  'name' => 'id',
143  'value' => (string) $upgrade_id,
144  ],
145  ],
146  ]);
147 
148  return $upgrades ? $upgrades[0] : false;
149  });
150  }
151 
161  public function getUpgradeByClass(string $class_name) {
162  return elgg_call(ELGG_IGNORE_ACCESS, function () use ($class_name) {
163  $upgrades = \Elgg\Database\Entities::find([
164  'type' => 'object',
165  'subtype' => 'elgg_upgrade',
166  'metadata_name_value_pairs' => [
167  [
168  'name' => 'class',
169  'value' => $class_name,
170  ],
171  ],
172  ]);
173 
174  return $upgrades ? $upgrades[0] : false;
175  });
176  }
177 }
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:304
static includeFile($file)
Include a file with as little context as possible.
Definition: Includer.php:18
$plugin
static elgg()
Get the Elgg codebase path with "/".
Definition: Paths.php:44
Exception thrown if an argument is not of the expected type.
Locates and registers both core and plugin upgrades.
Definition: Locator.php:16
$version
getBatch(string $class,\ElggUpgrade $upgrade=null)
Validates class and returns an instance of batch.
Definition: Locator.php:116
$site
Definition: icons.php:5
$plugin_id
Remove all user and plugin settings from the give plugin ID.
Definition: remove.php:8
$class
Definition: summary.php:44
$upgrades
Lists pending upgrades.
Definition: upgrades.php:11
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
if(empty($guid)) $upgrade
Definition: upgrade.php:11
Represents an upgrade that runs outside of the upgrade.php script.
Definition: ElggUpgrade.php:26
getUpgradeByClass(string $class_name)
Check if there already is an ElggUpgrade for this upgrade class.
Definition: Locator.php:161
locate()
Looks for upgrades and saves them as ElggUpgrade entities.
Definition: Locator.php:31
getUpgrade($class, $component_id)
Gets intance of an ElggUpgrade based on the given class and id.
Definition: Locator.php:75
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:101
__construct(protected Plugins $plugins)
Constructor.
Definition: Locator.php:23
upgradeExists($upgrade_id)
Check if there already is an ElggUpgrade for this upgrade.
Definition: Locator.php:135
Persistent, installation-wide key-value storage.
Definition: Plugins.php:28