|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Selfoss; |
| 6 | + |
| 7 | +use Bramus\Router\Router; |
| 8 | +use Psr\Container\ContainerInterface; |
| 9 | + |
| 10 | +final class Routes { |
| 11 | + private Router $router; |
| 12 | + private ContainerInterface $container; |
| 13 | + |
| 14 | + public function __construct(Router $router, ContainerInterface $container) { |
| 15 | + $this->router = $router; |
| 16 | + $this->container = $container; |
| 17 | + } |
| 18 | + |
| 19 | + private function setupRoutes(): void { |
| 20 | + // all users |
| 21 | + $this->router->get('/', function(): void { |
| 22 | + // json |
| 23 | + $this->container->get(controllers\Index::class)->home(); |
| 24 | + }); |
| 25 | + $this->router->get('/api/about', function(): void { |
| 26 | + // json |
| 27 | + $this->container->get(controllers\About::class)->about(); |
| 28 | + }); |
| 29 | + $this->router->post('/api/private/hash-password', function(): void { |
| 30 | + // json |
| 31 | + $this->container->get(controllers\Helpers\HashPassword::class)->hash(); |
| 32 | + }); |
| 33 | + $this->router->get('/login', function(): void { |
| 34 | + // json, deprecated |
| 35 | + $this->container->get(controllers\Authentication::class)->login(); |
| 36 | + }); |
| 37 | + $this->router->post('/login', function(): void { |
| 38 | + // json |
| 39 | + $this->container->get(controllers\Authentication::class)->login(); |
| 40 | + }); |
| 41 | + $this->router->get('/logout', function(): void { |
| 42 | + // json, deprecated |
| 43 | + $this->container->get(controllers\Authentication::class)->logout(); |
| 44 | + }); |
| 45 | + $this->router->delete('/api/session/current', function(): void { |
| 46 | + // json |
| 47 | + $this->container->get(controllers\Authentication::class)->logout(); |
| 48 | + }); |
| 49 | + $this->router->get('/update', function(): void { |
| 50 | + // text |
| 51 | + $this->container->get(controllers\Sources\Update::class)->updateAll(); |
| 52 | + }); |
| 53 | + |
| 54 | + // only for loggedin users or on public mode |
| 55 | + $this->router->get('/rss', function(): void { |
| 56 | + // rss |
| 57 | + $this->container->get(controllers\Rss::class)->rss(); |
| 58 | + }); |
| 59 | + $this->router->get('/feed', function(): void { |
| 60 | + // rss |
| 61 | + $this->container->get(controllers\Rss::class)->rss(); |
| 62 | + }); |
| 63 | + $this->router->get('/items', function(): void { |
| 64 | + // json |
| 65 | + $this->container->get(controllers\Items::class)->listItems(); |
| 66 | + }); |
| 67 | + $this->router->get('/tags', function(): void { |
| 68 | + // json |
| 69 | + $this->container->get(controllers\Tags::class)->listTags(); |
| 70 | + }); |
| 71 | + $this->router->get('/stats', function(): void { |
| 72 | + // json |
| 73 | + $this->container->get(controllers\Items\Stats::class)->stats(); |
| 74 | + }); |
| 75 | + $this->router->get('/items/sync', function(): void { |
| 76 | + // json |
| 77 | + $this->container->get(controllers\Items\Sync::class)->sync(); |
| 78 | + }); |
| 79 | + $this->router->get('/sources/stats', function(): void { |
| 80 | + // json |
| 81 | + $this->container->get(controllers\Sources::class)->stats(); |
| 82 | + }); |
| 83 | + |
| 84 | + // only loggedin users |
| 85 | + $this->router->post('/mark/([0-9]+)', function(string $itemId): void { |
| 86 | + // json |
| 87 | + $this->container->get(controllers\Items::class)->mark($itemId); |
| 88 | + }); |
| 89 | + $this->router->post('/mark', function(): void { |
| 90 | + // json |
| 91 | + $this->container->get(controllers\Items::class)->mark(); |
| 92 | + }); |
| 93 | + $this->router->post('/unmark/([0-9]+)', function(string $itemId): void { |
| 94 | + // json |
| 95 | + $this->container->get(controllers\Items::class)->unmark($itemId); |
| 96 | + }); |
| 97 | + $this->router->post('/starr/([0-9]+)', function(string $itemId): void { |
| 98 | + // json |
| 99 | + $this->container->get(controllers\Items::class)->starr($itemId); |
| 100 | + }); |
| 101 | + $this->router->post('/unstarr/([0-9]+)', function(string $itemId): void { |
| 102 | + // json |
| 103 | + $this->container->get(controllers\Items::class)->unstarr($itemId); |
| 104 | + }); |
| 105 | + $this->router->post('/items/sync', function(): void { |
| 106 | + // json |
| 107 | + $this->container->get(controllers\Items\Sync::class)->updateStatuses(); |
| 108 | + }); |
| 109 | + |
| 110 | + $this->router->get('/source/params', function(): void { |
| 111 | + // json |
| 112 | + $this->container->get(controllers\Sources::class)->params(); |
| 113 | + }); |
| 114 | + $this->router->get('/sources', function(): void { |
| 115 | + // json |
| 116 | + $this->container->get(controllers\Sources::class)->show(); |
| 117 | + }); |
| 118 | + $this->router->get('/source', function(): void { |
| 119 | + // json |
| 120 | + $this->container->get(controllers\Sources::class)->add(); |
| 121 | + }); |
| 122 | + $this->router->get('/sources/list', function(): void { |
| 123 | + // json |
| 124 | + $this->container->get(controllers\Sources::class)->listSources(); |
| 125 | + }); |
| 126 | + $this->router->post('/source/((?:new-)?[0-9]+)', function(string $id): void { |
| 127 | + // json |
| 128 | + $this->container->get(controllers\Sources\Write::class)->write($id); |
| 129 | + }); |
| 130 | + $this->router->post('/source', function(): void { |
| 131 | + // json |
| 132 | + $this->container->get(controllers\Sources\Write::class)->write(); |
| 133 | + }); |
| 134 | + $this->router->delete('/source/([0-9]+)', function(string $id): void { |
| 135 | + // json |
| 136 | + $this->container->get(controllers\Sources::class)->remove($id); |
| 137 | + }); |
| 138 | + $this->router->post('/source/delete/([0-9]+)', function(string $id): void { |
| 139 | + // json, deprecated |
| 140 | + $this->container->get(controllers\Sources::class)->remove($id); |
| 141 | + }); |
| 142 | + $this->router->post('/source/([0-9]+)/update', function(string $id): void { |
| 143 | + // json |
| 144 | + $this->container->get(controllers\Sources\Update::class)->update($id); |
| 145 | + }); |
| 146 | + $this->router->get('/sources/spouts', function(): void { |
| 147 | + // json |
| 148 | + $this->container->get(controllers\Sources::class)->spouts(); |
| 149 | + }); |
| 150 | + |
| 151 | + $this->router->post('/tags/color', function(): void { |
| 152 | + // json |
| 153 | + $this->container->get(controllers\Tags::class)->color(); |
| 154 | + }); |
| 155 | + |
| 156 | + $this->router->post('/opml', function(): void { |
| 157 | + // json |
| 158 | + $this->container->get(controllers\Opml\Import::class)->add(); |
| 159 | + }); |
| 160 | + $this->router->get('/opmlexport', function(): void { |
| 161 | + // xml |
| 162 | + $this->container->get(controllers\Opml\Export::class)->export(); |
| 163 | + }); |
| 164 | + |
| 165 | + // Client side routes need to be directed to index.html. |
| 166 | + $this->router->get('/sign/in|/opml|/password|/manage/sources(/add)?|/(newest|unread|starred)(/(all|tag-[^/]+|source-[0-9]+)(/[0-9]+)?)?', function(): void { |
| 167 | + // html |
| 168 | + $this->container->get(controllers\Index::class)->home(); |
| 169 | + }); |
| 170 | + |
| 171 | + $this->router->set404(function(): void { |
| 172 | + header('HTTP/1.1 404 Not Found'); |
| 173 | + echo 'Page not found.'; |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | + public function run(): bool { |
| 178 | + $this->setupRoutes(); |
| 179 | + |
| 180 | + // dispatch |
| 181 | + return $this->router->run(); |
| 182 | + } |
| 183 | +} |
0 commit comments