iiifx-production/lazy-init

此包已被 放弃 并不再维护。未建议替代包。

LazyInit - 懒加载助手

v1.0.3 2016-11-22 16:36 UTC

README

LazyInit - 用于快速创建懒加载方法的助手。

SensioLabsInsight

Latest Version on Packagist Build Status Total Downloads

懒加载 (Lazy initialization) - 程序设计中的技巧,在需要使用某些资源密集型操作(如创建对象、计算值)的结果时才执行这些操作。这样,初始化是在需要时执行,而不是预先执行。

经典使用示例

class DeepThought
{
    protected $answer;

    public function getAnswer ()
    {
        if ( $this->answer === null ) {
            $this->answer = 42;
        }

        return $this->answer;
    }
}

$deepThought = new DeepThought();
echo $deepThought->getAnswer(); # 42

使用 LazyInit 的类似示例

class DeepThought
{
    use \iiifx\LazyInit\LazyInitTrait;

    public function getAnswer ()
    {
        return $this->lazyInit( function () {
            return 42;
        } );
    }
}

$deepThought = new DeepThought();
echo $deepThought->getAnswer(); # 42

安装

使用 Composer

$ composer require "iiifx-production/lazy-init:1.*"

使用

LazyInitTrait 包含 lazyInit() 方法和 $lazyInitData 属性,该属性用于缓存计算结果。适用于动态上下文中的对象。

mixed lazyInit( Closure $container, string|array $dependency = null, array $params = [] )
  • $container - 包含计算并返回结果的闭包容器。
  • $dependency - 用于保存计算结果的字符串、数组或 null - 如果不指定键,则将自动生成。
  • $params - 在启动闭包容器时传递的额外数据。

LazyInitStaticTrait 包含 lazyInitStatic() 方法和 $lazyInitStaticData 属性,该属性用于缓存计算结果。适用于静态上下文中的静态类。

mixed lazyInitStatic( Closure $container, string|array $dependency = null, array $params = [] )

方法参数类似。

方法可以自动根据代码调用点生成缓存数据的键。这是通过使用函数 debug_backtrace() 实现的。

示例

简单的获取器

class Lazy
{
    use \iiifx\LazyInit\LazyInitTrait;

    /**
     * @return string
     */
    public function getDate ()
    {
        return $this->lazyInit( function () {
            return date( 'd.m.Y' );
        }, __METHOD__ );
    }
}

$lazy = new Lazy();
echo $lazy->getDate(); # '12.07.2015'

自动创建键的简单获取器

class Lazy
{
    use \iiifx\LazyInit\LazyInitTrait;

    /**
     * @return string
     */
    public function getMicrotime ()
    {
        return $this->lazyInit( function () {
            return microtime( true );
        } );
    }
}

$lazy = new Lazy();
echo $lazy->getMicrotime(); # 1438928722.9734

依赖于输入值的获取器

class Lazy
{
    use \iiifx\LazyInit\LazyInitTrait;

    /**
     * @param string $string
     *
     * @return mixed[]
     */
    public function parseString ( $string )
    {
        return $this->lazyInit( function () use ( $string ) { # Передаем параметр в замыкание напрямую
            return explode( ':', $string );
        }, [
            __METHOD__,
            $string,
        ] );
    }

    /**
     * @param int $timastamp
     *
     * @return string
     */
    public function formatTimastamp( $timastamp )
    {
        return $this->lazyInit( function ( $t ) {
            return date( 'd.m.Y', $t );
        }, [
            __METHOD__,
            $timastamp,
        ], [
            $timastamp # Передаем параметр как свойство
        ] );
    }
}

$lazy = new Lazy();
var_export( $lazy->parseString( 'A:B:C' ) ); # [ 0 => 'A', 1 => 'B', 2 => 'C' ]
var_export( $lazy->formatTimastamp( time() ) ); # '12.07.2015'

在静态方法中使用

class LazyStatic
{
    use \iiifx\LazyInit\LazyInitStaticTrait;

    /**
     * @return string
     */
    public static function getDate ()
    {
        return self::lazyInitStatic( function () {
            return date( 'd.m.Y' );
        }, __METHOD__ );
    }
}

echo LazyStatic::getDate(); # '12.07.2015'

在类外使用助手

use iiifx\LazyInit\LazyInitHelper;

function buildString( $array )
{
    return LazyInitHelper::lazyInit( function ($v) {
        return implode( '.', $v );
    }, 'build-string', [ $array ] );
}

echo buildString( [ 1, 5, 32 ] ); # '1.5.32'

在创建单例时使用

class Singleton
{
    use \iiifx\LazyInit\LazyInitStaticTrait;

    private function __construct () {}
    private function __clone () {}
    private function __wakeup () {}

    /**
     * @return static
     */
    public static function getInstance ()
    {
        return static::lazyInitStatic( function () {
            return new static();
        }, __METHOD__ );
    }
}
$instance = Singleton::getInstance();

在创建多例时使用

class Multiton
{
    use \iiifx\LazyInit\LazyInitStaticTrait;

    private function __clone () {}
    private function __wakeup () {}

    public $key;

    protected function __construct ( $key )
    {
        $this->key = $key;
    }

    /**
     * @param string $key
     *
     * @return static
     */
    public static function getInstance ( $key )
    {
        return static::lazyInitStatic( function ( $key ) {
            return new static( $key );
        }, $key, [ $key ] );
    }
}

echo Multiton::getInstance( 'master' )->key; # 'master'
echo Multiton::getInstance( 'slave' )->key; # 'slave'
echo Multiton::getInstance( 'master' )->key; # 'master'

测试

Build Status Code Coverage

许可证

Software License