Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decoupled calls among parent and heir classes.
Moved declaration of common interfaces to the acme/contracts package.
- Loading branch information
Grigori Kochanov
committed
Dec 7, 2020
1 parent
bd0630e
commit 33908a4
Showing
10 changed files
with
144 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
{ | |||
"name": "acme/contracts", | |||
"description": "a package for contracts between application and acme/image", | |||
"license": "MIT", | |||
"version": "0.1", | |||
"authors": [ | |||
{ | |||
"name": "Grigori Kochanov", | |||
"email": "public@grik.net" | |||
} | |||
], | |||
"autoload": { | |||
"psr-4": { | |||
"Acme\\Contracts\\": "src/" | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
<?php | |||
|
|||
|
|||
namespace Acme\Contracts; | |||
|
|||
|
|||
interface BaseModelInterface | |||
{ | |||
public function foo(); | |||
public function callBar(); | |||
} |
10 changes: 10 additions & 0 deletions
10
packages/acme-contracts/src/FooImplementationInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
<?php | |||
|
|||
namespace Acme\Contracts; | |||
|
|||
interface FooImplementationInterface | |||
{ | |||
public function withBaseModel(BaseModelInterface $baseModel): void; | |||
|
|||
public function doFoo(): mixed; | |||
} |
4 changes: 2 additions & 2 deletions
4
...me-image/src/Contracts/ImageInterface.php → ...ges/acme-contracts/src/ImageInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,13 +1,13 @@ | |||
<?php | <?php | ||
|
|
||
|
|
||
namespace Acme\Image\Contracts; | namespace Acme\Contracts; | ||
|
|
||
/** | /** | ||
* Interface ImageInterface | * Interface ImageInterface | ||
* @package Acme\Image | * @package Acme\Image | ||
*/ | */ | ||
interface ImageInterface | interface ImageInterface | ||
{ | { | ||
public function load(string $tmp_name): mixed; | public function load(string $id): mixed; | ||
} | } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,49 @@ | |||
<?php | |||
declare(strict_types=1); | |||
|
|||
namespace Acme; | |||
|
|||
use Acme\Contracts\{BaseModelInterface,FooImplementationInterface}; | |||
|
|||
/** | |||
* An adapter service for BaseModel to decouple inheritance into composition | |||
* | |||
* @package Acme | |||
*/ | |||
final class BaseModelAdapter extends \BaseModel implements BaseModelInterface | |||
{ | |||
public function __construct(private FooImplementationInterface $fooInstance) | |||
{ | |||
} | |||
|
|||
public function init():void | |||
{ | |||
parent::__construct(); | |||
} | |||
|
|||
public function getX(): int | |||
{ | |||
return $this->x; | |||
} | |||
|
|||
/** | |||
* Required by the interface of the BaseModel abstract class | |||
*/ | |||
public function foo() | |||
{ | |||
return $this->fooInstance->doFoo($this); | |||
} | |||
|
|||
/** | |||
* An adapter for a final protected method bar() | |||
*/ | |||
public function callBar() | |||
{ | |||
//print call trace after decoupling | |||
foreach (debug_backtrace() as ['function'=>$f,'class'=>$c]) { | |||
echo "$c::$f()\n"; | |||
} | |||
|
|||
return $this->bar(); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters