Skip to content

Commit f72cfe7

Browse files
committed
[viera-connector-homekit-connector-bridge] Prepared viera connector to homekit connector bridge (#306)
1 parent 328e14e commit f72cfe7

7 files changed

+193
-32
lines changed

src/DI/ApplicationExtension.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,7 @@ public function loadConfiguration(): void
157157
*/
158158

159159
if ($configuration->logging->console->enabled) {
160-
$builder->addDefinition(
161-
$this->prefix('subscribers.console'),
162-
new DI\Definitions\ServiceDefinition(),
163-
)
160+
$builder->addDefinition($this->prefix('subscribers.console'), new DI\Definitions\ServiceDefinition())
164161
->setType(Subscribers\Console::class)
165162
->setArguments([
166163
'handler' => $consoleHandler,
@@ -176,15 +173,15 @@ public function loadConfiguration(): void
176173
->setType(Subscribers\EntityDiscriminator::class);
177174
}
178175

176+
$builder->addDefinition($this->prefix('subscribers.eventLoop'), new DI\Definitions\ServiceDefinition())
177+
->setType(Subscribers\EventLoop::class);
178+
179179
/**
180180
* SENTRY ISSUES LOGGER
181181
*/
182182

183183
if (interface_exists('\Sentry\ClientInterface')) {
184-
$builder->addDefinition(
185-
$this->prefix('helpers.sentry'),
186-
new DI\Definitions\ServiceDefinition(),
187-
)
184+
$builder->addDefinition($this->prefix('helpers.sentry'), new DI\Definitions\ServiceDefinition())
188185
->setType(Helpers\Sentry::class);
189186
}
190187

@@ -244,6 +241,9 @@ public function loadConfiguration(): void
244241

245242
$builder->addDefinition($this->prefix('utilities.doctrineDateProvider'), new DI\Definitions\ServiceDefinition())
246243
->setType(Utilities\DateTimeProvider::class);
244+
245+
$builder->addDefinition($this->prefix('utilities.eventLoop.status'), new DI\Definitions\ServiceDefinition())
246+
->setType(Utilities\EventLoopStatus::class);
247247
}
248248

249249
/**

src/Events/DbTransactionFinished.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* DbTransactionFinished.php
5+
*
6+
* @license More in LICENSE.md
7+
* @copyright https://www.fastybird.com
8+
* @author Adam Kadlec <adam.kadlec@fastybird.com>
9+
* @package FastyBird:ApplicationLibrary!
10+
* @subpackage Events
11+
* @since 1.0.0
12+
*
13+
* @date 11.09.24
14+
*/
15+
16+
namespace FastyBird\Library\Application\Events;
17+
18+
use Symfony\Contracts\EventDispatcher;
19+
20+
/**
21+
* Database transaction finished event
22+
*
23+
* @package FastyBird:ApplicationLibrary!
24+
* @subpackage Events
25+
*
26+
* @author Adam Kadlec <adam.kadlec@fastybird.com>
27+
*/
28+
class DbTransactionFinished extends EventDispatcher\Event
29+
{
30+
31+
}

src/Events/DbTransactionStarted.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* DbTransactionStarted.php
5+
*
6+
* @license More in LICENSE.md
7+
* @copyright https://www.fastybird.com
8+
* @author Adam Kadlec <adam.kadlec@fastybird.com>
9+
* @package FastyBird:ApplicationLibrary!
10+
* @subpackage Events
11+
* @since 1.0.0
12+
*
13+
* @date 11.09.24
14+
*/
15+
16+
namespace FastyBird\Library\Application\Events;
17+
18+
use Symfony\Contracts\EventDispatcher;
19+
20+
/**
21+
* Database transaction started event
22+
*
23+
* @package FastyBird:ApplicationLibrary!
24+
* @subpackage Events
25+
*
26+
* @author Adam Kadlec <adam.kadlec@fastybird.com>
27+
*/
28+
class DbTransactionStarted extends EventDispatcher\Event
29+
{
30+
31+
}

src/Helpers/Database.php

+11-18
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
use Doctrine\DBAL;
1919
use Doctrine\ORM;
2020
use Doctrine\Persistence;
21+
use FastyBird\Library\Application\Events;
2122
use FastyBird\Library\Application\Exceptions;
2223
use FastyBird\Library\Metadata\Types as MetadataTypes;
2324
use Nette;
25+
use Psr\EventDispatcher;
2426
use Psr\Log;
2527
use Throwable;
2628
use function gc_collect_cycles;
@@ -41,6 +43,7 @@ class Database
4143

4244
public function __construct(
4345
private readonly Persistence\ManagerRegistry|null $managerRegistry = null,
46+
private readonly EventDispatcher\EventDispatcherInterface|null $dispatcher = null,
4447
private readonly Log\LoggerInterface $logger = new Log\NullLogger(),
4548
)
4649
{
@@ -95,6 +98,8 @@ public function transaction(callable $callback)
9598
// Start transaction connection to the database
9699
$connection->beginTransaction();
97100

101+
$this->dispatcher?->dispatch(new Events\DbTransactionStarted());
102+
98103
$result = $callback();
99104

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

114+
$this->dispatcher?->dispatch(new Events\DbTransactionFinished());
115+
109116
return $result;
110117
} catch (Throwable $ex) {
111118
// Revert all changes when error occur
@@ -137,6 +144,8 @@ public function beginTransaction(): void
137144
$this->pingAndReconnect();
138145

139146
$connection->beginTransaction();
147+
148+
$this->dispatcher?->dispatch(new Events\DbTransactionStarted());
140149
} catch (Throwable $ex) {
141150
throw new Exceptions\InvalidState(
142151
'An error occurred: ' . $ex->getMessage(),
@@ -165,6 +174,8 @@ public function commitTransaction(): void
165174
} else {
166175
// Commit all changes into database
167176
$connection->commit();
177+
178+
$this->dispatcher?->dispatch(new Events\DbTransactionFinished());
168179
}
169180
} catch (Throwable $ex) {
170181
// Revert all changes when error occur
@@ -234,24 +245,6 @@ public function clear(): void
234245
continue;
235246
}
236247

237-
// Flushing and then clearing Doctrine's entity manager allows
238-
// for more memory to be released by PHP
239-
try {
240-
if ($manager->isOpen()) {
241-
$manager->flush();
242-
}
243-
} catch (Throwable $ex) {
244-
// Log caught exception
245-
$this->logger->error(
246-
'An unhandled error occurred during flushing entity manager',
247-
[
248-
'source' => MetadataTypes\Sources\Module::NOT_SPECIFIED,
249-
'type' => 'helper',
250-
'exception' => Logger::buildException($ex),
251-
],
252-
);
253-
}
254-
255248
try {
256249
$manager->getConnection()->close();
257250
} catch (Throwable $ex) {

src/Helpers/Logger.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ class Logger
4747
/**
4848
* @return array<array<string, string|int>>
4949
*/
50-
public static function buildException(Throwable $ex): array
50+
public static function buildException(Throwable $ex, bool $render = true): array
5151
{
52-
try {
53-
$blueScreen = new Tracy\BlueScreen();
54-
$blueScreen->renderToFile($ex, self::getExceptionFile($ex));
55-
} catch (Throwable) {
56-
// Blue scree could not be saved
52+
if ($render) {
53+
try {
54+
$blueScreen = new Tracy\BlueScreen();
55+
$blueScreen->renderToFile($ex, self::getExceptionFile($ex));
56+
} catch (Throwable) {
57+
// Blue scree could not be saved
58+
}
5759
}
5860

5961
return self::processAllExceptions($ex);

src/Subscribers/EventLoop.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* EventLoop.php
5+
*
6+
* @license More in LICENSE.md
7+
* @copyright https://www.fastybird.com
8+
* @author Adam Kadlec <adam.kadlec@fastybird.com>
9+
* @package FastyBird:ApplicationLibrary!
10+
* @subpackage Subscribers
11+
* @since 1.0.0
12+
*
13+
* @date 11.09.24
14+
*/
15+
16+
namespace FastyBird\Library\Application\Subscribers;
17+
18+
use FastyBird\Library\Application\Events;
19+
use FastyBird\Library\Application\Utilities;
20+
use Nette;
21+
use Symfony\Component\EventDispatcher;
22+
23+
/**
24+
* Event loop events
25+
*
26+
* @package FastyBird:ApplicationLibrary!
27+
* @subpackage Subscribers
28+
*
29+
* @author Adam Kadlec <adam.kadlec@fastybird.com>
30+
*/
31+
final class EventLoop implements EventDispatcher\EventSubscriberInterface
32+
{
33+
34+
use Nette\SmartObject;
35+
36+
public function __construct(private readonly Utilities\EventLoopStatus $eventLoopStatus)
37+
{
38+
}
39+
40+
public static function getSubscribedEvents(): array
41+
{
42+
return [
43+
Events\EventLoopStarted::class => 'loopStarted',
44+
Events\EventLoopStopped::class => 'loopStopped',
45+
Events\EventLoopStopping::class => 'loopStopped',
46+
];
47+
}
48+
49+
public function loopStarted(): void
50+
{
51+
$this->eventLoopStatus->setStatus(true);
52+
}
53+
54+
public function loopStopped(): void
55+
{
56+
$this->eventLoopStatus->setStatus(false);
57+
}
58+
59+
}

src/Utilities/EventLoopStatus.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* EventLoopStatus.php
5+
*
6+
* @license More in LICENSE.md
7+
* @copyright https://www.fastybird.com
8+
* @author Adam Kadlec <adam.kadlec@fastybird.com>
9+
* @package FastyBird:ApplicationLibrary!
10+
* @subpackage Utilities
11+
* @since 1.0.0
12+
*
13+
* @date 12.09.24
14+
*/
15+
16+
namespace FastyBird\Library\Application\Utilities;
17+
18+
use Nette;
19+
20+
/**
21+
* Event loop status helper
22+
*
23+
* @package FastyBird:ApplicationLibrary!
24+
* @subpackage Utilities
25+
*
26+
* @author Adam Kadlec <adam.kadlec@fastybird.com>
27+
*/
28+
final class EventLoopStatus
29+
{
30+
31+
use Nette\SmartObject;
32+
33+
private bool $status = false;
34+
35+
public function setStatus(bool $status): void
36+
{
37+
$this->status = $status;
38+
}
39+
40+
public function isRunning(): bool
41+
{
42+
return $this->status;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)