Skip to content

Commit

Permalink
[viera-connector-homekit-connector-bridge] Prepared viera connector t…
Browse files Browse the repository at this point in the history
…o homekit connector bridge (#306)
  • Loading branch information
actions-user committed Sep 29, 2024
1 parent 328e14e commit f72cfe7
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 32 deletions.
16 changes: 8 additions & 8 deletions src/DI/ApplicationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ public function loadConfiguration(): void
*/

if ($configuration->logging->console->enabled) {
$builder->addDefinition(
$this->prefix('subscribers.console'),
new DI\Definitions\ServiceDefinition(),
)
$builder->addDefinition($this->prefix('subscribers.console'), new DI\Definitions\ServiceDefinition())
->setType(Subscribers\Console::class)
->setArguments([
'handler' => $consoleHandler,
Expand All @@ -176,15 +173,15 @@ public function loadConfiguration(): void
->setType(Subscribers\EntityDiscriminator::class);
}

$builder->addDefinition($this->prefix('subscribers.eventLoop'), new DI\Definitions\ServiceDefinition())
->setType(Subscribers\EventLoop::class);

/**
* SENTRY ISSUES LOGGER
*/

if (interface_exists('\Sentry\ClientInterface')) {
$builder->addDefinition(
$this->prefix('helpers.sentry'),
new DI\Definitions\ServiceDefinition(),
)
$builder->addDefinition($this->prefix('helpers.sentry'), new DI\Definitions\ServiceDefinition())
->setType(Helpers\Sentry::class);
}

Expand Down Expand Up @@ -244,6 +241,9 @@ public function loadConfiguration(): void

$builder->addDefinition($this->prefix('utilities.doctrineDateProvider'), new DI\Definitions\ServiceDefinition())
->setType(Utilities\DateTimeProvider::class);

$builder->addDefinition($this->prefix('utilities.eventLoop.status'), new DI\Definitions\ServiceDefinition())
->setType(Utilities\EventLoopStatus::class);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Events/DbTransactionFinished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

/**
* DbTransactionFinished.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:ApplicationLibrary!
* @subpackage Events
* @since 1.0.0
*
* @date 11.09.24
*/

namespace FastyBird\Library\Application\Events;

use Symfony\Contracts\EventDispatcher;

/**
* Database transaction finished event
*
* @package FastyBird:ApplicationLibrary!
* @subpackage Events
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
class DbTransactionFinished extends EventDispatcher\Event
{

}
31 changes: 31 additions & 0 deletions src/Events/DbTransactionStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

/**
* DbTransactionStarted.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:ApplicationLibrary!
* @subpackage Events
* @since 1.0.0
*
* @date 11.09.24
*/

namespace FastyBird\Library\Application\Events;

use Symfony\Contracts\EventDispatcher;

/**
* Database transaction started event
*
* @package FastyBird:ApplicationLibrary!
* @subpackage Events
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
class DbTransactionStarted extends EventDispatcher\Event
{

}
29 changes: 11 additions & 18 deletions src/Helpers/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
use Doctrine\DBAL;
use Doctrine\ORM;
use Doctrine\Persistence;
use FastyBird\Library\Application\Events;
use FastyBird\Library\Application\Exceptions;
use FastyBird\Library\Metadata\Types as MetadataTypes;
use Nette;
use Psr\EventDispatcher;
use Psr\Log;
use Throwable;
use function gc_collect_cycles;
Expand All @@ -41,6 +43,7 @@ class Database

public function __construct(
private readonly Persistence\ManagerRegistry|null $managerRegistry = null,
private readonly EventDispatcher\EventDispatcherInterface|null $dispatcher = null,
private readonly Log\LoggerInterface $logger = new Log\NullLogger(),
)
{
Expand Down Expand Up @@ -95,6 +98,8 @@ public function transaction(callable $callback)
// Start transaction connection to the database
$connection->beginTransaction();

$this->dispatcher?->dispatch(new Events\DbTransactionStarted());

$result = $callback();

if ($connection->isRollbackOnly()) {
Expand All @@ -106,6 +111,8 @@ public function transaction(callable $callback)
$connection->commit();
}

$this->dispatcher?->dispatch(new Events\DbTransactionFinished());

return $result;
} catch (Throwable $ex) {
// Revert all changes when error occur
Expand Down Expand Up @@ -137,6 +144,8 @@ public function beginTransaction(): void
$this->pingAndReconnect();

$connection->beginTransaction();

$this->dispatcher?->dispatch(new Events\DbTransactionStarted());
} catch (Throwable $ex) {
throw new Exceptions\InvalidState(
'An error occurred: ' . $ex->getMessage(),
Expand Down Expand Up @@ -165,6 +174,8 @@ public function commitTransaction(): void
} else {
// Commit all changes into database
$connection->commit();

$this->dispatcher?->dispatch(new Events\DbTransactionFinished());
}
} catch (Throwable $ex) {
// Revert all changes when error occur
Expand Down Expand Up @@ -234,24 +245,6 @@ public function clear(): void
continue;
}

// Flushing and then clearing Doctrine's entity manager allows
// for more memory to be released by PHP
try {
if ($manager->isOpen()) {
$manager->flush();
}
} catch (Throwable $ex) {
// Log caught exception
$this->logger->error(
'An unhandled error occurred during flushing entity manager',
[
'source' => MetadataTypes\Sources\Module::NOT_SPECIFIED,
'type' => 'helper',
'exception' => Logger::buildException($ex),
],
);
}

try {
$manager->getConnection()->close();
} catch (Throwable $ex) {
Expand Down
14 changes: 8 additions & 6 deletions src/Helpers/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ class Logger
/**
* @return array<array<string, string|int>>
*/
public static function buildException(Throwable $ex): array
public static function buildException(Throwable $ex, bool $render = true): array
{
try {
$blueScreen = new Tracy\BlueScreen();
$blueScreen->renderToFile($ex, self::getExceptionFile($ex));
} catch (Throwable) {
// Blue scree could not be saved
if ($render) {
try {
$blueScreen = new Tracy\BlueScreen();
$blueScreen->renderToFile($ex, self::getExceptionFile($ex));
} catch (Throwable) {
// Blue scree could not be saved
}
}

return self::processAllExceptions($ex);
Expand Down
59 changes: 59 additions & 0 deletions src/Subscribers/EventLoop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

/**
* EventLoop.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:ApplicationLibrary!
* @subpackage Subscribers
* @since 1.0.0
*
* @date 11.09.24
*/

namespace FastyBird\Library\Application\Subscribers;

use FastyBird\Library\Application\Events;
use FastyBird\Library\Application\Utilities;
use Nette;
use Symfony\Component\EventDispatcher;

/**
* Event loop events
*
* @package FastyBird:ApplicationLibrary!
* @subpackage Subscribers
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
final class EventLoop implements EventDispatcher\EventSubscriberInterface
{

use Nette\SmartObject;

public function __construct(private readonly Utilities\EventLoopStatus $eventLoopStatus)
{
}

public static function getSubscribedEvents(): array
{
return [
Events\EventLoopStarted::class => 'loopStarted',
Events\EventLoopStopped::class => 'loopStopped',
Events\EventLoopStopping::class => 'loopStopped',
];
}

public function loopStarted(): void
{
$this->eventLoopStatus->setStatus(true);
}

public function loopStopped(): void
{
$this->eventLoopStatus->setStatus(false);
}

}
45 changes: 45 additions & 0 deletions src/Utilities/EventLoopStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

/**
* EventLoopStatus.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:ApplicationLibrary!
* @subpackage Utilities
* @since 1.0.0
*
* @date 12.09.24
*/

namespace FastyBird\Library\Application\Utilities;

use Nette;

/**
* Event loop status helper
*
* @package FastyBird:ApplicationLibrary!
* @subpackage Utilities
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
final class EventLoopStatus
{

use Nette\SmartObject;

private bool $status = false;

public function setStatus(bool $status): void
{
$this->status = $status;
}

public function isRunning(): bool
{
return $this->status;
}

}

0 comments on commit f72cfe7

Please sign in to comment.