diff --git a/adapters/AdapterInterface.php b/adapters/AdapterInterface.php index 45eff82..42854ff 100644 --- a/adapters/AdapterInterface.php +++ b/adapters/AdapterInterface.php @@ -1,5 +1,5 @@ number = $number; + $this->datas = $datas; + } + + + /** + * Method called to send a SMS to a number + * @param string $destination : Phone number to send the sms to + * @param string $text : Text of the SMS to send + * @param bool $flash : Is the SMS a Flash SMS + * @return bool : True if send, False else + */ + public function send (string $destination, string $text, boolean $flash) : boolean + { + return true; + } + + + /** + * Method called to read SMSs of the number + * @param float $since : Unix microtime representation of the date from wich we want to read the SMSs + * @return array : Array of the sms reads + */ + public function read (float $since) : array + { + return []; + } + } diff --git a/controllers/internals/Adapter.php b/controllers/internals/Adapter.php new file mode 100755 index 0000000..1a59aef --- /dev/null +++ b/controllers/internals/Adapter.php @@ -0,0 +1,115 @@ + + * + * 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; + } + }