]>
Commit | Line | Data |
---|---|---|
510377d2 | 1 | <?php |
f24896b2 | 2 | |
bcf056c9 V |
3 | namespace Shaarli\Updater; |
4 | ||
cf92b4dd | 5 | use Shaarli\Bookmark\BookmarkServiceInterface; |
485b168a | 6 | use Shaarli\Config\ConfigManager; |
bcf056c9 | 7 | use Shaarli\Updater\Exception\UpdaterException; |
510377d2 A |
8 | |
9 | /** | |
cf92b4dd | 10 | * Class Updater. |
510377d2 | 11 | * Used to update stuff when a new Shaarli's version is reached. |
cf92b4dd | 12 | * Update methods are ran only once, and the stored in a TXT file. |
510377d2 A |
13 | */ |
14 | class Updater | |
15 | { | |
16 | /** | |
17 | * @var array Updates which are already done. | |
18 | */ | |
19 | protected $doneUpdates; | |
20 | ||
510377d2 | 21 | /** |
cf92b4dd | 22 | * @var BookmarkServiceInterface instance. |
510377d2 | 23 | */ |
cf92b4dd | 24 | protected $linkServices; |
510377d2 | 25 | |
278d9ee2 A |
26 | /** |
27 | * @var ConfigManager $conf Configuration Manager instance. | |
28 | */ | |
29 | protected $conf; | |
30 | ||
510377d2 A |
31 | /** |
32 | * @var bool True if the user is logged in, false otherwise. | |
33 | */ | |
34 | protected $isLoggedIn; | |
35 | ||
28f26524 | 36 | /** |
cf92b4dd | 37 | * @var \ReflectionMethod[] List of current class methods. |
510377d2 A |
38 | */ |
39 | protected $methods; | |
40 | ||
41 | /** | |
42 | * Object constructor. | |
43 | * | |
cf92b4dd A |
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. | |
510377d2 | 48 | */ |
cf92b4dd | 49 | public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn) |
510377d2 A |
50 | { |
51 | $this->doneUpdates = $doneUpdates; | |
cf92b4dd | 52 | $this->linkServices = $linkDB; |
278d9ee2 | 53 | $this->conf = $conf; |
510377d2 A |
54 | $this->isLoggedIn = $isLoggedIn; |
55 | ||
56 | // Retrieve all update methods. | |
cf92b4dd | 57 | $class = new \ReflectionClass($this); |
510377d2 A |
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 | ||
ee6f4b64 | 78 | if ($this->methods === null) { |
cf92b4dd | 79 | throw new UpdaterException('Couldn\'t retrieve LegacyUpdater class methods.'); |
510377d2 A |
80 | } |
81 | ||
82 | foreach ($this->methods as $method) { | |
83 | // Not an update method or already done, pass. | |
cf92b4dd | 84 | if (! startsWith($method->getName(), 'updateMethod') |
510377d2 A |
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 | } | |
cf92b4dd | 97 | } catch (\Exception $e) { |
510377d2 A |
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 | } | |
485b168a A |
114 | |
115 | /** | |
116 | * With the Slim routing system, default header link should be `./` instead of `?`. | |
117 | * Otherwise you can not go back to the home page. Example: `/picture-wall` -> `/picture-wall?` instead of `/`. | |
118 | */ | |
119 | public function updateMethodRelativeHomeLink(): bool | |
120 | { | |
121 | $link = trim($this->conf->get('general.header_link')); | |
122 | if ($link[0] === '?') { | |
123 | $link = './'. ltrim($link, '?'); | |
124 | ||
125 | $this->conf->set('general.header_link', $link, true, true); | |
126 | } | |
127 | ||
128 | return true; | |
129 | } | |
510377d2 | 130 | } |