diff options
author | Arthur <arthur@hoa.ro> | 2016-12-20 11:30:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-20 11:30:05 +0100 |
commit | 80677a23e2e10d78bc527e9754286787b453ce61 (patch) | |
tree | 18feefc47f389171f3886b191fb14f2ace6d0175 /application/api/controllers | |
parent | e350aa750f9e9e742bb60a1e04ebd9e21f763c78 (diff) | |
parent | 18e6796726d73d7dc90ecdd16c181493941f5487 (diff) | |
download | Shaarli-80677a23e2e10d78bc527e9754286787b453ce61.tar.gz Shaarli-80677a23e2e10d78bc527e9754286787b453ce61.tar.zst Shaarli-80677a23e2e10d78bc527e9754286787b453ce61.zip |
Merge pull request #666 from ArthurHoaro/slim-api
REST API structure using Slim framework
Diffstat (limited to 'application/api/controllers')
-rw-r--r-- | application/api/controllers/ApiController.php | 54 | ||||
-rw-r--r-- | application/api/controllers/Info.php | 42 |
2 files changed, 96 insertions, 0 deletions
diff --git a/application/api/controllers/ApiController.php b/application/api/controllers/ApiController.php new file mode 100644 index 00000000..1dd47f17 --- /dev/null +++ b/application/api/controllers/ApiController.php | |||
@@ -0,0 +1,54 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Api\Controllers; | ||
4 | |||
5 | use \Slim\Container; | ||
6 | |||
7 | /** | ||
8 | * Abstract Class ApiController | ||
9 | * | ||
10 | * Defines REST API Controller dependencies injected from the container. | ||
11 | * | ||
12 | * @package Api\Controllers | ||
13 | */ | ||
14 | abstract class ApiController | ||
15 | { | ||
16 | /** | ||
17 | * @var Container | ||
18 | */ | ||
19 | protected $ci; | ||
20 | |||
21 | /** | ||
22 | * @var \ConfigManager | ||
23 | */ | ||
24 | protected $conf; | ||
25 | |||
26 | /** | ||
27 | * @var \LinkDB | ||
28 | */ | ||
29 | protected $linkDb; | ||
30 | |||
31 | /** | ||
32 | * @var int|null JSON style option. | ||
33 | */ | ||
34 | protected $jsonStyle; | ||
35 | |||
36 | /** | ||
37 | * ApiController constructor. | ||
38 | * | ||
39 | * Note: enabling debug mode displays JSON with readable formatting. | ||
40 | * | ||
41 | * @param Container $ci Slim container. | ||
42 | */ | ||
43 | public function __construct(Container $ci) | ||
44 | { | ||
45 | $this->ci = $ci; | ||
46 | $this->conf = $ci->get('conf'); | ||
47 | $this->linkDb = $ci->get('db'); | ||
48 | if ($this->conf->get('dev.debug', false)) { | ||
49 | $this->jsonStyle = JSON_PRETTY_PRINT; | ||
50 | } else { | ||
51 | $this->jsonStyle = null; | ||
52 | } | ||
53 | } | ||
54 | } | ||
diff --git a/application/api/controllers/Info.php b/application/api/controllers/Info.php new file mode 100644 index 00000000..25433f72 --- /dev/null +++ b/application/api/controllers/Info.php | |||
@@ -0,0 +1,42 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Api\Controllers; | ||
4 | |||
5 | use Slim\Http\Request; | ||
6 | use Slim\Http\Response; | ||
7 | |||
8 | /** | ||
9 | * Class Info | ||
10 | * | ||
11 | * REST API Controller: /info | ||
12 | * | ||
13 | * @package Api\Controllers | ||
14 | * @see http://shaarli.github.io/api-documentation/#links-instance-information-get | ||
15 | */ | ||
16 | class Info extends ApiController | ||
17 | { | ||
18 | /** | ||
19 | * Service providing various information about Shaarli instance. | ||
20 | * | ||
21 | * @param Request $request Slim request. | ||
22 | * @param Response $response Slim response. | ||
23 | * | ||
24 | * @return Response response. | ||
25 | */ | ||
26 | public function getInfo($request, $response) | ||
27 | { | ||
28 | $info = [ | ||
29 | 'global_counter' => count($this->linkDb), | ||
30 | 'private_counter' => count_private($this->linkDb), | ||
31 | 'settings' => array( | ||
32 | 'title' => $this->conf->get('general.title', 'Shaarli'), | ||
33 | 'header_link' => $this->conf->get('general.header_link', '?'), | ||
34 | 'timezone' => $this->conf->get('general.timezone', 'UTC'), | ||
35 | 'enabled_plugins' => $this->conf->get('general.enabled_plugins', []), | ||
36 | 'default_private_links' => $this->conf->get('privacy.default_private_links', false), | ||
37 | ), | ||
38 | ]; | ||
39 | |||
40 | return $response->withJson($info, 200, $this->jsonStyle); | ||
41 | } | ||
42 | } | ||