Completed
Push — master ( 8ddcca...9ec735 )
by Hong
02:37
created

ShareableTrait::setShareable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Shareable;
16
17
/**
18
 * Implementation of ShareableInterface
19
 *
20
 * @package Phossa2\Shared
21
 * @author  Hong Zhang <[email protected]>
22
 * @see     ShareableInterface
23
 * @version 2.0.10
24
 * @since   2.0.10 added
25
 */
26
trait ShareableTrait
27
{
28
    /**
29
     * Shareables' pool
30
     *
31
     * @var    ShareableInstance[]
32
     * @access private
33
     * @staticvar
34
     */
35
    private static $shareables = [];
36
37
    /**
38
     * minimum constructor
39
     *
40
     * @access public
41
     */
42
    public function __construct()
43
    {
44
    }
45
46
    /*
47
     * {@inheritDoc}
48
     */
49
    public static function getShareable(
50
        /*# string */ $scope = '__GLOBAL__'
51
    )/*# : ShareableInterface */ {
52
        $class = get_called_class();
53
        if (!isset(self::$shareables[$scope][$class])) {
54
            static::setShareable(new static(), $scope);
55
        }
56
        return self::$shareables[$class];
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public static function setShareable(
63
        ShareableInterface $instance,
64
        /*# string */ $scope = '__GLOBAL__'
65
    ) {
66
        self::$shareables[$scope][get_called_class()] = $instance;
67
    }
68
69
    /*
70
     * {@inheritDoc}
71
     */
72
    public function isShareable(
73
        /*# string */ $scope = '__GLOBAL__'
74
    )/*# : bool */ {
75
        return $this === static::getShareable($scope);
76
    }
77
}
78