Skip to content

Commit 15ec877

Browse files
committed
Move routes to a class
1 parent aa1836e commit 15ec877

File tree

2 files changed

+188
-167
lines changed

2 files changed

+188
-167
lines changed

index.php

Lines changed: 5 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -2,171 +2,9 @@
22

33
declare(strict_types=1);
44

5-
use Psr\Container\ContainerInterface;
6-
use Selfoss\controllers;
5+
require __DIR__ . '/vendor/autoload.php';
76

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

src/Routes.php

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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

Comments
 (0)