]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/updater/Updater.php
95654d81da96c0d23c2d26424c47983b39c25961
[github/shaarli/Shaarli.git] / application / updater / Updater.php
1 <?php
2
3 namespace Shaarli\Updater;
4
5 use Shaarli\Config\ConfigManager;
6 use Shaarli\Bookmark\BookmarkServiceInterface;
7 use Shaarli\Updater\Exception\UpdaterException;
8
9 /**
10 * Class Updater.
11 * Used to update stuff when a new Shaarli's version is reached.
12 * Update methods are ran only once, and the stored in a TXT file.
13 */
14 class Updater
15 {
16 /**
17 * @var array Updates which are already done.
18 */
19 protected $doneUpdates;
20
21 /**
22 * @var BookmarkServiceInterface instance.
23 */
24 protected $linkServices;
25
26 /**
27 * @var ConfigManager $conf Configuration Manager instance.
28 */
29 protected $conf;
30
31 /**
32 * @var bool True if the user is logged in, false otherwise.
33 */
34 protected $isLoggedIn;
35
36 /**
37 * @var \ReflectionMethod[] List of current class methods.
38 */
39 protected $methods;
40
41 /**
42 * Object constructor.
43 *
44 * @param array $doneUpdates Updates which are already done.
45 * @param BookmarkServiceInterface $linkDB LinksService instance.
46 * @param ConfigManager $conf Configuration Manager instance.
47 * @param boolean $isLoggedIn True if the user is logged in.
48 */
49 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
50 {
51 $this->doneUpdates = $doneUpdates;
52 $this->linkServices = $linkDB;
53 $this->conf = $conf;
54 $this->isLoggedIn = $isLoggedIn;
55
56 // Retrieve all update methods.
57 $class = new \ReflectionClass($this);
58 $this->methods = $class->getMethods();
59 }
60
61 /**
62 * Run all new updates.
63 * Update methods have to start with 'updateMethod' and return true (on success).
64 *
65 * @return array An array containing ran updates.
66 *
67 * @throws UpdaterException If something went wrong.
68 */
69 public function update()
70 {
71 $updatesRan = array();
72
73 // If the user isn't logged in, exit without updating.
74 if ($this->isLoggedIn !== true) {
75 return $updatesRan;
76 }
77
78 if ($this->methods === null) {
79 throw new UpdaterException('Couldn\'t retrieve LegacyUpdater class methods.');
80 }
81
82 foreach ($this->methods as $method) {
83 // Not an update method or already done, pass.
84 if (! startsWith($method->getName(), 'updateMethod')
85 || in_array($method->getName(), $this->doneUpdates)
86 ) {
87 continue;
88 }
89
90 try {
91 $method->setAccessible(true);
92 $res = $method->invoke($this);
93 // Update method must return true to be considered processed.
94 if ($res === true) {
95 $updatesRan[] = $method->getName();
96 }
97 } catch (\Exception $e) {
98 throw new UpdaterException($method, $e);
99 }
100 }
101
102 $this->doneUpdates = array_merge($this->doneUpdates, $updatesRan);
103
104 return $updatesRan;
105 }
106
107 /**
108 * @return array Updates methods already processed.
109 */
110 public function getDoneUpdates()
111 {
112 return $this->doneUpdates;
113 }
114 }