Elgg  Version 4.3
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 
121  public function getBatch($class) {
122  if (!class_exists($class)) {
123  throw new InvalidArgumentException("Upgrade class $class was not found");
124  }
125 
126  if (!is_subclass_of($class, Batch::class)) {
127  throw new InvalidArgumentException("Upgrade class $class should implement " . Batch::class);
128  }
129 
130  return new $class;
131  }
132 
140  public function upgradeExists($upgrade_id) {
141  return elgg_call(ELGG_IGNORE_ACCESS, function () use ($upgrade_id) {
142  $upgrades = \Elgg\Database\Entities::find([
143  'type' => 'object',
144  'subtype' => 'elgg_upgrade',
145  'private_setting_name_value_pairs' => [
146  [
147  'name' => 'id',
148  'value' => (string) $upgrade_id,
149  ],
150  ],
151  ]);
152 
153  return $upgrades ? $upgrades[0] : false;
154  });
155  }
156 
166  public function getUpgradeByClass(string $class_name) {
167  return elgg_call(ELGG_IGNORE_ACCESS, function () use ($class_name) {
168  $upgrades = \Elgg\Database\Entities::find([
169  'type' => 'object',
170  'subtype' => 'elgg_upgrade',
171  'private_setting_name_value_pairs' => [
172  [
173  'name' => 'class',
174  'value' => $class_name,
175  ],
176  ],
177  ]);
178 
179  return $upgrades ? $upgrades[0] : false;
180  });
181  }
182 }
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:592
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
$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:146
if(empty($guid)) $upgrade
Definition: upgrade.php:11
getUpgradeByClass(string $class_name)
Check if there already is an ElggUpgrade for this upgrade class.
Definition: Locator.php:166
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:99
upgradeExists($upgrade_id)
Check if there already is an ElggUpgrade for this upgrade.
Definition: Locator.php:140
Persistent, installation-wide key-value storage.
Definition: Plugins.php:28
getBatch($class)
Validates class and returns an instance of batch.
Definition: Locator.php:121
__construct(Plugins $plugins)
Constructor.
Definition: Locator.php:28