2019-11-12 05:18:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace controllers\internals;
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Class to interact with adapters.
|
2019-11-12 05:18:32 +01:00
|
|
|
*/
|
|
|
|
class Adapter extends \descartes\InternalController
|
|
|
|
{
|
|
|
|
private const ADAPTERS_FILES_END = 'Adapter.php';
|
|
|
|
private const ADAPTERS_META_START = 'meta_';
|
|
|
|
|
|
|
|
/**
|
2021-03-19 02:45:12 +01:00
|
|
|
* List adapters with filepath and internal metas.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2021-03-19 02:45:12 +01:00
|
|
|
* @return array : ['adapter_filepath' => ['meta...' => value, ...], ...]
|
2019-11-12 05:18:32 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function list_adapters()
|
2019-11-12 05:18:32 +01:00
|
|
|
{
|
|
|
|
$adapters = [];
|
|
|
|
|
|
|
|
$files = $this->list_files();
|
|
|
|
if (!$files)
|
|
|
|
{
|
|
|
|
return $adapters;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
$metas = $this->read_adapter_metas($file);
|
|
|
|
if (!$metas)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
$adapters[$file] = $metas;
|
2019-11-12 05:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $adapters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* List Adapters files.
|
|
|
|
*
|
|
|
|
* @return mixed (false|array) : array of adapters files path
|
|
|
|
*/
|
|
|
|
public function list_files()
|
2019-11-12 05:18:32 +01:00
|
|
|
{
|
|
|
|
if (!is_readable(PWD_ADAPTERS))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$adapters_files = [];
|
|
|
|
|
|
|
|
$files = scandir(PWD_ADAPTERS);
|
|
|
|
foreach ($files as $filename)
|
|
|
|
{
|
|
|
|
$len = mb_strlen(self::ADAPTERS_FILES_END);
|
|
|
|
$end = mb_substr($filename, -$len);
|
2020-01-17 18:19:25 +01:00
|
|
|
if (self::ADAPTERS_FILES_END !== $end)
|
2019-11-12 05:18:32 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:47:08 +01:00
|
|
|
$adapters_files[] = PWD_ADAPTERS . '/' . $filename;
|
2019-11-12 05:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $adapters_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Read constants of an adapter.
|
|
|
|
*
|
|
|
|
* @param mixed $adapter_file
|
|
|
|
*
|
2019-11-12 05:18:32 +01:00
|
|
|
* @return mixed(array|bool) : False on error, array of constants name => value
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function read_adapter_metas($adapter_file)
|
2019-11-12 05:18:32 +01:00
|
|
|
{
|
|
|
|
$metas = [];
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-12 05:18:32 +01:00
|
|
|
if (!is_readable($adapter_file))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$adapter_classname = pathinfo($adapter_file, PATHINFO_FILENAME);
|
2020-01-17 18:47:08 +01:00
|
|
|
$reflection_class = new \ReflectionClass('\adapters\\' . $adapter_classname);
|
2019-11-12 05:18:32 +01:00
|
|
|
if (!$reflection_class)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$methods = $reflection_class->getMethods(\ReflectionMethod::IS_STATIC);
|
|
|
|
foreach ($methods as $method)
|
|
|
|
{
|
|
|
|
$start_with = mb_substr($method->getName(), 0, mb_strlen(self::ADAPTERS_META_START));
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
if (self::ADAPTERS_META_START !== $start_with)
|
2019-11-12 05:18:32 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$metas[$method->getName()] = $method->invoke(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $metas;
|
|
|
|
}
|
2021-03-19 02:45:12 +01:00
|
|
|
|
|
|
|
/**
|
2021-06-17 00:51:33 +02:00
|
|
|
* List all adapters for a meta value.
|
|
|
|
*
|
2021-03-19 02:45:12 +01:00
|
|
|
* @param $search_name : Name of the meta
|
|
|
|
* @param $search_value : Value of the meta
|
|
|
|
*
|
|
|
|
* @return array : Array with ['adapter filepath' => ['search_name' => value, ...], ...]
|
|
|
|
*/
|
|
|
|
public function list_adapters_with_meta_equal($search_name, $search_value)
|
|
|
|
{
|
|
|
|
$adapters = $this->list_adapters();
|
2021-06-17 00:51:33 +02:00
|
|
|
|
|
|
|
return array_filter($adapters, function ($metas) use ($search_name, $search_value)
|
|
|
|
{
|
2021-03-19 02:45:12 +01:00
|
|
|
$match = false;
|
|
|
|
foreach ($metas as $name => $value)
|
|
|
|
{
|
|
|
|
if ($name === $search_name && $value === $search_value)
|
|
|
|
{
|
|
|
|
$match = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $match;
|
|
|
|
});
|
|
|
|
}
|
2019-11-12 05:18:32 +01:00
|
|
|
}
|