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

D palm/hw17 #1128

Open
wants to merge 5 commits into
base: DPalm/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 composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "patterns/daniel",
"type": "project",
"autoload": {
"psr-4": {
"Patterns\\Daniel\\": "src/"
}
},
"authors": [
{
"name": "Daniel"
}
],
"require": {}
}
18 changes: 18 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: '3'
services:
nginx:
build:
context: ./nginx
dockerfile: Dockerfile
image: myapp/nginx
container_name: webserver
ports:
- "80:80"
volumes:
- .:/data
networks:
- app-network

app:
build:
context: ./fpm
dockerfile: Dockerfile
image: myapp/php
container_name: app
volumes:
- .:/data
networks:
- app-network

postgres:
container_name: postgresql
image: postgres:latest
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:

networks:
app-network:
driver: bridge
27 changes: 27 additions & 0 deletions fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Используйте официальный образ PHP 8.2 FPM
FROM php:8.2-fpm

# Установка зависимостей
RUN apt-get update && apt-get install -y \
libpq-dev \
&& docker-php-ext-install pdo_pgsql

# Установка Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Установка рабочей директории
WORKDIR /data

VOLUME /data

# Копирование вашего приложения в контейнер (предполагается, что ваш код находится в текущей директории)
COPY . /data

# Выставление прав на директорию
RUN chown -R www-data:www-data /var/www

# Открытие порта 9000
EXPOSE 9000

# Запуск PHP-FPM
CMD ["php-fpm"]
3 changes: 3 additions & 0 deletions fpm/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
session.save_handler = memcache
session.save_path = "tcp://memcache:11211"
extension=memcached.so
11 changes: 11 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM nginx:latest

COPY ./hosts/mysite.local.conf /etc/nginx/conf.d/mysite.local.conf

WORKDIR /data

VOLUME /data

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
29 changes: 29 additions & 0 deletions nginx/hosts/mysite.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
server {

listen 80;

server_name application.local;

root /data/public;

index index.php index.html;


location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
15 changes: 15 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

use Patterns\Daniel\App;
use Patterns\Daniel\Patterns\AbstractFactory\BurgerFactory;
use Patterns\Daniel\Patterns\Builder\OrderBuilder;
use Patterns\Daniel\Patterns\Observer\PreparationObserver;

$productFactory = new BurgerFactory();
$orderBuilder = new OrderBuilder();
$preparationObserver = new PreparationObserver();

$app = new App($productFactory, $orderBuilder, $preparationObserver);
$app->run();
56 changes: 56 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Patterns\Daniel;

use Patterns\Daniel\Patterns\AbstractFactory\ProductFactoryInterface;
use Patterns\Daniel\Patterns\Adapter\PizzaAdapter;
use Patterns\Daniel\Patterns\Builder\OrderBuilderInterface;
use Patterns\Daniel\Patterns\Decorator\IngredientsDecorator;
use Patterns\Daniel\Patterns\Observer\ObserverInterface;
use Patterns\Daniel\Products\Pizza;

class App
{
public function __construct(
private readonly ProductFactoryInterface $productFactory,
private readonly OrderBuilderInterface $orderBuilder,

Check failure on line 18 in src/App.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space between type hint and argument &quot;$orderBuilder&quot;; 3 found
private readonly ObserverInterface $preparationObserver,

Check failure on line 19 in src/App.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space between type hint and argument &quot;$preparationObserver&quot;; 7 found
)
{

Check failure on line 21 in src/App.php

View workflow job for this annotation

GitHub Actions / phpcs

The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line

}

Check failure on line 23 in src/App.php

View workflow job for this annotation

GitHub Actions / phpcs

Function closing brace must go on the next line following the body; found 1 blank lines before brace

public function run(): void
{
$burger = $this->productFactory->createBurger();

// Adding ingredients using the decorator
$customBurger = new IngredientsDecorator(new IngredientsDecorator($burger, "Sauce", 0.20), "Cheese", 0.50);

// Using the pizza adapter
$pizza = new Pizza(); // A pizza that does not implement ProductInterface initially.
$pizzaAdapter = new PizzaAdapter($pizza);

$this->orderBuilder->addProduct($customBurger)->addProduct($pizzaAdapter);
$order = $this->orderBuilder->getOrder();

$this->processOrder($order);
}

protected function processOrder($order): void
{
echo "Start of order processing...\n";

// There could be logic here to check and prepare the order, for example:
foreach ($order as $item) {
// Simulate processing of each order item
echo "Preparing: {$item['product']->getName()}\n";
// Assume we have a method to notify observers
$this->preparationObserver->update('order_prepared', ['orderId' => 123, 'status' => 'prepared']);
}

echo "The order is ready and can be handed over to the customer.\n";
}
}
33 changes: 33 additions & 0 deletions src/Patterns/AbstractFactory/BurgerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Patterns\Daniel\Patterns\AbstractFactory;

use Exception;
use Patterns\Daniel\Products\Burger;
use Patterns\Daniel\Products\ProductInterface;

class BurgerFactory implements ProductFactoryInterface
{
public function createBurger(): ProductInterface
{
return new Burger();
}

/**
* @throws Exception
*/
public function createSandwich(): ProductInterface
{
throw new Exception("Sandwiches are not made at BurgerFactory");
}

/**
* @throws Exception
*/
public function createHotDog(): ProductInterface
{
throw new Exception("Hot dogs are not made at BurgerFactory");
}
}
33 changes: 33 additions & 0 deletions src/Patterns/AbstractFactory/HotDogFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Patterns\Daniel\Patterns\AbstractFactory;

use Exception;
use Patterns\Daniel\Products\HotDog;
use Patterns\Daniel\Products\ProductInterface;

class HotDogFactory implements ProductFactoryInterface
{
public function createHotDog(): ProductInterface
{
return new HotDog();
}

/**
* @throws Exception
*/
public function createBurger(): ProductInterface
{
throw new Exception("Burgers are not made at HotDogFactory");
}

/**
* @throws Exception
*/
public function createSandwich(): ProductInterface
{
throw new Exception("Sandwiches are not made at HotDogFactory.");
}
}
16 changes: 16 additions & 0 deletions src/Patterns/AbstractFactory/ProductFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Patterns\Daniel\Patterns\AbstractFactory;

use Patterns\Daniel\Products\ProductInterface;

interface ProductFactoryInterface
{
public function createBurger(): ProductInterface;

public function createSandwich(): ProductInterface;

public function createHotDog(): ProductInterface;
}
33 changes: 33 additions & 0 deletions src/Patterns/AbstractFactory/SandwichFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Patterns\Daniel\Patterns\AbstractFactory;

use Exception;
use Patterns\Daniel\Products\ProductInterface;
use Patterns\Daniel\Products\Sandwich;

class SandwichFactory implements ProductFactoryInterface
{
public function createSandwich(): ProductInterface
{
return new Sandwich();
}

/**
* @throws Exception
*/
public function createBurger(): ProductInterface
{
throw new Exception("Burgers are not made at SandwichFactory");
}

/**
* @throws Exception
*/
public function createHotDog(): ProductInterface
{
throw new Exception("Hot dogs are not made at SandwichFactory.");
}
}
36 changes: 36 additions & 0 deletions src/Patterns/Adapter/PizzaAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Patterns\Daniel\Patterns\Adapter;

use Patterns\Daniel\Products\Pizza;
use Patterns\Daniel\Products\ProductInterface;

class PizzaAdapter implements ProductInterface
{
/**
* @var Pizza
*/
private Pizza $pizza;

public function __construct(Pizza $pizza)
{
$this->pizza = $pizza;
}

public function getName(): string
{
return $this->pizza->getPizzaName();
}

public function getPrice(): float
{
return $this->pizza->getPizzaPrice();
}

public function getIngredients(): array
{
return $this->pizza->getPizzaIngredients();
}
}
Loading
Loading