Skip to content

Commit

Permalink
Moving a class to a local package and registering it as a new service
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigori Kochanov committed Dec 6, 2020
1 parent 4cfba01 commit de797ef
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 21 deletions.
10 changes: 8 additions & 2 deletions composer.json
Expand Up @@ -4,7 +4,8 @@
"require": { "require": {
"php": "^8.0", "php": "^8.0",
"symfony/dependency-injection": "^5.2", "symfony/dependency-injection": "^5.2",
"symfony/config": "^5.2" "symfony/config": "^5.2",
"acme/image": "^0.1"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
Expand All @@ -14,7 +15,12 @@
"src/BaseModel.php" "src/BaseModel.php"
] ]
}, },
"license": "MIT", "repositories": [
{
"type": "path",
"url": "./packages/acme-image"
}
], "license": "MIT",
"authors": [ "authors": [
{ {
"name": "Grigori Kochanov", "name": "Grigori Kochanov",
Expand Down
19 changes: 19 additions & 0 deletions packages/acme-image/composer.json
@@ -0,0 +1,19 @@
{
"name": "acme/image",
"description": "a demo module for splitting a monolith application",
"license": "proprietary",
"version": "0.1",
"authors": [
{
"name": "Grigori",
"email": "public@grik.net"
}
],
"require": {
},
"autoload": {
"psr-4": {
"Acme\\Image\\": "src/"
}
}
}
13 changes: 13 additions & 0 deletions packages/acme-image/src/Contracts/ImageInterface.php
@@ -0,0 +1,13 @@
<?php


namespace Acme\Image\Contracts;

/**
* Interface ImageInterface
* @package Acme\Image
*/
interface ImageInterface
{
public function load(string $tmp_name): mixed;
}
37 changes: 37 additions & 0 deletions packages/acme-image/src/ImageService.php
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace Acme\Image;

use Acme\Image\Contracts\ImageInterface;
use Acme\Image\lib\Image;

/**
* Class ImageService provides an API for the application.
* It is a bridge registered as a service assisting decoupling the module
* to change it independently from the service API.
*
* @package Acme\Image
* @api
*/
class ImageService implements ImageInterface
{

/**
* @var Image
*/
private Image $imageLib;

public function __construct()
{
$this->imageLib = new Image();
}

/**
* @param string $tmp_name
*/
public function load(string $tmp_name): mixed
{
return $this->imageLib->load($tmp_name);
}
}
20 changes: 20 additions & 0 deletions packages/acme-image/src/lib/Image.php
@@ -0,0 +1,20 @@
<?php

namespace Acme\Image\lib;

/**
* This class can now be changed without breaking the application
* because API is fixed in the ImageService class
*
* @internal
* @package Acme\Image
*/
class Image extends \BaseModel
{
public function load($tmp_name)
{
// calls to inherited methods
$this->foo();
parent::bar();
}
}
3 changes: 3 additions & 0 deletions services.php
Expand Up @@ -2,6 +2,8 @@


namespace Symfony\Component\DependencyInjection\Loader\Configurator; namespace Symfony\Component\DependencyInjection\Loader\Configurator;


use Acme\Image\{Contracts\ImageInterface,ImageService};

return function(ContainerConfigurator $configurator) { return function(ContainerConfigurator $configurator) {
/** /**
* @see https://symfony.com/doc/current/service_container/autowiring.html * @see https://symfony.com/doc/current/service_container/autowiring.html
Expand All @@ -11,4 +13,5 @@
$services->load('Acme\\', './src/*') $services->load('Acme\\', './src/*')
->exclude('./src/BaseModel.php'); ->exclude('./src/BaseModel.php');


$services->set(ImageInterface::class, ImageService::class);
}; };
18 changes: 0 additions & 18 deletions src/Image.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/ImageResizeController.php
Expand Up @@ -4,10 +4,12 @@
namespace Acme; namespace Acme;




use Acme\Image\Contracts\ImageInterface;

class ImageResizeController class ImageResizeController
{ {
public function __construct( public function __construct(
public Image $image public ImageInterface $image
){ ){
} }


Expand Down

0 comments on commit de797ef

Please sign in to comment.