Source of file ListenerManager.php
Size: 7,094 Bytes - Last Modified: 2020-10-24T02:46:31+00:00
/home/travis/build/NextDom/nextdom-core/src/Managers/ListenerManager.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 | <?php /* * This file is part of the NextDom software (https://github.com/NextDom or http://nextdom.github.io). * Copyright (c) 2018 NextDom. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 2. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* This file is part of Jeedom. * * Jeedom is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Jeedom is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jeedom. If not, see <http://www.gnu.org/licenses/>. */ namespace NextDom\Managers; use Exception; use NextDom\Enums\Common; use NextDom\Exceptions\CoreException; use NextDom\Helpers\DBHelper; use NextDom\Helpers\LogHelper; use NextDom\Helpers\SystemHelper; use NextDom\Helpers\Utils; use NextDom\Managers\Parents\BaseManager; use NextDom\Managers\Parents\CommonManager; use NextDom\Model\Entity\Listener; use ReflectionException; use const NEXTDOM_ROOT; use function GuzzleHttp\json_encode; /** * Class ListenerManager * @package NextDom\Managers */ class ListenerManager extends BaseManager { use CommonManager; const CLASS_NAME = Listener::class; const DB_CLASS_NAME = '`listener`'; /** * @return array|mixed|null * @throws CoreException * @throws ReflectionException */ public static function all() { return static::getAll(); } /** * @param $_class * @return array|mixed|null * @throws CoreException * @throws ReflectionException */ public static function byClass($_class) { return static::getMultipleByClauses([Common::CLASS => $_class]); } /** * @param $_class * @param $_function * @param string $_option * @return array|mixed|null * @throws CoreException * @throws ReflectionException */ public static function byClassAndFunction($_class, $_function, $_option = '') { $clauses = [ Common::CLASS_CODE => $_class, Common::FUNCTION => $_function, ]; if ($_option != '') { $_option = json_encode($_option, JSON_UNESCAPED_UNICODE); $clauses[Common::OPTION] = $_option; } return static::getOneByClauses($clauses); } /** * @param $_class * @param $_function * @param string $_option * @return array|mixed|null * @throws CoreException * @throws ReflectionException */ public static function searchClassFunctionOption($_class, $_function, $_option = '') { $value = [ Common::CLASS_CODE => $_class, Common::FUNCTION => $_function, Common::OPTION => '%' . $_option . '%', ]; $sql = static::getBaseSQL() . ' WHERE `class` = :class AND `function` = :function AND `option` LIKE :option'; return DBHelper::getAllObjects($sql, $value, self::CLASS_NAME); } /** * @param $_class * @param $_function * @param $_event * @return array|mixed|null * @throws CoreException * @throws ReflectionException */ public static function byClassFunctionAndEvent($_class, $_function, $_event) { return static::getMultipleByClauses([ Common::CLASS_CODE => $_class, Common::FUNCTION => $_function, Common::EVENT => $_event ]); } /** * @param $_class * @param $_function * @param $_event * @param string $_option * @throws CoreException */ public static function removeByClassFunctionAndEvent($_class, $_function, $_event, $_option = '') { $value = [ Common::CLASS_CODE => $_class, Common::FUNCTION => $_function, Common::EVENT => $_event, ]; $sql = 'DELETE FROM ' . self::DB_CLASS_NAME . ' WHERE `class` = :class AND `function` = :function AND `event` = :event'; if ($_option != '') { $_option = json_encode($_option, JSON_UNESCAPED_UNICODE); $value[Common::OPTION] = $_option; $sql .= ' AND `option`=:option'; } DBHelper::exec($sql, $value); } /** * @param $_event * @param $_value * @param $_datetime * @throws Exception */ public static function check($_event, $_value, $_datetime = null) { $listeners = self::searchEvent($_event); if (count($listeners) > 0) { foreach ($listeners as $listener) { $listener->run(str_replace('#', '', $_event), $_value, $_datetime); } } } /** * @param $_event * @return Listener[]|null * @throws Exception */ public static function searchEvent($_event) { if (strpos($_event, '#') !== false) { $clauses = [Common::EVENT => '%' . $_event . '%']; } else { $clauses = [Common::EVENT => '%#' . $_event . '#%']; } return static::searchMultipleByClauses($clauses); } /** * @param $_event * @throws Exception */ public static function backgroundCalculDependencyCmd($_event) { if (count(CmdManager::byValue($_event, 'info')) == 0) { return; } $cmd = NEXTDOM_ROOT . '/src/Api/start_listener.php'; $cmd .= ' event_id=' . $_event; SystemHelper::php($cmd . ' >> /dev/null 2>&1 &'); } public static function clean() { foreach (self::all() as $listener) { $events = $listener->getEvent(); if(count($events) > 0){ $listener->emptyEvent(); foreach ($events as $event) { $cmd = CmdManager::byId(str_replace('#','',$event)); if(is_object($cmd)){ $listener->addEvent($cmd->getId()); } } $listener->save(); $events = $listener->getEvent(); } else { LogHelper::add('listener','debug','Remove listener : '.json_encode(Utils::o2a($listener))); $listener->remove(); } } } } |