Elgg  Version 5.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 
21  private $plugins;
22 
28  public function __construct(Plugins $plugins) {
29  $this->plugins = $plugins;
30  }
31 
37  public function locate() {
38  $pending_upgrades = [];
39 
40  // Check for core upgrades
41  $core_upgrades = Includer::includeFile(Paths::elgg() . 'engine/upgrades.php');
42 
43  foreach ($core_upgrades as $class) {
44  $upgrade = $this->getUpgrade($class, 'core');
45  if ($upgrade) {
46  $pending_upgrades[] = $upgrade;
47  }
48  }
49 
50  $plugins = $this->plugins->find('active');
51 
52  // Check for plugin upgrades
53  foreach ($plugins as $plugin) {
54  $batches = $plugin->getStaticConfig('upgrades');
55 
56  if (empty($batches)) {
57  continue;
58  }
59 
60  $plugin_id = $plugin->getID();
61 
62  foreach ($batches as $class) {
63  $upgrade = $this->getUpgrade($class, $plugin_id);
64  if ($upgrade) {
65  $pending_upgrades[] = $upgrade;
66  }
67  }
68  }
69 
70  return $pending_upgrades;
71  }
72 
81  public function getUpgrade($class, $component_id) {
82 
83  $batch = $this->getBatch($class);
84 
85  $version = $batch->getVersion();
86  $upgrade_id = "{$component_id}:{$version}";
87 
88  $upgrade = $this->upgradeExists($upgrade_id);
89 
90  if (!$upgrade) {
91  $upgrade = elgg_call(ELGG_IGNORE_ACCESS, function () use ($upgrade_id, $class, $component_id, $version) {
93 
94  // Create a new ElggUpgrade to represent the upgrade in the database
95  $upgrade = new \ElggUpgrade();
96  $upgrade->owner_guid = $site->guid;
97  $upgrade->container_guid = $site->guid;
98 
99  $upgrade->setId($upgrade_id);
100  $upgrade->setClass($class);
101  $upgrade->title = "{$component_id}:upgrade:{$version}:title";
102  $upgrade->description = "{$component_id}:upgrade:{$version}:description";
103  $upgrade->offset = 0;
104  $upgrade->save();
105 
106  return $upgrade;
107  });
108  }
109 
110  return $upgrade;
111  }
112 
122  public function getBatch(string $class, \ElggUpgrade $upgrade = null) {
123  if (!class_exists($class)) {
124  throw new InvalidArgumentException("Upgrade class $class was not found");
125  }
126 
127  if (!is_subclass_of($class, Batch::class)) {
128  throw new InvalidArgumentException("Upgrade class $class should implement " . Batch::class);
129  }
130 
131  return new $class($upgrade);
132  }
133 
141  public function upgradeExists($upgrade_id) {
142  return elgg_call(ELGG_IGNORE_ACCESS, function () use ($upgrade_id) {
143  $upgrades = \Elgg\Database\Entities::find([
144  'type' => 'object',
145  'subtype' => 'elgg_upgrade',
146  'metadata_name_value_pairs' => [
147  [
148  'name' => 'id',
149  'value' => (string) $upgrade_id,
150  ],
151  ],
152  ]);
153 
154  return $upgrades ? $upgrades[0] : false;
155  });
156  }
157 
167  public function getUpgradeByClass(string $class_name) {
168  return elgg_call(ELGG_IGNORE_ACCESS, function () use ($class_name) {
169  $upgrades = \Elgg\Database\Entities::find([
170  'type' => 'object',
171  'subtype' => 'elgg_upgrade',
172  'metadata_name_value_pairs' => [
173  [
174  'name' => 'class',
175  'value' => $class_name,
176  ],
177  ],
178  ]);
179 
180  return $upgrades ? $upgrades[0] : false;
181  });
182  }
183 }
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:299
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:122
$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:130
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:167
locate()
Looks for upgrades and saves them as ElggUpgrade entities.
Definition: Locator.php:37
getUpgrade($class, $component_id)
Gets intance of an ElggUpgrade based on the given class and id.
Definition: Locator.php:81
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:98
upgradeExists($upgrade_id)
Check if there already is an ElggUpgrade for this upgrade.
Definition: Locator.php:141
Persistent, installation-wide key-value storage.
Definition: Plugins.php:29
__construct(Plugins $plugins)
Constructor.
Definition: Locator.php:28