diff --git a/src/Application.php b/src/Application.php index f57b61d2..a496d8c7 100644 --- a/src/Application.php +++ b/src/Application.php @@ -3,6 +3,8 @@ namespace Misuzu; use Aitemu\RouteCollection; use Misuzu\Config\ConfigManager; +use UnexpectedValueException; +use InvalidArgumentException; /** * Handles the set up procedures. @@ -51,7 +53,7 @@ class Application extends ApplicationBase public function startDatabase(): void { if ($this->hasDatabase) { - throw new \Exception('Database module has already been started.'); + throw new UnexpectedValueException('Database module has already been started.'); } $this->addModule('database', new Database($this->config, self::DATABASE_CONNECTIONS[0])); @@ -70,7 +72,7 @@ class Application extends ApplicationBase $section = 'Database.' . $name; if (!$config->contains($section)) { - throw new \Exception("Database {$name} is not configured."); + throw new InvalidArgumentException("Database {$name} is not configured."); } $database->addConnectionFromConfig($section, $name); @@ -83,7 +85,7 @@ class Application extends ApplicationBase public function startTemplating(): void { if ($this->hasTemplating) { - throw new \Exception('Templating module has already been started.'); + throw new UnexpectedValueException('Templating module has already been started.'); } $this->addModule('templating', $twig = new TemplateEngine); @@ -110,7 +112,7 @@ class Application extends ApplicationBase public function startRouter(array $routes = null): void { if ($this->hasRouter) { - throw new \Exception('Router module has already been started.'); + throw new UnexpectedValueException('Router module has already been started.'); } $this->addModule('router', $router = new RouteCollection); diff --git a/src/ApplicationBase.php b/src/ApplicationBase.php index 33f7c76f..8e624fdb 100644 --- a/src/ApplicationBase.php +++ b/src/ApplicationBase.php @@ -1,6 +1,9 @@ modules[$name]; } - throw new \Exception('Invalid property.'); + throw new InvalidArgumentException('Invalid property.'); } /** @@ -97,7 +100,7 @@ abstract class ApplicationBase public function addModule(string $name, $module): void { if ($this->hasModule($name)) { - throw new \Exception('This module has already been registered.'); + throw new InvalidArgumentException('This module has already been registered.'); } $this->modules[$name] = $module; diff --git a/src/Colour.php b/src/Colour.php index 99a0edf0..fb8ac695 100644 --- a/src/Colour.php +++ b/src/Colour.php @@ -1,6 +1,8 @@ configManager->contains($section, 'driver')) { - throw new \Exception('Config section not found!'); + throw new InvalidArgumentException('Config section not found!'); } $driver = $this->configManager->get($section, 'driver'); if (!in_array($driver, self::SUPPORTED_DB_ALS)) { - throw new \Exception('Unsupported driver.'); + throw new InvalidArgumentException('Unsupported driver.'); } $args = [ diff --git a/src/IO/FileStream.php b/src/IO/FileStream.php index 07c9c7d2..0ee15b14 100644 --- a/src/IO/FileStream.php +++ b/src/IO/FileStream.php @@ -221,7 +221,6 @@ class FileStream extends Stream public function writeChar(int $char): void { - $this->ensureHandleActive(); $this->write(chr($char), 0, 1); } diff --git a/src/IO/NetworkStream.php b/src/IO/NetworkStream.php new file mode 100644 index 00000000..59e85221 --- /dev/null +++ b/src/IO/NetworkStream.php @@ -0,0 +1,149 @@ +host = $host; + $this->port = $port; + $this->timeout = $timeout ?? (int)ini_get('default_socket_timeout'); + + try { + $this->resourceHandle = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); + } catch (ErrorException $ex) { + throw new IOException($ex->getMessage()); + } + + if ($this->resourceHandle === false) { + throw new IOException("[{$errno}] {$errstr}"); + } + + $this->ensureHandleActive(); + } + + public function __destruct() + { + if (!is_resource($this->resourceHandle)) { + return; + } + + $this->close(); + } + + public function getResource(): resource + { + $this->ensureHandleActive(); + return $this->resourceHandle; + } + + protected function ensureHandleActive(): void + { + if (!is_resource($this->resourceHandle)) { + throw new IOException("No active file handle."); + } + } + + protected function getCanRead(): bool + { + return true; + } + + protected function getCanSeek(): bool + { + return false; + } + + protected function getCanTimeout(): bool + { + return true; + } + + protected function getCanWrite(): bool + { + return true; + } + + protected function getLength(): int + { + return -1; + } + + protected function getPosition(): int + { + return -1; + } + + protected function getReadTimeout(): int + { + return -1; + } + + protected function getWriteTimeout(): int + { + return -1; + } + + public function flush(): void + { + $this->ensureHandleActive(); + fflush($this->resourceHandle); + } + + public function close(): void + { + $this->ensureHandleActive(); + fclose($this->resourceHandle); + } + + public function read(int $length): string + { + $this->ensureHandleActive(); + + $read = fread($this->resourceHandle, $length); + + if ($read === false) { + throw new IOException('Read failed.'); + } + + return $read; + } + + public function readChar(): int + { + $this->ensureHandleActive(); + + return ord(fgetc($this->resourceHandle)); + } + + public function write(string $data): int + { + $this->ensureHandleActive(); + + $write = fwrite($this->resourceHandle, $data); + + if ($write === false) { + throw new IOException('Write failed.'); + } + + return $write; + } + + public function writeChar(int $char): void + { + $this->write(chr($char), 0, 1); + } + + /** + * @SuppressWarnings("unused") + */ + public function seek(int $offset, int $origin): void + { + throw new IOException('This stream cannot perform seek operations.'); + } +} diff --git a/src/IO/Stream.php b/src/IO/Stream.php index c64ee76c..b0b2aaa0 100644 --- a/src/IO/Stream.php +++ b/src/IO/Stream.php @@ -1,6 +1,8 @@ {$name}(); } - // todo: specialised exception type - throw new IOException('Invalid property.'); + throw new InvalidArgumentException; } abstract protected function getCanRead(): bool; diff --git a/src/Net/CIDR.php b/src/Net/CIDR.php index 5f25cc73..f15ffb4d 100644 --- a/src/Net/CIDR.php +++ b/src/Net/CIDR.php @@ -1,6 +1,8 @@ assertEquals($colour->blue, static::BLUE_HEX3); $this->assertFalse($colour->inherit); } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Invalid hex colour format! + */ + public function testHexException() + { + Colour::fromHex('invalid hex code'); + } }