Bitrix-D7  20.0.0
forum/lib/internals/entityfabric.php
См. документацию.
1 <?php
2 namespace Bitrix\Forum\Internals;
3 trait EntityFabric
4 {
5  protected static $repo = [];
6 
7  /**
8  * @param $id
9  * @return static
10  */
11  public static function getById(int $id)
12  {
13  if (!array_key_exists(__CLASS__, self::$repo))
14  {
15  self::$repo[__CLASS__] = [];
16  }
17  if (!array_key_exists($id, self::$repo[__CLASS__]))
18  {
19  self::$repo[__CLASS__][$id] = new static($id);
20  }
21  return self::$repo[__CLASS__][$id];
22  }
23 
24  public function destroy()
25  {
26  if (array_key_exists(__CLASS__, self::$repo))
27  {
28  foreach (self::$repo[__CLASS__] as $key => $object)
29  {
30  if ($object === $this)
31  {
32  unset(self::$repo[__CLASS__][$key]);
33  AddMessage2Log("Object: ".__CLASS__." $key were destroyed.");
34  return;
35  }
36  }
37  }
38  }
39  /**
40  * @param static|int $object
41  * @return static
42  */
43  public static function getInstance($object)
44  {
45  if ($object instanceof static)
46  {
47  return $object;
48  }
49  return static::getById($object);
50  }
51 }
Bitrix\Forum\Internals\getInstance
static getInstance($object)
Definition: forum/lib/internals/entityfabric.php:43
Bitrix\Forum\Internals\getById
static getById(int $id)
Definition: forum/lib/internals/entityfabric.php:11
Bitrix\Forum\Internals\destroy
destroy()
Definition: forum/lib/internals/entityfabric.php:24
Bitrix\Forum\Internals\EntityFabric
trait EntityFabric
Definition: forum/lib/internals/entityfabric.php:4
Bitrix\Forum\Internals
Definition: forum/lib/internals/conversionhandlers.php:3