构建自己的PHP框架--创建组件的机制

在之前的博客中,我们完成了基本的Model类,但是大家应该还记得,我们创建数据库的pdo实例时,是hard好的配置,并且直接hard在Model类中。

代码如下:

    public static function getDb()
    {
        if (empty(static::$pdo)) {
            $host = 'localhost';
            $database = 'sf';
            $username = 'jun';
            $password = 'jun';
            $options = [
                PDO::ATTR_EMULATE_PREPARES => false,
                PDO::ATTR_STRINGIFY_FETCHES => false
            ];
            static::$pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password, $options);
            static::$pdo->exec("set names 'utf8'");
        }

        return static::$pdo;
    }

这明显是不合适的,修改的时候需要取修改框架的代码。现在我们就来将它抽出来。

首先我们需要将配置拿到相应的配置文件中,我们在simple-framework文件夹下创建一个config文件夹,然后添加一个db.php文件。在里面添加相关的配置。

代码如下:

return [
    'class' => 'PDO',
    'dsn' => 'mysql:host=localhost;dbname=sf',
    'username' => 'jun',
    'password' => 'jun',
    'options' => [
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::ATTR_STRINGIFY_FETCHES => false,
    ],
];

其实就是返回一个数组,数组中是我们创建pdo实例所需要的参数。在创建pdo实例之前,将db.php文件require进来即可。

代码如下:

    public static function getDb()
    {
        if (empty(static::$pdo)) {
            $config = require('../config/db.php');
            static::$pdo = new $config['class']($config['dsn'], $config['username'], $config['password'], $config['options']);
            static::$pdo->exec("set names 'utf8'");
        }

        return static::$pdo;
    }

这样还是有点不爽,在require配置文件的时候,我需要一层层的去看相对位置,如果错了,就找不到了。而且将来不管是配置文件还是Model文件的位置变动了,相对位置就变了,我都需要修改这段代码。如果改成绝对地址是不是会好一些,至少配置文件位置不变,就不需要修改这段代码。

但是要改成绝对地址,我就需要定位simple-framework文件夹的地址,所以我们在public/index.php中define一个常量,标记simple-framework文件夹的地址。

public/index.php代码如下:

<?php
define('SF_PATH', dirname(__DIR__));
require_once(SF_PATH . '/vendor/autoload.php');

$application = new sfwebApplication();
$application->run();

定义了常量SF_PATH,Model中的require可改为如下代码:

$config = require(SF_PATH . '/config/db.php');

OK,这样看起来好多了。

但是我又想到,假设将来我又需要一个cache的实例,我是不是又要把getDb的方法写一遍呢?如果要避免这种情况,我是不是应该把它抽想出来呢?那要如何抽象呢?

这个时候我们可能需要有一个方法,我们只需要告诉它我们需要创建一个什么实例,然后它就自己去取相应的参数,并创建出相应的实例来。这样是不是很爽。

我们需要在src文件夹下创建一个Sf.php,在其中创建一个createObject方法,用来创建实例。

但这个时候我们又遇到一个问题,pdo实例创建的时候,要按一定的顺序传一定个数的参数,但我们之后需要创建的其它的实例,可能传递的参数的个数和key都不同,那我们该怎么办?

所以我们需要一种机制能够将参数存起来,真正创建实例时,再取出来使用。我们可以在src/db文件夹下创建一个Connection类,Sf中的createObject方法创建了这个类的实例,这个类提供一个创建pdo实例的方法即可。

Sf.php中的代码如下:

<?php
/**
 * Sf is a helper class serving common framework functionalities.
 * @author Harry Sun <sunguangjun@126.com>
 */
class Sf
{
    /**
     * Creates a new object using the given configuration.
     * You may view this method as an enhanced version of the `new` operator.
     * @param string $name the object name
     */
    public static function createObject($name)
    {
        $config = require(SF_PATH . "/config/$name.php");
        // create instance
        $instance = new $config['class']();
        unset($config['class']);
        // add attributes
        foreach ($config as $key => $value) {
            $instance->$key = $value;
        }
        return $instance;
    }
}

Sf类没有namespace,所以不符合psr4的规则,需要手动引入,在public/index.php中加入如下一句话:

require_once(SF_PATH . '/src/Sf.php');

Connection.php中的代码如下:

<?php
namespace sfdb;

use PDO;

/**
 * Connection represents a connection to a database via [PDO](php.net/manual/en/book.pdo.php).
 * @author Harry Sun <sunguangjun@126.com>
 */
class Connection
{
    /**
     * @var string the Data Source Name, or DSN, contains the information required to connect to the database.
     * Please refer to the [PHP manual](http://www.php.net/manual/en/function.PDO-construct.php) on
     * the format of the DSN string.
     * @see charset
     */
    public $dsn;

    /**
     * @var string the username for establishing DB connection. Defaults to `null` meaning no username to use.
     */
    public $username;

    /**
     * @var string the password for establishing DB connection. Defaults to `null` meaning no password to use.
     */
    public $password;

    /**
     * @var array PDO attributes (name => value)
     * to establish a DB connection. Please refer to the
     * [PHP manual](http://www.php.net/manual/en/function.PDO-setAttribute.php) for
     * details about available attributes.
     */
    public $attributes;

    public function getDb()
    {
        return new PDO($this->dsn, $this->username, $this->password, $this->attributes);
    }
}

虽然不显示声明dsn/username/password和attributes属性也是可行的,但我觉得还是声明出来比较好,这样大家看到这个类的时候,会知道有哪些属性。

Model类中的getDb方法改成如下代码:

    public static function getDb()
    {
        if (empty(static::$pdo)) {
            static::$pdo = Sf::createObject('db')->getDb();
            static::$pdo->exec("set names 'utf8'");
        }

        return static::$pdo;
    }

不要忘记需要use一下Sf。

config中的db.php也要跟着修改,代码如下:

<?php
return [
    'class' => 'sfdbConnection',
    'dsn' => 'mysql:host=localhost;dbname=sf',
    'username' => 'jun',
    'password' => 'jun',
    'attributes' => [
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::ATTR_STRINGIFY_FETCHES => false,
    ],
];

我默默的将options换成了attributes,毕竟在PDO的文档里,写的是attribute。

好了,今天就先到这里。项目内容和博客内容也都会放到Github上,欢迎大家提建议。

code:https://github.com/CraryPrimitiveMan/simple-framework/tree/0.8

blog project:https://github.com/CraryPrimitiveMan/create-your-own-php-framework

原文地址:https://www.cnblogs.com/CraryPrimitiveMan/p/5299019.html