Elgg  Version 3.0
Locator.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrade;
4 
6 use Elgg\Includer;
7 use Elgg\Loggable;
9 use ElggUpgrade;
11 
20 class Locator {
21 
22  use Loggable;
23 
27  private $plugins;
28 
32  private $logger;
33 
40  public function __construct(Plugins $plugins, LoggerInterface $logger) {
41  $this->plugins = $plugins;
42  $this->logger = $logger;
43  }
44 
50  public function locate() {
51  $pending_upgrades = [];
52 
53  // Check for core upgrades
54  $core_upgrades = Includer::includeFile(Paths::elgg() . 'engine/upgrades.php');
55 
56  foreach ($core_upgrades as $class) {
57  $upgrade = $this->getUpgrade($class, 'core');
58  if ($upgrade) {
59  $pending_upgrades[] = $upgrade;
60  }
61  }
62 
63  $plugins = $this->plugins->find('active');
64 
65  // Check for plugin upgrades
66  foreach ($plugins as $plugin) {
67  $batches = $plugin->getStaticConfig('upgrades');
68 
69  if (empty($batches)) {
70  continue;
71  }
72 
73  $plugin_id = $plugin->getID();
74 
75  foreach ($batches as $class) {
76  $upgrade = $this->getUpgrade($class, $plugin_id);
77  if ($upgrade) {
78  $pending_upgrades[] = $upgrade;
79  }
80  }
81  }
82 
83  return $pending_upgrades;
84  }
85 
94  public function getUpgrade($class, $component_id) {
95 
96  $batch = $this->getBatch($class);
97 
98  $version = $batch->getVersion();
99  $upgrade_id = "{$component_id}:{$version}";
100 
101  $upgrade = $this->upgradeExists($upgrade_id);
102 
103  if (!$upgrade) {
104  $upgrade = elgg_call(ELGG_IGNORE_ACCESS, function () use ($upgrade_id, $class, $component_id, $version) {
106 
107  // Create a new ElggUpgrade to represent the upgrade in the database
108  $upgrade = new ElggUpgrade();
109  $upgrade->owner_guid = $site->guid;
110  $upgrade->container_guid = $site->guid;
111 
112  $upgrade->setId($upgrade_id);
113  $upgrade->setClass($class);
114  $upgrade->title = "{$component_id}:upgrade:{$version}:title";
115  $upgrade->description = "{$component_id}:upgrade:{$version}:description";
116  $upgrade->offset = 0;
117  $upgrade->save();
118 
119  return $upgrade;
120  });
121  }
122 
123  return $upgrade;
124  }
125 
133  public function getBatch($class) {
134  if (!class_exists($class)) {
135  throw new \InvalidArgumentException("Upgrade class $class was not found");
136  }
137 
138  if (!is_subclass_of($class, Batch::class)) {
139  throw new \InvalidArgumentException("Upgrade class $class should implement " . Batch::class);
140  }
141 
142  return new $class;
143  }
144 
152  public function upgradeExists($upgrade_id) {
153  return elgg_call(ELGG_IGNORE_ACCESS, function () use ($upgrade_id) {
154  $upgrades = \Elgg\Database\Entities::find([
155  'type' => 'object',
156  'subtype' => 'elgg_upgrade',
157  'private_setting_name_value_pairs' => [
158  [
159  'name' => 'id',
160  'value' => (string) $upgrade_id,
161  ],
162  ],
163  ]);
164 
165  return $upgrades ? $upgrades[0] : false;
166  });
167  }
168 }
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:1176
static includeFile($file)
Include a file with as little context as possible.
Definition: Includer.php:18
__construct(Plugins $plugins, LoggerInterface $logger)
Constructor.
Definition: Locator.php:40
$plugin
static elgg()
Get the Elgg codebase path with "/".
Definition: Paths.php:44
Locates and registers both core and plugin upgrades.
Definition: Locator.php:20
trait Loggable
Enables adding a logger.
Definition: Loggable.php:12
$upgrades
Lists pending upgrades.
Definition: upgrades.php:11
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:156
$upgrade
Definition: upgrade.php:8
$class
Definition: field.php:29
$plugin_id
Definition: save.php:15
locate()
Looks for upgrades and saves them as ElggUpgrade entities.
Definition: Locator.php:50
getUpgrade($class, $component_id)
Gets intance of an ElggUpgrade based on the given class and id.
Definition: Locator.php:94
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:130
class
Definition: placeholder.php:21
upgradeExists($upgrade_id)
Check if there already is an ElggUpgrade for this upgrade.
Definition: Locator.php:152
Persistent, installation-wide key-value storage.
Definition: Plugins.php:36
$version
Definition: version.php:14
getBatch($class)
Validates class and returns an instance of batch.
Definition: Locator.php:133