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

Y stopchyk/hw1 #18

Open
wants to merge 5 commits into
base: YStopchyk/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
29 changes: 29 additions & 0 deletions .env.exapmle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#mysql
DB_HOST=mysql
DB_USERNAME=
DB_PASSWORD=
DB_DATABASE=
DB_ROOT_PASSWORD=root
DB_PORT=3306
#mysql port for docker mapping
DB_PORT_MAPPING=

#nginx
NGINX_PORT=80
NGINX_PORT_MAPPING=

#app
PHP_FPM_PORT=9000
PHP_FPM_PORT_MAPPING=

#redis
REDIS_HOST=redis
REDIS_PORT=6379
#redis port for docker mapping
REDIS_PORT_MAPPING=

#memcached
MEMCACHED_HOST=memcached
MEMCACHED_PORT=11211
#memcached port for docker mapping
MEMCACHED_PORT_MAPPING=
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
.DS_Store
.AppleDouble
.LSOverride
.env
.var
.vendor
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
start:
docker-compose -f ./docker-compose.yml --env-file .env exec app composer config --global discard-changes true
docker-compose -f ./docker-compose.yml --env-file .env exec app composer install --no-interaction --prefer-dist --no-progress --no-suggest
docker-compose -f ./docker-compose.yml --env-file .env exec nginx service nginx reload

docker-up:
docker-compose up -d --build
docker-down:
docker-compose down --remove-orphans
docker-build:
docker-compose build

start-otus: docker-up start
stop-otus: docker-down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
# PHP_2024

https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus

## Quickstart

1. [Install git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
2. [Install docker](https://docs.docker.com/install/)
3. [Install docker-compose](https://docs.docker.com/compose/install/)
4. [Install make](https://wiki.ubuntu.com/ubuntu-make)

```
cp .env.example .env
#Add mapping ports for redis, musql, memcached
```

###Run via console

```
docker-compose up -d --build
docker-compose exec app bash
composer install
```

###Run via make

```
make start-otus
```

###web

Add this lines to your hosts file

```
127.0.0.1 mysite.local
```
5 changes: 5 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

spl_autoload_register(function ($class) {
require_once __DIR__ . '/' . str_replace('\\', '/', $class . '.php');
});
34 changes: 34 additions & 0 deletions classes/Memcached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace classes;

class Memcached
{
private const DEFAULT_HOST = '127.0.0.1';
private const DEFAULT_PORT = 11211;

/**
* @param string $host
* @param int $port
*/
public function __construct(
private string $host = self::DEFAULT_HOST,
private int $port = self::DEFAULT_PORT
) {
}

/**
* @return false|string
*/
public function ping()
{
try {
$m = new \Memcached();
$m->addServer($this->host, $this->port);
} catch (\Exception $exception) {
return $exception->getMessage();
}

return json_encode($m->getVersion());
}
}
40 changes: 40 additions & 0 deletions classes/Mysql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace classes;

use PDO;
use PDOException;

class Mysql
{
private const DEFAULT_HOST = '127.0.0.1';
private const DEFAULT_DBNAME = 'test';
private const DEFAULT_PORT = 3306;

/**
* @param string $host
* @param int $port
*/
public function __construct(
private string $user,
private string $pass,
private string $host = self::DEFAULT_HOST,
private string $dbname = self::DEFAULT_DBNAME,
private int $port = self::DEFAULT_PORT
) {
}

/**
* @return string
*/
public function ping()
{
try {
$DBH = new PDO("mysql:host=$this->host;dbname=$this->dbname;port=$this->port", $this->user, $this->pass);
$DBH->query('SELECT 1');
return "established";
} catch (PDOException $e) {
return "error " . $e->getMessage();
}
}
}
50 changes: 50 additions & 0 deletions classes/Redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace classes;

use Predis\Client;

class Redis
{
private const DEFAULT_HOST = '127.0.0.1';
private const DEFAULT_PORT = 6379;
private const DEFAULT_TIMEOUT = 0.8;

/**
* @param string $host
* @param int $port
* @param float $timeout
*/
public function __construct(
private string $host = self::DEFAULT_HOST,
private int $port = self::DEFAULT_PORT,
private float $timeout = self::DEFAULT_TIMEOUT
) {
}

/**
* @return mixed|string
*/
public function ping()
{
$redis = $this->getClient();

try {
return $redis->ping();
} catch (\Exception $e) {
return $e->getMessage();
}
}

/**
* @return Client
*/
private function getClient(): Client
{
return new Client([
'host' => $this->host,
'port' => $this->port,
'timeout' => $this->timeout
]);
}
}
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "otus/ystopchyk",
"description": "Otus home work",
"minimum-stability": "dev",
"type": "project",
"require": {
"php": ">=8.0",
"vlucas/phpdotenv": "^5.6",
"predis/predis": "^2.2",
"ext-memcached": "^3.1",
"ext-pdo": "*"
},
"autoload": {
"psr-4": {
"app\\": "src/"
}
},
"prefer-stable": true,
"require-dev": {
"roave/security-advisories": "dev-latest"
}
}
Loading
Loading