File tree Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ declare (strict_types=1 );
3+
4+ namespace App \Repository ;
5+
6+
7+ use App \Model \Model ;
8+
9+ /**
10+ * @template T of Model
11+ * @property T $model
12+ */
13+ abstract class IRepository
14+ {
15+ /**
16+ * @return null|T
17+ */
18+ public function getById ($ id )
19+ {
20+ return $ this ->model ->newQuery ()->find ($ id );
21+ }
22+
23+ public function __construct ()
24+ {
25+ $ this ->setModel ();
26+ }
27+
28+ abstract function setModel (): void ;
29+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ declare (strict_types=1 );
3+
4+ namespace App \Repository ;
5+
6+ use App \Model \User ;
7+
8+ /**
9+ * @extends IRepository<User>
10+ */
11+ class UserRepository extends IRepository
12+ {
13+
14+ function setModel (): void
15+ {
16+ $ this ->model = new User ();
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ declare (strict_types=1 );
3+
4+ namespace App \Service ;
5+
6+
7+ use App \Repository \IRepository ;
8+
9+ /**
10+ * @template T
11+ * @property IRepository<T> $repository
12+ */
13+ abstract class IService
14+ {
15+ public function __construct ()
16+ {
17+ $ this ->repository = $ this ->repository ();
18+ }
19+ /**
20+ * @return T|null
21+ */
22+ public function getById ($ id )
23+ {
24+ return $ this ->repository ->getById ($ id );
25+ }
26+
27+ /**
28+ * @return IRepository<T>
29+ */
30+ abstract protected function repository ();
31+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ declare (strict_types=1 );
3+
4+ namespace App \Service ;
5+
6+ use App \Repository \IRepository ;
7+ use App \Repository \UserRepository ;
8+ use function App \Kernel \di ;
9+
10+ /**
11+ * @extends IService<UserRepository>
12+ */
13+ final class UserService extends IService
14+ {
15+
16+ protected function repository ()
17+ {
18+ return di (UserRepository::class);
19+ }
20+ }
You can’t perform that action at this time.
0 commit comments