123 lines
2.9 KiB
PHP
123 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace core\log;
|
|
|
|
/**
|
|
* Stream factory, to create stream with ease
|
|
*/
|
|
class StreamFactory
|
|
{
|
|
/**
|
|
* Available client NOTE: to update for "vanilla" client
|
|
*
|
|
* @var \array[\string]
|
|
*/
|
|
const array AVAILABLE_CLIENTS = array(
|
|
'MQTT',
|
|
'Redis',
|
|
'File',
|
|
);
|
|
|
|
/**
|
|
* Check if the tag is in the allow list of tags
|
|
*
|
|
* @param \core\log\Tag $tag Tag to search for
|
|
* @param \array[\core\log\Tag] $allow_list Allow list of tags
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function search_tag(\core\log\Tag $tag, array $allow_list) : bool
|
|
{
|
|
$found = False;
|
|
foreach ($allow_list as $accepted_tag) # NOTE: time depends on the number of tags (not constant time), but we don't care
|
|
{
|
|
if ($tag->__tostring() === $accepted_tag->__tostring())
|
|
{
|
|
$found = True;
|
|
break;
|
|
}
|
|
}
|
|
return $found;
|
|
}
|
|
|
|
/**
|
|
* Instanciate client from name, with tag as an argument
|
|
*
|
|
* @param string $client_name Name of the client
|
|
* @param \core\log\Tag $tag Tag associated to the client
|
|
*
|
|
* @throws \core\log\StreamException
|
|
*
|
|
* @return \core\log\Client
|
|
*/
|
|
public static function create_client(string $client_name, \core\log\Tag) : \core\log\Stream
|
|
{
|
|
$client = null;
|
|
|
|
$client_name = \ucwords($client_name);
|
|
$client_name = \str_replace(' ', '', $client_name);
|
|
|
|
$vanilla = '\\core\\log\\clients\\' . $client_name;
|
|
|
|
if (\in_array($client_name, self::AVAILABLE_CLIENTS))
|
|
{
|
|
$client = new $client_name($tag);
|
|
}
|
|
else
|
|
{
|
|
$plugin = \core\plugin\PluginManager::fetch_class($client_name);
|
|
|
|
if (!\is_null($plugin) && \class_exists($plugin))
|
|
{
|
|
$client = new $plugin($tag);
|
|
}
|
|
else
|
|
{
|
|
\throw new \core\log\StreamException(_('unknown client in configuration', 1));
|
|
}
|
|
}
|
|
|
|
return $client;
|
|
}
|
|
|
|
/**
|
|
* Build the wanted tag to use for future logging purpose
|
|
*
|
|
* @throws \core\config\ConfigException
|
|
* @throws \core\log\StreamException
|
|
*
|
|
* @return \core\log\Stream | null If the tag should not be built, return null
|
|
*/
|
|
public static function build(\core\log\Tag $tag) : \core\log\Stream | null
|
|
{
|
|
$hardcoded_configuration = \core\config\class\core\LogConfiguration(array(
|
|
'tags' => array(),
|
|
'client' => array('name' => 'File'),
|
|
));
|
|
$client_name = $hardcoded_configuration->get('client', 'name');
|
|
$tags = $hardcoded_configuration->get('tags');
|
|
|
|
if (\core\config\GlobalConfiguration::exists('class', 'core', 'log')) # set over hard-coded fallback
|
|
{
|
|
$configuration = \core\config\GlobalConfiguration::read('class' 'core', 'log');
|
|
|
|
if ($configuration->exists('client', 'name'))
|
|
{
|
|
$client_name = $configuration->get('client', 'name');
|
|
}
|
|
if ($configuration->exists('tags'))
|
|
{
|
|
$tags = $configuration->get('tags');
|
|
}
|
|
}
|
|
|
|
if (!$self::search_tag($tag, $tags))
|
|
{
|
|
return null; # NOTE: the tag should not be created because the admin does not ask to log it
|
|
}
|
|
|
|
return self::create_client($client_name, $tag);
|
|
}
|
|
}
|
|
|
|
?>
|