Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 13c4da7

Browse files
committed
some update
1 parent 7b1ec0f commit 13c4da7

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

src/DI/Container.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ public function createCallback($target, array $arguments = [], array $props = []
375375
*/
376376
public function get($id)
377377
{
378+
// a class name. get from object pool
379+
if (strpos($id, '\\')) {
380+
return Obj::get($id);
381+
}
382+
378383
return $this->getInstance($id);
379384
}
380385

src/Helpers/Obj.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88

99
namespace Inhere\Library\Helpers;
1010

11+
use Inhere\Library\Traits\ObjectPoolTrait;
12+
1113
/**
1214
* Class Obj
1315
* alias of the ObjectHelper
1416
* @package Inhere\Library\Helpers
1517
*/
1618
class Obj extends ObjectHelper
1719
{
20+
use ObjectPoolTrait;
1821
}

src/Traits/ObjectPoolTrait.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-09-19
6+
* Time: 17:02
7+
*/
8+
9+
namespace Inhere\Library\Traits;
10+
11+
/**
12+
* Class ObjectPoolTrait
13+
* @package Inhere\Library\Traits
14+
*/
15+
trait ObjectPoolTrait
16+
{
17+
/**
18+
* @var \SplStack[] [class => \SplStack]
19+
*/
20+
private static $pool = [];
21+
22+
/**
23+
* @param string $class
24+
* @return mixed
25+
*/
26+
public static function get(string $class)
27+
{
28+
$stack = self::getStack($class);
29+
30+
if (!$stack->isEmpty()) {
31+
return $stack->shift();
32+
}
33+
34+
return new $class;
35+
}
36+
37+
/**
38+
* @param \stdClass|string $object
39+
*/
40+
public static function put($object)
41+
{
42+
if (is_string($object)) {
43+
$object = new $object;
44+
}
45+
46+
self::getStack($object)->push($object);
47+
}
48+
49+
/**
50+
* @param string $class
51+
* @param \Closure $handler
52+
* @return mixed
53+
*/
54+
public static function use($class, \Closure $handler)
55+
{
56+
$obj = self::get($class);
57+
58+
$ret = $handler($obj);
59+
60+
self::put($obj);
61+
62+
return $ret;
63+
}
64+
65+
/**
66+
* @param string|\stdClass $class
67+
* @return \SplStack
68+
*/
69+
public static function getStack($class)
70+
{
71+
$class = is_string($class) ? $class : get_class($class);
72+
73+
if (!isset(self::$pool[$class])) {
74+
self::$pool[$class] = new \SplStack();
75+
}
76+
77+
return self::$pool[$class];
78+
}
79+
80+
/**
81+
* @param null $class
82+
* @return int
83+
*/
84+
public static function count($class = null)
85+
{
86+
if ($class) {
87+
if (!isset(self::$pool[$class])) {
88+
throw new \InvalidArgumentException("The object is never created of the class: $class");
89+
}
90+
91+
return self::$pool[$class]->count();
92+
}
93+
94+
return count(self::$pool);
95+
}
96+
97+
/**
98+
* @param null $class
99+
*/
100+
public static function destroy($class = null)
101+
{
102+
if ($class) {
103+
if (!isset(self::$pool[$class])) {
104+
throw new \InvalidArgumentException("The object is never created of the class: $class");
105+
}
106+
107+
unset(self::$pool[$class]);
108+
} else {
109+
self::$pool = [];
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)