diff --git a/src/Client.php b/src/Client.php index a4ae8c3..0a5884c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -39,13 +39,14 @@ class Client implements Interfaces\ClientInterface /** * Client constructor. * - * @param array|\RouterOS\Config $config + * @param array|\RouterOS\Interfaces\ConfigInterface $config Array with configuration or Config object + * @param bool $autoConnect If false it will skip auto-connect stage if not need to instantiate connection * * @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\QueryException */ - public function __construct($config) + public function __construct($config, bool $autoConnect = true) { // If array then need create object if (is_array($config)) { @@ -60,6 +61,11 @@ class Client implements Interfaces\ClientInterface // Save config if everything is okay $this->_config = $config; + // Skip next step if not need to instantiate connection + if (false === $autoConnect) { + return; + } + // Throw error if cannot to connect if (false === $this->connect()) { throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); @@ -385,7 +391,7 @@ class Client implements Interfaces\ClientInterface * @param string $value * @param array $matches */ - private function pregResponse(string $value, &$matches) + private function pregResponse(string $value, &$matches): void { preg_match_all('/^[=|.](.*)=(.*)/', $value, $matches); } @@ -454,7 +460,7 @@ class Client implements Interfaces\ClientInterface * @return bool * @throws ConfigException */ - private function isLegacy(array &$response): bool + private function isLegacy(array $response): bool { return count($response) > 1 && $response[0] === '!done' && !$this->config('legacy'); } @@ -467,7 +473,7 @@ class Client implements Interfaces\ClientInterface * @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\QueryException */ - private function connect(): bool + public function connect(): bool { // By default we not connected $connected = false; diff --git a/src/Laravel/Facade.php b/src/Laravel/Facade.php index 1afcd3e..dc16db3 100644 --- a/src/Laravel/Facade.php +++ b/src/Laravel/Facade.php @@ -2,9 +2,9 @@ namespace RouterOS\Laravel; -use Illuminate\Support\Facades\Facade; +use Illuminate\Support\Facades\Facade as BaseFacade; -class ClientFacade extends Facade +class Facade extends BaseFacade { /** * Get the registered name of the component. @@ -13,6 +13,6 @@ class ClientFacade extends Facade */ protected static function getFacadeAccessor(): string { - return ClientWrapper::class; + return Wrapper::class; } } diff --git a/src/Laravel/ServiceProvider.php b/src/Laravel/ServiceProvider.php index 9ce33ab..7bb6d49 100644 --- a/src/Laravel/ServiceProvider.php +++ b/src/Laravel/ServiceProvider.php @@ -4,7 +4,7 @@ namespace RouterOS\Laravel; use Illuminate\Support\ServiceProvider as BaseServiceProvider; -class ClientServiceProvider extends BaseServiceProvider +class ServiceProvider extends BaseServiceProvider { /** * Bootstrap any application services. @@ -29,6 +29,6 @@ class ClientServiceProvider extends BaseServiceProvider __DIR__ . '/../../configs/routeros-api.php', 'routeros-api' ); - $this->app->bind(ClientWrapper::class); + $this->app->bind(Wrapper::class); } } diff --git a/src/Laravel/Wrapper.php b/src/Laravel/Wrapper.php index 59cba9a..a2be5f2 100644 --- a/src/Laravel/Wrapper.php +++ b/src/Laravel/Wrapper.php @@ -3,22 +3,61 @@ namespace RouterOS\Laravel; use RouterOS\Client; +use RouterOS\Config; +use RouterOS\Interfaces\ClientInterface; +use RouterOS\Interfaces\ConfigInterface; -class ClientWrapper +class Wrapper { /** + * Alias for \RouterOS::client() method + * * @param array $params * * @return \RouterOS\Client * @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\QueryException + * @deprecated + * @codeCoverageIgnore + */ + public function getClient(array $params = []): ClientInterface + { + return $this->client($params); + } + + /** + * Get configs of library + * + * @param array $params + * + * @return \RouterOS\Interfaces\ConfigInterface + * @throws \RouterOS\Exceptions\ConfigException + */ + public function config(array $params = []): ConfigInterface + { + $config = config('routeros-api'); + $config = array_merge($config, $params); + $config = new Config($config); + + return $config; + } + + /** + * Instantiate client object + * + * @param array $params + * @param bool $autoConnect + * + * @return \RouterOS\Interfaces\ClientInterface + * @throws \RouterOS\Exceptions\ClientException + * @throws \RouterOS\Exceptions\ConfigException + * @throws \RouterOS\Exceptions\QueryException */ - public function getClient(array $params = []): Client + public function client(array $params = [], bool $autoConnect = true): ClientInterface { - $configs = config('routeros-api'); - $configs = array_merge($configs, $params); + $config = $this->config($params); - return new Client($configs); + return new Client($config, $autoConnect); } } diff --git a/tests/Laravel/ServiceProviderTests.php b/tests/Laravel/ServiceProviderTests.php new file mode 100644 index 0000000..bff8ba4 --- /dev/null +++ b/tests/Laravel/ServiceProviderTests.php @@ -0,0 +1,65 @@ +assertInstanceOf(Wrapper::class, $manager); + } + + public function testConfig(): void + { + $config = \RouterOS::config([ + 'host' => '192.168.1.3', + 'user' => 'admin', + 'pass' => 'admin' + ]); + $this->assertInstanceOf(Config::class, $config); + + $params = $config->getParameters(); + $this->assertArrayHasKey('host', $params); + $this->assertArrayHasKey('user', $params); + $this->assertArrayHasKey('pass', $params); + $this->assertArrayHasKey('ssl', $params); + $this->assertArrayHasKey('legacy', $params); + $this->assertArrayHasKey('timeout', $params); + $this->assertArrayHasKey('attempts', $params); + $this->assertArrayHasKey('delay', $params); + } + + public function testClient(): void + { + $client = \RouterOS::client([ + 'host' => '192.168.1.3', + 'user' => 'admin', + 'pass' => 'admin' + ], false); + + $this->assertEquals(get_class_methods($client), $this->client); + } +} diff --git a/tests/Laravel/TestCase.php b/tests/Laravel/TestCase.php new file mode 100644 index 0000000..8faf6f6 --- /dev/null +++ b/tests/Laravel/TestCase.php @@ -0,0 +1,35 @@ + Facade::class, + ]; + } +}