Add config class

parent 84f9ffc5
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot;
use Illuminate\Config\Repository;
class Config extends Repository
{
/**
* Config data.
*
* @var array
*/
protected $data;
/**
* Create a new config instance.
*
* @param array $data
*/
public function __construct(array $data)
{
$this->data = $data;
}
/**
* Get config parameter
*
* @param string $key
* @param mixed $default
* @return array|mixed|null
*/
public function get($key = null, $default = null)
{
$array = $this->data;
if ($key === null) {
return $array;
}
if (array_key_exists($key, $array)) {
return $array[$key];
}
foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return $default;
}
}
return $array;
}
}
...@@ -65,13 +65,6 @@ class Telegram ...@@ -65,13 +65,6 @@ class Telegram
*/ */
protected $container; protected $container;
/**
* Raw request data (json) for webhook methods
*
* @var string
*/
protected $input;
/** /**
* Custom commands paths * Custom commands paths
* *
...@@ -206,6 +199,17 @@ class Telegram ...@@ -206,6 +199,17 @@ class Telegram
$this->container = Container::getInstance(); $this->container = Container::getInstance();
$this->container->instance(Telegram::class, $this); $this->container->instance(Telegram::class, $this);
$config = new Config([
'commands' => [
'paths' => [
TB_BASE_COMMANDS_PATH . '/SystemCommands',
],
],
]);
dump($config);
die;
$this->container->instance(Config::class, $config);
} }
/** /**
...@@ -326,30 +330,6 @@ class Telegram ...@@ -326,30 +330,6 @@ class Telegram
return null; return null;
} }
/**
* Set custom input string for debug purposes
*
* @param string $input (json format)
*
* @return \Longman\TelegramBot\Telegram
*/
public function setCustomInput($input)
{
$this->input = $input;
return $this;
}
/**
* Get custom input string for debug purposes
*
* @return string
*/
public function getCustomInput()
{
return $this->input;
}
/** /**
* Get the ServerResponse of the last Command execution * Get the ServerResponse of the last Command execution
* *
......
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Tests\Unit;
class ConfigTest extends AbstractTestCase
{
/**
* @test
*/
public function get_config_array()
{
$configData = [
'locales' => [
'ka' => [
'name' => 'Georgian',
],
],
];
$config = $this->getConfig($configData);
$this->assertEquals($configData, $config->get());
}
/**
* @test
*/
public function set_get()
{
$config = [
'locales' => [
'ka' => [
'name' => 'Georgian',
],
],
];
$config = $this->getConfig($config);
$this->assertEquals('Georgian', $config->get('locales.ka.name'));
}
/**
* @test
*/
public function get_default_value()
{
$config = [
'locales' => [
'ka' => [
'name' => 'Georgian',
],
],
];
$config = $this->getConfig($config);
$this->assertEquals('Thai', $config->get('locales.th.name', 'Thai'));
}
/**
* @test
*/
public function table_name()
{
$config = [
'db' => [
'autosave' => true,
'connection' => 'mysql',
'texts_table' => 'texts',
],
];
$config = $this->getConfig($config);
$this->assertEquals('texts', $config->get('db.texts_table'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment