Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ДЗ №16: Паттерны проектирования #1132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions hw16/.docker/php-fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM php:8.2-fpm

RUN mkdir -p /var/www/otusphp.local

WORKDIR /var/www/otusphp.local

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN pecl install xdebug-3.3.0 \
&& docker-php-ext-enable xdebug

RUN chown -R www-data:www-data /var/www/otusphp.local
COPY ./xdebug.ini /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

CMD ["php-fpm"]
8 changes: 8 additions & 0 deletions hw16/.docker/php-fpm/xdebug.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[xdebug]
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.client_host=host.docker.internal
xdebug.client_port=54321
xdebug.log=/var/www/otusphp.local/xdebug.log
xdebug.start_with_request=yes
xdebug.idekey=PHPSTORM
7 changes: 7 additions & 0 deletions hw16/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3"
services:
php-fpm:
container_name: php-fpm
build: ./.docker/php-fpm
volumes:
- ./www:/var/www/otusphp.local
15 changes: 15 additions & 0 deletions hw16/www/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "shabanov/otusphp",
"autoload": {
"psr-4": {
"Shabanov\\Otusphp\\": "src/"
}
},
"authors": [
{
"name": "Shabanov Vyacheslav",
"email": "[email protected]"
}
],
"require": {}
}
12 changes: 12 additions & 0 deletions hw16/www/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

Check failure on line 1 in hw16/www/public/index.php

View workflow job for this annotation

GitHub Actions / phpcs

Header blocks must be separated by a single blank line
declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use Shabanov\Otusphp\App;

try {
(new App())->run();
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
33 changes: 33 additions & 0 deletions hw16/www/src/Adapter/PizzaAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Shabanov\Otusphp\Adapter;

use Shabanov\Otusphp\Builder\ProductBuilder;
use Shabanov\Otusphp\Entity\Pizza;
use Shabanov\Otusphp\Interfaces\ProductInterface;
use Shabanov\Otusphp\Observer\Event;
use Shabanov\Otusphp\Services\Cooking;

class PizzaAdapter
{
private ProductInterface $pizza;
private Cooking $cooking;
public function __construct(private Event $event)
{
$this->createPizza();
$this->cooking = new Cooking($this->pizza, $this->event);
}

private function createPizza(): void
{
$this->pizza = (new ProductBuilder(new Pizza()))
->addSalad()
->addOnion()
->build();
}

public function cook(): void
{
$this->cooking->run();
}
}
61 changes: 61 additions & 0 deletions hw16/www/src/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Shabanov\Otusphp;

use Shabanov\Otusphp\Adapter\PizzaAdapter;
use Shabanov\Otusphp\Builder\ProductBuilder;
use Shabanov\Otusphp\Decorator\OnionIngradient;
use Shabanov\Otusphp\Decorator\PepperIngradient;
use Shabanov\Otusphp\Decorator\SaladIngradient;
use Shabanov\Otusphp\Entity\Client;
use Shabanov\Otusphp\Interfaces\ProductInterface;
use Shabanov\Otusphp\Observer\Event;
use Shabanov\Otusphp\Services\Cooking;

class App
{
private const PRODUCT = 'Shabanov\Otusphp\Fabrics\BurgerFabric';
private Event $event;
public function __construct() {}

Check failure on line 19 in hw16/www/src/App.php

View workflow job for this annotation

GitHub Actions / phpcs

Opening brace should be on a new line

Check failure on line 19 in hw16/www/src/App.php

View workflow job for this annotation

GitHub Actions / phpcs

Closing brace must be on a line by itself

public function run()
{
/**
* Создадим клиентов и слушатель событий
*/
$this->createClients();
/**
* Создадим продукт
*/
$product = $this->createProduct();
$product = (new ProductBuilder($product))
->addOnion()
->addPepper()
->addSalad()
->build();
/**
* Приготовим продукт
*/
$cooking = new Cooking($product, $this->event);
$cooking->run();
/**
* Пиццу приготовим через адаптер
*/
$pizzaAdapter = new PizzaAdapter($this->event);
$pizzaAdapter->cook();
}

private function createProduct(): ProductInterface
{
return self::PRODUCT::createProduct();
}

private function createClients(): void
{
$client1 = new Client('Вячеслав');
$client2 = new Client('Сергей');
$this->event = new Event();
$this->event->addSubscriber($client1)
->addSubscriber($client2);
}
}
36 changes: 36 additions & 0 deletions hw16/www/src/Builder/ProductBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Shabanov\Otusphp\Builder;

use Shabanov\Otusphp\Decorator\OnionIngradient;
use Shabanov\Otusphp\Decorator\PepperIngradient;
use Shabanov\Otusphp\Decorator\SaladIngradient;
use Shabanov\Otusphp\Interfaces\ProductInterface;

class ProductBuilder
{
public function __construct(private ProductInterface $product) {}

Check failure on line 12 in hw16/www/src/Builder/ProductBuilder.php

View workflow job for this annotation

GitHub Actions / phpcs

Opening brace should be on a new line

Check failure on line 12 in hw16/www/src/Builder/ProductBuilder.php

View workflow job for this annotation

GitHub Actions / phpcs

Closing brace must be on a line by itself

public function addOnion(): self
{
$this->product = new OnionIngradient($this->product);
return $this;
}

public function addPepper(): self
{
$this->product = new PepperIngradient($this->product);
return $this;
}

public function addSalad(): self
{
$this->product = new SaladIngradient($this->product);
return $this;
}

public function build(): ProductInterface
{
return $this->product;
}
}
15 changes: 15 additions & 0 deletions hw16/www/src/Decorator/AbstractIngradients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Shabanov\Otusphp\Decorator;

use Shabanov\Otusphp\Interfaces\ProductInterface;

abstract class AbstractIngradients implements ProductInterface
{
public function __construct(private readonly ProductInterface $product) {}

Check failure on line 9 in hw16/www/src/Decorator/AbstractIngradients.php

View workflow job for this annotation

GitHub Actions / phpcs

Opening brace should be on a new line

Check failure on line 9 in hw16/www/src/Decorator/AbstractIngradients.php

View workflow job for this annotation

GitHub Actions / phpcs

Closing brace must be on a line by itself

public function getInfo(): string
{
return $this->product->getInfo();
}
}
21 changes: 21 additions & 0 deletions hw16/www/src/Decorator/MultipleIngradients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Shabanov\Otusphp\Decorator;

use Shabanov\Otusphp\Interfaces\ProductInterface;

class MultipleIngradients extends AbstractIngradients
{
public function __construct(private ProductInterface $product,

Check failure on line 9 in hw16/www/src/Decorator/MultipleIngradients.php

View workflow job for this annotation

GitHub Actions / phpcs

The first parameter of a multi-line function declaration must be on the line after the opening bracket
private array $ingradients

Check failure on line 10 in hw16/www/src/Decorator/MultipleIngradients.php

View workflow job for this annotation

GitHub Actions / phpcs

Multi-line function declaration not indented correctly; expected 8 spaces but found 32
) {
foreach ($this->ingradients as $ingradient) {
$this->product = new $ingradient($this->product);
}
}

public function getInfo(): string
{
return $this->product->getInfo();
}
}
12 changes: 12 additions & 0 deletions hw16/www/src/Decorator/OnionIngradient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Shabanov\Otusphp\Decorator;

class OnionIngradient extends AbstractIngradients
{

Check failure on line 6 in hw16/www/src/Decorator/OnionIngradient.php

View workflow job for this annotation

GitHub Actions / phpcs

Opening brace must not be followed by a blank line

public function getInfo(): string
{
return parent::getInfo() . ' с луком';
}
}
11 changes: 11 additions & 0 deletions hw16/www/src/Decorator/PepperIngradient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Shabanov\Otusphp\Decorator;

class PepperIngradient extends AbstractIngradients
{
public function getInfo(): string
{
return parent::getInfo() . ' с перцем';
}
}
12 changes: 12 additions & 0 deletions hw16/www/src/Decorator/SaladIngradient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Shabanov\Otusphp\Decorator;

class SaladIngradient extends AbstractIngradients
{

public function getInfo(): string
{
return parent::getInfo() . ' с салатом';
}
}
14 changes: 14 additions & 0 deletions hw16/www/src/Entity/Burger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Shabanov\Otusphp\Entity;

use Shabanov\Otusphp\Interfaces\ProductInterface;

class Burger implements ProductInterface
{

public function getInfo(): string
{
return 'Бургер';
}
}
16 changes: 16 additions & 0 deletions hw16/www/src/Entity/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Shabanov\Otusphp\Entity;

use Shabanov\Otusphp\Interfaces\ObserverInterface;

class Client implements ObserverInterface
{
public function __construct(private string $name) {}


public function update($product, $status): void
{
echo $this->name . ': Статус приготовления ' . $product->getInfo() . ' -- ' . $status . PHP_EOL;
}
}
14 changes: 14 additions & 0 deletions hw16/www/src/Entity/HotDog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Shabanov\Otusphp\Entity;

use Shabanov\Otusphp\Interfaces\ProductInterface;

class HotDog implements ProductInterface
{

public function getInfo(): string
{
return 'Хот дог';
}
}
13 changes: 13 additions & 0 deletions hw16/www/src/Entity/Pizza.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Shabanov\Otusphp\Entity;

use Shabanov\Otusphp\Interfaces\ProductInterface;

class Pizza implements ProductInterface
{
public function getInfo(): string
{
return 'Пицца';
}
}
14 changes: 14 additions & 0 deletions hw16/www/src/Entity/Sandwich.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Shabanov\Otusphp\Entity;

use Shabanov\Otusphp\Interfaces\ProductInterface;

class Sandwich implements ProductInterface
{

public function getInfo(): string
{
return 'Сэндвич';
}
}
10 changes: 10 additions & 0 deletions hw16/www/src/Fabrics/AbstractProductFabric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Shabanov\Otusphp\Fabrics;

use Shabanov\Otusphp\Interfaces\ProductInterface;

abstract class AbstractProductFabric
{
abstract public static function createProduct(): ProductInterface;
}
15 changes: 15 additions & 0 deletions hw16/www/src/Fabrics/BurgerFabric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Shabanov\Otusphp\Fabrics;

use Shabanov\Otusphp\Entity\Burger;
use Shabanov\Otusphp\Interfaces\ProductInterface;

class BurgerFabric extends AbstractProductFabric
{

public static function createProduct(): ProductInterface
{
return new Burger();
}
}
15 changes: 15 additions & 0 deletions hw16/www/src/Fabrics/HotDogFabric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Shabanov\Otusphp\Fabrics;

use Shabanov\Otusphp\Entity\HotDog;
use Shabanov\Otusphp\Interfaces\ProductInterface;

class HotDogFabric extends AbstractProductFabric
{

public static function createProduct(): ProductInterface
{
return new HotDog();
}
}
Loading
Loading