├── .gitignore ├── Dockerfile ├── README.md ├── composer.json ├── composer.lock ├── docker-compose.yml ├── docker └── nginx │ └── app.conf ├── public └── index.php └── src └── Core └── Creational ├── Builder ├── Conceitual │ ├── ApplePhone.php │ ├── Request │ │ ├── BuilderRequest.php │ │ ├── MethodsEnum.php │ │ ├── Request.php │ │ └── RequestInterface.php │ ├── SamsungPhone.php │ ├── SmartPhone.php │ ├── SmartPhoneBuilder.php │ ├── SmartPhoneBuilderInterface.php │ └── SmartPhoneCreator.php └── Practical │ ├── Address.php │ ├── Enums │ └── Role.php │ ├── Phone.php │ ├── User.php │ ├── UserBuilder.php │ └── UserBuilderInterface.php └── Singleton ├── Conceptual └── Singleton.php └── Practical ├── DbConnection.php └── Singleton.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /.idea 3 | /.vscode 4 | *.log 5 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:7.4-fpm 2 | 3 | # Arguments defined in docker-compose.yml 4 | ARG user 5 | ARG uid 6 | 7 | # Install system dependencies 8 | RUN apt-get update && apt-get install -y \ 9 | git \ 10 | curl \ 11 | libpng-dev \ 12 | libonig-dev \ 13 | libxml2-dev \ 14 | zip \ 15 | unzip 16 | 17 | # Clear cache 18 | RUN apt-get clean && rm -rf /var/lib/apt/lists/* 19 | 20 | # Install PHP extensions 21 | RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd sockets 22 | 23 | # Get latest Composer 24 | COPY --from=composer:latest /usr/bin/composer /usr/bin/composer 25 | 26 | # Create system user to run Composer and Artisan Commands 27 | RUN useradd -G www-data,root -u $uid -d /home/$user $user 28 | RUN mkdir -p /home/$user/.composer && \ 29 | chown -R $user:$user /home/$user 30 | 31 | # Install redis 32 | RUN pecl install -o -f redis \ 33 | && rm -rf /tmp/pear \ 34 | && docker-php-ext-enable redis 35 | 36 | # Set working directory 37 | WORKDIR /var/www 38 | 39 | USER $user -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Padrões de Projeto com PHP 8 2 | [Assine a Academy, e Seja VIP!](https://academy.especializati.com.br) 3 | 4 | Arquivos do curso GRATUITO [Padrões de Projeto com PHP 8](https://academy.especializati.com.br/curso/design-patterns) 5 | 6 | ### Passo a passo para rodar o projeto 7 | Clone Repositório 8 | ```sh 9 | git clone https://github.com/especializati/php-design-patterns.git 10 | ``` 11 | 12 | ```sh 13 | cd php-design-patterns 14 | ``` 15 | 16 | Acessar o container 17 | ```sh 18 | docker-compose exec app bash 19 | ``` 20 | 21 | 22 | Instalar as dependências do projeto 23 | ```sh 24 | composer install 25 | ``` 26 | 27 | 28 | Acessar o projeto 29 | [http://localhost:8989](http://localhost:8989) 30 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "carlosfgti/design-patterns", 3 | "description": "Padrões de Projeto com PHP", 4 | "autoload": { 5 | "psr-4": { 6 | "Core\\": "src/Core/" 7 | } 8 | }, 9 | "authors": [ 10 | { 11 | "name": "Carlos Ferreira", 12 | "email": "carlos@especializati.com.br" 13 | } 14 | ], 15 | "require": {} 16 | } 17 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "75359abe511880fd5ab0b300000debdd", 8 | "packages": [], 9 | "packages-dev": [], 10 | "aliases": [], 11 | "minimum-stability": "stable", 12 | "stability-flags": [], 13 | "prefer-stable": false, 14 | "prefer-lowest": false, 15 | "platform": [], 16 | "platform-dev": [], 17 | "plugin-api-version": "2.2.0" 18 | } 19 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.7" 2 | 3 | services: 4 | app: 5 | image: especializati/laravel-app 6 | restart: unless-stopped 7 | working_dir: /var/www/ 8 | volumes: 9 | - ./:/var/www 10 | depends_on: 11 | - redis 12 | networks: 13 | - design-patterns 14 | 15 | # nginx 16 | nginx: 17 | image: nginx:alpine 18 | restart: unless-stopped 19 | ports: 20 | - "8989:80" 21 | volumes: 22 | - ./:/var/www 23 | - ./docker/nginx/:/etc/nginx/conf.d/ 24 | networks: 25 | - design-patterns 26 | 27 | networks: 28 | design-patterns: 29 | driver: bridge -------------------------------------------------------------------------------- /docker/nginx/app.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | index index.php; 4 | root /var/www/public; 5 | 6 | location ~ \.php$ { 7 | try_files $uri =404; 8 | fastcgi_split_path_info ^(.+\.php)(/.+)$; 9 | fastcgi_pass app:9000; 10 | fastcgi_index index.php; 11 | include fastcgi_params; 12 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 13 | fastcgi_param PATH_INFO $fastcgi_path_info; 14 | } 15 | 16 | location / { 17 | try_files $uri $uri/ /index.php?$query_string; 18 | gzip_static on; 19 | } 20 | 21 | error_log /var/log/nginx/error.log; 22 | access_log /var/log/nginx/access.log; 23 | } -------------------------------------------------------------------------------- /public/index.php: -------------------------------------------------------------------------------- 1 | setAddress( 23 | // new Address( 24 | // street: 'Rua x', 25 | // city: 'City X', 26 | // state: 'State X', 27 | // postalCode: 5757009, 28 | // country: 'BR', 29 | // ) 30 | // ); 31 | // $carlosFounder->setPhone( 32 | // new Phone( 33 | // ddd: 64, 34 | // number: 981701406, 35 | // ) 36 | // ); 37 | // $user = (new UserBuilder) 38 | // ->addBasicInfo( 39 | // firstName: 'Carlos', 40 | // lastName: 'Ferreira', 41 | // email: 'carlos@especializati.com.br', 42 | // age: 29, 43 | // role: Role::F, 44 | // ) 45 | // ->addAddress( 46 | // street: 'Rua x', 47 | // city: 'City X', 48 | // state: 'State X', 49 | // postalCode: 5757009, 50 | // country: 'BR', 51 | // ) 52 | // ->addPhone( 53 | // ddd: 64, 54 | // number: 981701406, 55 | // ) 56 | // ->build(); 57 | 58 | /** 59 | * Singleton Conceitual 60 | */ 61 | // $instanceA = Singleton::getInstance(); 62 | // $instanceB = Singleton::getInstance(); 63 | // var_dump($instanceA === $instanceB); 64 | 65 | // $instance = DbConnection::getInstance(); 66 | // DbConnection::getInstance(); 67 | // DbConnection::getInstance(); 68 | // DbConnection::getInstance(); 69 | // DbConnection::getInstance(); 70 | // DbConnection::getInstance(); 71 | 72 | DbConnection::getConnection(); 73 | DbConnection::getConnection(); 74 | DbConnection::getConnection(); 75 | DbConnection::getConnection(); 76 | DbConnection::getConnection(); 77 | DbConnection::getConnection(); 78 | -------------------------------------------------------------------------------- /src/Core/Creational/Builder/Conceitual/ApplePhone.php: -------------------------------------------------------------------------------- 1 | request = new Request(); 12 | } 13 | 14 | public function url(string $url): RequestInterface 15 | { 16 | $this->request->url = $url; 17 | 18 | return $this; 19 | } 20 | 21 | public function payload(array $payload): RequestInterface 22 | { 23 | $this->request->payload = $payload; 24 | 25 | return $this; 26 | } 27 | 28 | public function method(MethodsEnum $method): RequestInterface 29 | { 30 | $this->request->method = $method; 31 | 32 | return $this; 33 | } 34 | 35 | public function build(): Request 36 | { 37 | return $this->request; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Core/Creational/Builder/Conceitual/Request/MethodsEnum.php: -------------------------------------------------------------------------------- 1 | validate(); 15 | } 16 | 17 | public function __set($name, $value) 18 | { 19 | $this->{$name} = $value; 20 | } 21 | 22 | public function __get($name) 23 | { 24 | return $this->{$name}; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Core/Creational/Builder/Conceitual/SmartPhoneBuilder.php: -------------------------------------------------------------------------------- 1 | smartPhone->gpu = $this->data['gpu']; 22 | 23 | return $this; 24 | } 25 | 26 | public function addCpu(): SmartPhoneBuilderInterface 27 | { 28 | $this->smartPhone->cpu = $this->data['cpu']; 29 | 30 | return $this; 31 | } 32 | 33 | public function addRam(): SmartPhoneBuilderInterface 34 | { 35 | $this->smartPhone->ram = $this->data['ram']; 36 | 37 | return $this; 38 | } 39 | 40 | public function addSensors(): SmartPhoneBuilderInterface 41 | { 42 | $this->smartPhone->sensors = $this->data['sensors']; 43 | 44 | return $this; 45 | } 46 | 47 | public function addModel(): SmartPhoneBuilderInterface 48 | { 49 | $this->smartPhone->model = $this->data['model']; 50 | 51 | return $this; 52 | } 53 | 54 | public function getSmartPhone(): SmartPhone 55 | { 56 | return $this->smartPhone; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/Core/Creational/Builder/Conceitual/SmartPhoneBuilderInterface.php: -------------------------------------------------------------------------------- 1 | smartPhone->addGpu(); 14 | $this->smartPhone->addCpu(); 15 | $this->smartPhone->addRam(); 16 | $this->smartPhone->addModel(); 17 | $this->smartPhone->addSensors(); 18 | 19 | return $this->smartPhone->getSmartPhone(); 20 | } 21 | 22 | public function buildPhoneWithoutSensors() 23 | { 24 | $this->smartPhone->addGpu(); 25 | $this->smartPhone->addCpu(); 26 | $this->smartPhone->addRam(); 27 | $this->smartPhone->addModel(); 28 | 29 | return $this->smartPhone->getSmartPhone(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Core/Creational/Builder/Practical/Address.php: -------------------------------------------------------------------------------- 1 | validate(); 20 | } 21 | 22 | public function setAddress(Address $address): void 23 | { 24 | $this->address = $address; 25 | 26 | // $this->validate(); 27 | } 28 | 29 | public function setPhone(Phone $phone): void 30 | { 31 | $this->phone = $phone; 32 | 33 | // $this->validate(); 34 | } 35 | 36 | public function __get($name) 37 | { 38 | return $this->{$name}; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Core/Creational/Builder/Practical/UserBuilder.php: -------------------------------------------------------------------------------- 1 | reset(); 14 | } 15 | 16 | public function reset(): void 17 | { 18 | $this->user = null; 19 | } 20 | 21 | public function addBasicInfo( 22 | string $firstName, 23 | string $lastName, 24 | string $email, 25 | int $age, 26 | Role $role, 27 | bool $active = false, 28 | ): UserBuilderInterface { 29 | $this->user = new User( 30 | firstName: $firstName, 31 | lastName: $lastName, 32 | email: $email, 33 | age: $age, 34 | role: $role, 35 | ); 36 | 37 | return $this; 38 | } 39 | 40 | public function addAddress( 41 | string $street, 42 | string $city, 43 | string $state, 44 | int $postalCode, 45 | string $country, 46 | ) : UserBuilderInterface { 47 | $address = new Address( 48 | street: $street, 49 | city: $city, 50 | state: $state, 51 | postalCode: $postalCode, 52 | country: $country, 53 | ); 54 | 55 | $this->user->setAddress($address); 56 | 57 | return $this; 58 | } 59 | 60 | public function addPhone( 61 | int $ddd, 62 | int $number, 63 | bool $active = true, 64 | ) : UserBuilderInterface { 65 | $this->user->setPhone(new Phone( 66 | ddd: $ddd, 67 | number: $number, 68 | )); 69 | 70 | return $this; 71 | } 72 | 73 | public function build(): User 74 | { 75 | return $this->user; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Core/Creational/Builder/Practical/UserBuilderInterface.php: -------------------------------------------------------------------------------- 1 | conn = new stdClass; 16 | } 17 | 18 | public static function getConnection(): stdClass 19 | { 20 | $instance = static::getInstance(); 21 | 22 | return $instance->conn; 23 | } 24 | } 25 | 26 | // use PDO; 27 | 28 | // class DbConnection extends Singleton 29 | // { 30 | // private ?PDO $conn = null; 31 | 32 | // protected function __construct() 33 | // { 34 | // $this->conn = new PDO("mysql:host=localhost,dbname=design_patterns", 'user_db', 'user_pass'); 35 | // } 36 | 37 | // public static function getConnection(): PDO 38 | // { 39 | // $instance = static::getInstance(); 40 | 41 | // return $instance->conn; 42 | // } 43 | // } -------------------------------------------------------------------------------- /src/Core/Creational/Singleton/Practical/Singleton.php: -------------------------------------------------------------------------------- 1 |