Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit e9dbd75

Browse files
Add trait HasListeners to Application and Route classes
1 parent a23903c commit e9dbd75

File tree

4 files changed

+46
-63
lines changed

4 files changed

+46
-63
lines changed

src/Abstract/ApplicationAbstract.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
use Pavlusha311245\UnitPhpSdk\Enums\ApplicationTypeEnum;
1010
use Pavlusha311245\UnitPhpSdk\Exceptions\UnitException;
1111
use Pavlusha311245\UnitPhpSdk\Interfaces\ApplicationInterface;
12+
use Pavlusha311245\UnitPhpSdk\Traits\HasListeners;
1213

1314
abstract class ApplicationAbstract implements ApplicationInterface
1415
{
15-
private ?Listener $_listener = null;
16+
use HasListeners;
1617

1718
private string $_type;
1819

@@ -264,20 +265,4 @@ public function parseFromArray(array $data): void
264265
$this->setLimits(new RequestLimit($data['limits']));
265266
}
266267
}
267-
268-
/**
269-
* @param Listener $listener
270-
*/
271-
public function setListener(Listener $listener): void
272-
{
273-
$this->_listener = $listener;
274-
}
275-
276-
/**
277-
* @return Listener
278-
*/
279-
public function getListener(): ?Listener
280-
{
281-
return $this->_listener;
282-
}
283268
}

src/Config/Route.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
use Pavlusha311245\UnitPhpSdk\Config\Routes\RouteBlock;
66
use Pavlusha311245\UnitPhpSdk\Interfaces\RouteInterface;
7+
use Pavlusha311245\UnitPhpSdk\Traits\HasListeners;
78

89
/**
910
* This class presents "routes" section from config
1011
*/
1112
class Route implements RouteInterface
1213
{
14+
use HasListeners;
15+
1316
private array $_routeBlocks;
1417

1518
private array $_listeners = [];
@@ -23,30 +26,6 @@ public function __construct(
2326
}
2427
}
2528

26-
/**
27-
* @inheritDoc
28-
*/
29-
public function hasListeners(): bool
30-
{
31-
return !empty($this->_listeners);
32-
}
33-
34-
/**
35-
* @inheritDoc
36-
*/
37-
public function setListener(Listener $listener): void
38-
{
39-
$this->_listeners[$listener->getListener()] = $listener;
40-
}
41-
42-
/**
43-
* @inheritDoc
44-
*/
45-
public function getListeners(): array
46-
{
47-
return $this->_listeners;
48-
}
49-
5029
/**
5130
* @inheritDoc
5231
*/

src/Interfaces/RouteInterface.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,6 @@ interface RouteInterface
1313
*/
1414
public function getName(): string;
1515

16-
/**
17-
* Get listeners linked to route;
18-
*
19-
* @return mixed
20-
*/
21-
public function getListeners(): array;
22-
23-
/**
24-
* Setup new listener
25-
*
26-
* @param Listener $listener
27-
* @return void
28-
*/
29-
public function setListener(Listener $listener): void;
30-
31-
/**
32-
* Check if listeners are empty or not
33-
*
34-
* @return bool
35-
*/
36-
public function hasListeners(): bool;
37-
3816
/**
3917
* Get route blocks
4018
*

src/Traits/HasListeners.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Pavlusha311245\UnitPhpSdk\Traits;
4+
5+
use Pavlusha311245\UnitPhpSdk\Config\Listener;
6+
7+
trait HasListeners
8+
{
9+
private array $_listeners = [];
10+
11+
/**
12+
* Setup new listener
13+
*
14+
* @param Listener $listener
15+
* @return void
16+
*/
17+
public function setListener(Listener $listener): void
18+
{
19+
$this->_listeners[$listener->getListener()] = $listener;
20+
}
21+
22+
/**
23+
* Get listeners linked to object;
24+
*
25+
* @return mixed
26+
*/
27+
public function getListeners(): array
28+
{
29+
return $this->_listeners;
30+
}
31+
32+
/**
33+
* Check if listeners are empty or not
34+
*
35+
* @return bool
36+
*/
37+
public function hasListeners(): bool
38+
{
39+
return !empty($this->_listeners);
40+
}
41+
}

0 commit comments

Comments
 (0)