Skip to content

Commit e87b9a6

Browse files
authored
Merge pull request #632 from zghosts/di-container
Implement pimple as a psr11 compatible di container
2 parents b15826f + e836584 commit e87b9a6

13 files changed

+640
-36
lines changed

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
"source" : "https://github.com/joindin/joindin-api"
1111
},
1212
"require" : {
13-
"ext-pdo": "*",
1413
"ext-json": "*",
15-
"swiftmailer/swiftmailer": "^v5.4.9",
16-
"michelf/php-markdown": "^1.8",
14+
"ext-pdo": "*",
1715
"guzzlehttp/guzzle": "^4.2.4",
18-
"guzzlehttp/oauth-subscriber": "0.1.*"
16+
"guzzlehttp/oauth-subscriber": "0.1.*",
17+
"michelf/php-markdown": "^1.8",
18+
"pimple/pimple": "^3.2",
19+
"swiftmailer/swiftmailer": "^v5.4.9"
1920
},
2021
"require-dev": {
2122
"exussum12/coverage-checker": "^0.11.0",

composer.lock

Lines changed: 102 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/controllers/ContactController.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@
55
*/
66
class ContactController extends BaseApiController
77
{
8+
/**
9+
* @var ContactEmailService
10+
*/
11+
private $emailService;
12+
13+
/**
14+
* @var SpamCheckService
15+
*/
16+
private $spamCheckService;
17+
18+
/**
19+
* ContactController constructor.
20+
*
21+
* @param ContactEmailService $emailService
22+
* @param SpamCheckServiceInterface $spamCheckService
23+
* @param array $config
24+
*/
25+
public function __construct(
26+
ContactEmailService $emailService,
27+
SpamCheckServiceInterface $spamCheckService,
28+
array $config = []
29+
) {
30+
$this->emailService = $emailService;
31+
$this->spamCheckService = $spamCheckService;
32+
33+
parent::__construct($config);
34+
}
35+
836
public function handle(Request $request, PDO $db)
937
{
1038
// really need to not require this to be declared
@@ -32,20 +60,22 @@ public function contact(Request $request, PDO $db)
3260
// only trusted clients can contact us to save on spam
3361
$clientId = $request->getParameter('client_id');
3462
$clientSecret = $request->getParameter('client_secret');
35-
$this->oauthModel = $request->getOauthModel($db);
36-
if (! $this->oauthModel->isClientPermittedPasswordGrant($clientId, $clientSecret)) {
63+
$oauthModel = $request->getOauthModel($db);
64+
if (! $oauthModel->isClientPermittedPasswordGrant($clientId, $clientSecret)) {
3765
throw new Exception("This client cannot perform this action", 403);
3866
}
3967

4068
$fields = ['name', 'email', 'subject', 'comment'];
4169
$error = [];
70+
$data = [];
4271
foreach ($fields as $name) {
4372
$value = $request->getParameter($name);
4473
if (empty($value)) {
4574
$error[] = "'$name'";
4675
}
4776
$data[$name] = $value;
4877
}
78+
4979
if (! empty($error)) {
5080
$message = 'The field';
5181
$message .= count($error) == 1 ? ' ' : 's ';
@@ -55,24 +85,15 @@ public function contact(Request $request, PDO $db)
5585
throw new Exception($message, 400);
5686
}
5787

58-
// run it by akismet if we have it
59-
if (isset($this->config['akismet']['apiKey'], $this->config['akismet']['blog'])) {
60-
$spamCheckService = new SpamCheckService(
61-
$this->config['akismet']['apiKey'],
62-
$this->config['akismet']['blog']
63-
);
64-
$isValid = $spamCheckService->isCommentAcceptable(
65-
$data['comment'],
66-
$request->getClientIP(),
67-
$request->getClientUserAgent()
68-
);
69-
if (!$isValid) {
70-
throw new Exception("Comment failed spam check", 400);
71-
}
88+
if (!$this->spamCheckService->isCommentAcceptable(
89+
$data,
90+
$request->getClientIP(),
91+
$request->getClientUserAgent()
92+
)) {
93+
throw new Exception("Comment failed spam check", 400);
7294
}
7395

74-
$emailService = new ContactEmailService($this->config);
75-
$emailService->sendEmail($data);
96+
$this->emailService->sendEmail($data);
7697

7798
$view = $request->getView();
7899

src/inc/ContainerFactory.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
use Pimple\Container;
4+
5+
class ContainerFactory
6+
{
7+
/**
8+
* @var \Psr\Container\ContainerInterface
9+
*/
10+
private static $container;
11+
12+
/**
13+
* Builds a Psr11 compatible container
14+
*
15+
* @param array $config
16+
* @param bool $rebuild
17+
*
18+
* @return \Psr\Container\ContainerInterface
19+
*/
20+
public static function build(array $config, $rebuild = false)
21+
{
22+
if (!isset(static::$container) || $rebuild) {
23+
$container = new Container();
24+
25+
$container[SpamCheckServiceInterface::class] = function ($c) {
26+
return new NullSpamCheckService();
27+
};
28+
29+
//add $config
30+
if (isset($config['akismet']['apiKey'], $config['akismet']['blog'])) {
31+
$container[SpamCheckServiceInterface::class] = function ($c) use ($config) {
32+
return new SpamCheckService(
33+
$config['akismet']['apiKey'],
34+
$config['akismet']['blog']
35+
);
36+
};
37+
}
38+
39+
$container[ContactEmailService::class] = function ($c) use ($config) {
40+
return new ContactEmailService($config);
41+
};
42+
43+
$container[ContactController::class] = $container->factory(function (Container $c) use ($config) {
44+
return new ContactController(
45+
$c[ContactEmailService::class],
46+
$c[SpamCheckServiceInterface::class],
47+
$config
48+
);
49+
});
50+
51+
$container[ApplicationsController::class] = $container->factory(function ($c) use ($config) {
52+
return new ApplicationsController($config);
53+
});
54+
55+
$container[DefaultController::class] = $container->factory(function ($c) use ($config) {
56+
return new DefaultController($config);
57+
});
58+
59+
$container[EmailsController::class] = $container->factory(function ($c) use ($config) {
60+
return new EmailsController($config);
61+
});
62+
63+
$container[Event_commentsController::class] = $container->factory(function ($c) use ($config) {
64+
return new Event_commentsController($config);
65+
});
66+
67+
$container[Event_hostsController::class] = $container->factory(function ($c) use ($config) {
68+
return new Event_hostsController($config);
69+
});
70+
71+
$container[EventImagesController::class] = $container->factory(function ($c) use ($config) {
72+
return new EventImagesController($config);
73+
});
74+
75+
$container[EventsController::class] = $container->factory(function ($c) use ($config) {
76+
return new EventsController($config);
77+
});
78+
79+
$container[FacebookController::class] = $container->factory(function ($c) use ($config) {
80+
return new FacebookController($config);
81+
});
82+
83+
$container[LanguagesController::class] = $container->factory(function ($c) use ($config) {
84+
return new LanguagesController($config);
85+
});
86+
87+
$container[Talk_commentsController::class] = $container->factory(function ($c) use ($config) {
88+
return new Talk_commentsController($config);
89+
});
90+
91+
$container[TalkLinkController::class] = $container->factory(function ($c) use ($config) {
92+
return new TalkLinkController($config);
93+
});
94+
95+
$container[TalksController::class] = $container->factory(function ($c) use ($config) {
96+
return new TalksController($config);
97+
});
98+
99+
$container[TalkTypesController::class] = $container->factory(function ($c) use ($config) {
100+
return new TalkTypesController($config);
101+
});
102+
103+
$container[TokenController::class] = $container->factory(function ($c) use ($config) {
104+
return new TokenController($config);
105+
});
106+
107+
$container[TracksController::class] = $container->factory(function ($c) use ($config) {
108+
return new TracksController($config);
109+
});
110+
111+
$container[TwitterController::class] = $container->factory(function ($c) use ($config) {
112+
return new TwitterController($config);
113+
});
114+
115+
$container[UsersController::class] = $container->factory(function ($c) use ($config) {
116+
return new UsersController($config);
117+
});
118+
119+
static::$container = new \Pimple\Psr11\Container($container);
120+
}
121+
122+
return static::$container;
123+
}
124+
}

src/public/index.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ function handle_exception($e)
3232
// config setup
3333
define('BASEPATH', '.');
3434
include '../config.php';
35+
36+
$container = ContainerFactory::build($config);
37+
3538
if ($config['mode'] == "development") {
3639
ini_set("html_errors", 0);
3740
}
@@ -65,7 +68,7 @@ function handle_exception($e)
6568
$router = new ApiRouter($config, $routers, ['2']);
6669

6770
$route = $router->getRoute($request);
68-
$return_data = $route->dispatch($request, $ji_db, $config);
71+
$return_data = $route->dispatch($request, $ji_db, $container);
6972

7073
if ($return_data && isset($request->user_id)) {
7174
$return_data['meta']['user_uri'] = $request->base . '/' . $request->version . '/users/' . $request->user_id;

0 commit comments

Comments
 (0)