Skip to content

Commit ab4ff2c

Browse files
authored
Merge pull request #5 from CottaCush/features/dependency-injection
Integrate dependency injection (DI)
2 parents c6ffc1d + 1228ef5 commit ab4ff2c

File tree

7 files changed

+65
-8
lines changed

7 files changed

+65
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
#### 1.4.0
4+
5+
* Add dependency injection (DI) *2016-10-19*
6+
7+
38
#### 1.3.1
49

510
* Fix issues with deployer REPO_URL and SLACK_HOOK_URL *2016-09-16*

app/config/services.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
/**
3+
* @author Adegoke Obasa <[email protected]>
4+
*/
5+
Yii::$container->set('app\services\DummyServiceInterface', 'app\services\DummyService');

app/controllers/SiteController.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
namespace app\controllers;
44

5+
use app\models\ContactForm;
6+
use app\models\LoginForm;
7+
use app\services\DummyServiceInterface;
58
use Yii;
69
use yii\filters\AccessControl;
710
use yii\filters\VerbFilter;
8-
use app\models\LoginForm;
9-
use app\models\ContactForm;
1011

1112
class SiteController extends BaseController
1213
{
14+
/** @var DummyServiceInterface */
15+
protected $dummyService;
16+
17+
public function __construct($id, $module, DummyServiceInterface $dummyService, $config = [])
18+
{
19+
$this->dummyService = $dummyService;
20+
parent::__construct($id, $module, $config);
21+
}
22+
1323
public function behaviors()
1424
{
1525
return [
@@ -51,6 +61,11 @@ public function actionIndex()
5161
return $this->render('index');
5262
}
5363

64+
public function actionServiceTest()
65+
{
66+
return $this->dummyService->shout("Hello World");
67+
}
68+
5469
public function actionLogin()
5570
{
5671
if (!\Yii::$app->user->isGuest) {
@@ -88,8 +103,9 @@ public function actionContact()
88103

89104
public function actionAbout()
90105
{
91-
if($this->getUser()->isGuest)
106+
if ($this->getUser()->isGuest) {
92107
return $this->getUser()->loginRequired();
108+
}
93109
return $this->render('about');
94110
}
95111
}

app/services/DummyService.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* @author Adegoke Obasa <[email protected]>
4+
*/
5+
6+
namespace app\services;
7+
8+
use yii\base\Object;
9+
10+
class DummyService extends Object implements DummyServiceInterface
11+
{
12+
public function shout($text)
13+
{
14+
return $text;
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* @author Adegoke Obasa <[email protected]>
4+
*/
5+
6+
namespace app\services;
7+
8+
interface DummyServiceInterface
9+
{
10+
public function shout($text);
11+
}

app/web/index.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
$envs = ['development', 'staging', 'production'];
1818
$env = getenv("APPLICATION_ENV");
1919

20-
if(!in_array($env, $envs)) {
20+
if (!in_array($env, $envs)) {
2121
die("Environment is not valid");
2222
}
2323

2424
// Only show debug toolbar and allow gii when in development
25-
if($env == "development") {
25+
if ($env == "development") {
2626
defined('YII_DEBUG') or define('YII_DEBUG', true);
2727
defined('YII_ENV') or define('YII_ENV', 'dev');
2828
}
@@ -32,6 +32,10 @@
3232
// Require configuration file
3333
$config = require(__DIR__ . "/../config/web.php");
3434

35+
36+
// Service Dependencies
37+
require(__DIR__ . "/../config/services.php");
38+
3539
// Change this to your timezone
36-
ini_set( 'date.timezone', 'Africa/Lagos');
37-
(new yii\web\Application($config))->run();
40+
ini_set('date.timezone', 'Africa/Lagos');
41+
(new yii\web\Application($config))->run();

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cottacush/yii2-base-project",
33
"description": "A Yii 2 Base Project Template",
44
"keywords": ["yii2", "framework", "basic", "project template", "improved"],
5-
"version": "1.3.1",
5+
"version": "1.4.0",
66
"type": "project",
77
"license": "MIT",
88
"minimum-stability": "stable",

0 commit comments

Comments
 (0)