Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
statmethod.php
1<?php
2namespace Bitrix\Rest;
3
5
32class StatMethodTable extends Main\Entity\DataManager
33{
34 const METHOD_TYPE_METHOD = 'M';
35 const METHOD_TYPE_EVENT = 'E';
37 const METHOD_TYPE_ROBOT = 'R';
39
40 protected static $methodCache = null;
41
47 public static function getTableName()
48 {
49 return 'b_rest_stat_method';
50 }
51
57 public static function getMap()
58 {
59 return array(
60 'ID' => array(
61 'data_type' => 'integer',
62 'primary' => true,
63 'autocomplete' => true,
64 ),
65 'NAME' => array(
66 'data_type' => 'string',
67 'required' => true,
68 'validation' => array(__CLASS__, 'validateName'),
69 ),
70 'METHOD_TYPE' => array(
71 'data_type' => 'enum',
72 'required' => false,
73 'values' => array(
74 self::METHOD_TYPE_METHOD,
75 self::METHOD_TYPE_EVENT,
76 self::METHOD_TYPE_PLACEMENT,
77 self::METHOD_TYPE_ROBOT,
78 self::METHOD_TYPE_ACTIVITY,
79 ),
80 ),
81 );
82 }
88 public static function validateName()
89 {
90 return array(
91 new Main\Entity\Validator\Unique(),
92 );
93 }
94
95 public static function getId($methodName)
96 {
97 static::checkList(array($methodName));
98
99 return static::$methodCache[$methodName];
100 }
101
102 public static function checkList($methodList, $methodType = self::METHOD_TYPE_METHOD)
103 {
104 static::loadFromCache();
105
106 $update = false;
107 foreach($methodList as $method)
108 {
109 if(!array_key_exists($method, static::$methodCache))
110 {
111 static::addMethod($method, $methodType);
112 $update = true;
113 }
114 }
115
116 if($update)
117 {
118 static::loadFromCache(true);
119 }
120
121 }
122
123 protected static function addMethod($methodName, $methodType)
124 {
125 $connection = Main\Application::getConnection();
126 $helper = $connection->getSqlHelper();
127
128 $sqlTableName = static::getTableName();
129 $sqlMethodName = $helper->forSql($methodName);
130 $sqlMethodType = in_array(
131 $methodType, [
132 static::METHOD_TYPE_METHOD,
133 static::METHOD_TYPE_EVENT,
134 static::METHOD_TYPE_PLACEMENT,
135 static::METHOD_TYPE_ROBOT,
136 static::METHOD_TYPE_ACTIVITY,
137 ]
138 ) ? $methodType : self::METHOD_TYPE_METHOD;
139
140 $query = "INSERT IGNORE INTO {$sqlTableName} (NAME, METHOD_TYPE) VALUES ('{$sqlMethodName}', '{$sqlMethodType}')";
141 $connection->query($query);
142 }
143
144 protected static function loadFromCache($force = false)
145 {
146 $managedCache = Main\Application::getInstance()->getManagedCache();
147 $cacheId = 'stat_method_cache';
148
149 if($force)
150 {
151 static::$methodCache = null;
152 $managedCache->clean($cacheId);
153 }
154
155 if(static::$methodCache === null)
156 {
157 if($managedCache->read(86400, $cacheId))
158 {
159 static::$methodCache = $managedCache->get($cacheId);
160 }
161 else
162 {
163 static::$methodCache = array();
164 $dbRes = static::getList();
165 while($method = $dbRes->fetch())
166 {
167 static::$methodCache[$method['NAME']] = $method['ID'];
168 }
169 $managedCache->set($cacheId, static::$methodCache);
170 }
171 }
172 }
173}
static getId($methodName)
static checkList($methodList, $methodType=self::METHOD_TYPE_METHOD)
static addMethod($methodName, $methodType)
static loadFromCache($force=false)