Skip to content

Commit d284a3b

Browse files
committed
依赖注入容器 DI Container
1 parent 8a49a46 commit d284a3b

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

core/App.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Class App
5+
*
6+
* @package \\${NAMESPACE}
7+
*/
8+
class App
9+
{
10+
protected static $registries = [];
11+
12+
public static function bind($key, $value)
13+
{
14+
static::$registries[$key] = $value;
15+
}
16+
17+
public static function get($key)
18+
{
19+
if(!array_key_exists($key, static::$registries))
20+
{
21+
throw new Exception("No {$key} is bound in the container.");
22+
}
23+
24+
return static::$registries[$key];
25+
}
26+
27+
}

core/bootstrap.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<?php
22

3-
$app = [];
4-
5-
// 获取配置
6-
$app['config'] = require ROOT . DS . 'config.php';
73
require 'functions.php';
84

95
/*
@@ -20,6 +16,19 @@
2016
// 类自动加载方法,其他的文件不会自动引入,如function.php
2117
require __DIR__ . '/..' . '/vendor/autoload.php';
2218

23-
$app['database'] = new QueryBuilder(
24-
Connection::make($app['config']['database'])
19+
// $app = [];
20+
21+
// 获取配置
22+
// $app['config'] = require ROOT . DS . 'config.php';
23+
24+
// 这里使用依赖注入优化上边的数组方法
25+
App::bind('config', require ROOT . DS . 'config.php');
26+
27+
/*
28+
$app['database'] = new QueryBuilder(
29+
Connection::make(App::get('config')['database'])
2530
);
31+
*/
32+
App::bind('database', new QueryBuilder(
33+
Connection::make(App::get('config')['database'])
34+
));

vendor/composer/autoload_classmap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
$baseDir = dirname($vendorDir);
77

88
return array(
9+
'App' => $baseDir . '/core/App.php',
10+
'ComposerAutoloaderInit83cb48187cf44a304a7a6be5e700ede3' => $vendorDir . '/composer/autoload_real.php',
11+
'Composer\\Autoload\\ClassLoader' => $vendorDir . '/composer/ClassLoader.php',
12+
'Composer\\Autoload\\ComposerStaticInit83cb48187cf44a304a7a6be5e700ede3' => $vendorDir . '/composer/autoload_static.php',
913
'Connection' => $baseDir . '/core/database/Connection.php',
1014
'QueryBuilder' => $baseDir . '/core/database/QueryBuilder.php',
1115
'Request' => $baseDir . '/core/Request.php',

vendor/composer/autoload_static.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
class ComposerStaticInit83cb48187cf44a304a7a6be5e700ede3
88
{
99
public static $classMap = array (
10+
'App' => __DIR__ . '/../..' . '/core/App.php',
11+
'ComposerAutoloaderInit83cb48187cf44a304a7a6be5e700ede3' => __DIR__ . '/..' . '/composer/autoload_real.php',
12+
'Composer\\Autoload\\ClassLoader' => __DIR__ . '/..' . '/composer/ClassLoader.php',
13+
'Composer\\Autoload\\ComposerStaticInit83cb48187cf44a304a7a6be5e700ede3' => __DIR__ . '/..' . '/composer/autoload_static.php',
1014
'Connection' => __DIR__ . '/../..' . '/core/database/Connection.php',
1115
'QueryBuilder' => __DIR__ . '/../..' . '/core/database/QueryBuilder.php',
1216
'Request' => __DIR__ . '/../..' . '/core/Request.php',

0 commit comments

Comments
 (0)