mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-23 09:56:34 +02:00
add adapters
This commit is contained in:
parent
b1c4a1e3aa
commit
f609fc175f
3 changed files with 212 additions and 18 deletions
115
controllers/internals/Adapter.php
Executable file
115
controllers/internals/Adapter.php
Executable file
|
@ -0,0 +1,115 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Class to interact with adapters
|
||||
*/
|
||||
class Adapter extends \descartes\InternalController
|
||||
{
|
||||
private const ADAPTERS_FILES_END = 'Adapter.php';
|
||||
private const ADAPTERS_META_START = 'meta_';
|
||||
|
||||
/**
|
||||
* List adapters using internal metas
|
||||
* @return array
|
||||
*/
|
||||
public function list_adapters ()
|
||||
{
|
||||
$adapters = [];
|
||||
|
||||
$files = $this->list_files();
|
||||
if (!$files)
|
||||
{
|
||||
return $adapters;
|
||||
}
|
||||
|
||||
foreach ($files as $file)
|
||||
{
|
||||
$metas = $this->read_adapter_metas($file);
|
||||
if (!$metas)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$adapters[] = $metas;
|
||||
}
|
||||
|
||||
return $adapters;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* List Adapters files
|
||||
* @return mixed (false|array) : array of adapters files path
|
||||
*/
|
||||
public function list_files ()
|
||||
{
|
||||
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);
|
||||
if ($end !== self::ADAPTERS_FILES_END)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$adapters_files[] = PWD_ADAPTERS . '/' . $filename;
|
||||
}
|
||||
|
||||
return $adapters_files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read constants of an adapter
|
||||
* @return mixed(array|bool) : False on error, array of constants name => value
|
||||
*/
|
||||
public function read_adapter_metas ($adapter_file)
|
||||
{
|
||||
$metas = [];
|
||||
|
||||
if (!is_readable($adapter_file))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$adapter_classname = pathinfo($adapter_file, PATHINFO_FILENAME);
|
||||
$reflection_class = new \ReflectionClass('\adapters\\' . $adapter_classname);
|
||||
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));
|
||||
|
||||
if ($start_with !== self::ADAPTERS_META_START)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$metas[$method->getName()] = $method->invoke(null);
|
||||
}
|
||||
|
||||
return $metas;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue