Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
objectpool.php
1<?
3
9final class ObjectPool
10{
11 protected $usage = array();
13 protected $objects = array();
14 protected $maxObjectsCount = 0;
15
16 public function __construct($maxObjectsCount = 0)
17 {
18 $this->maxObjectsCount = $maxObjectsCount;
19 }
20
21 public function getObject(array $fields)
22 {
23 $result = null;
24 $index = $this->createIndex($fields);
25
26 if(!isset($this->objects[$index]))
27 {
28 if($this->maxObjectsCount > 0 && count($this->objects) > $this->maxObjectsCount)
29 $this->deleteOutdatedObject();
30
31 $result = $this->createObject($index, $fields);
32 }
33 else
34 {
35 $result = $this->objects[$index];
36 unset($this->usage[array_search($index, $this->usage)]);
37 }
38
39 array_push($this->usage, $index);
40 return $result;
41 }
42
43 protected function createObject($index, array $fields)
44 {
45 $this->objects[$index] = Manager::createObject($fields);
46 return $this->objects[$index];
47 }
48
49 protected function deleteOutdatedObject()
50 {
51 reset($this->usage);
52 unset($this->objects[current($this->usage)]);
53 unset($this->usage[key($this->usage)]);
54 }
55
56 protected function createIndex(array $fields)
57 {
58 return intval($fields['ID']) > 0 ? intval($fields['ID']) : md5(serialize($fields));
59 }
60}
static createObject(array $srvParams)
Definition manager.php:367