Skip to content

Commit 7d31425

Browse files
committed
save.
1 parent c87652a commit 7d31425

File tree

17 files changed

+366
-4
lines changed

17 files changed

+366
-4
lines changed

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"Test::test_foo_get":5},"times":{"Test::test_foo_get":0.024}}

App/Connectors/IConnector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Connectors;
4+
5+
interface IConnector
6+
{
7+
public function getConnection();
8+
}

App/Connectors/Mongodb.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Connectors;
4+
5+
class Mongodb implements IConnector
6+
{
7+
8+
private ?\MongoDB\Client $connection = null;
9+
10+
public function __construct()
11+
{
12+
$this->connection = new \MongoDB\Client('mongodb://localhost:27017');
13+
}
14+
15+
public function getConnection(): ?\MongoDB\Client
16+
{
17+
return $this->connection;
18+
}
19+
}

App/Connectors/Mysql.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Connectors;
4+
5+
use PDO;
6+
7+
class Mysql implements IConnector
8+
{
9+
private ?PDO $connection = null;
10+
11+
public function __construct()
12+
{
13+
$config = database()['mysql'];
14+
$dsn = sprintf("mysql:host=%s;dbname=%s;",$config['host'],$config['dbname']);
15+
$this->connection = new PDO($dsn,$config['username'],$config['password']);
16+
}
17+
18+
19+
public function getConnection(): ?PDO
20+
{
21+
return $this->connection;
22+
}
23+
}

App/Controllers/FooController.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use App\Models\Foo;
6+
use ZankoKhaledi\PhpSimpleRouter\HTTPResponse;
7+
use ZankoKhaledi\PhpSimpleRouter\Request;
8+
9+
class FooController
10+
{
11+
12+
protected ?Foo $model = null;
13+
14+
public function __construct()
15+
{
16+
$this->model = new Foo();
17+
}
18+
19+
20+
public function index(Request $request)
21+
{
22+
$data = $this->model->all();
23+
24+
$httpResponse = new HTTPResponse();
25+
26+
$response = $httpResponse->json($data,200);
27+
28+
echo $response;
29+
}
30+
31+
32+
public function store(Request $request)
33+
{
34+
$model = $this->model;
35+
36+
$response = $model->create([
37+
'name' => $request->post('name')
38+
]);
39+
40+
$httpResponse = new HTTPResponse();
41+
42+
echo $httpResponse->json([
43+
'message' => $response
44+
],HTTPResponse::CREATED);
45+
}
46+
}

App/Database.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\Connectors\IConnector;
6+
use PDO;
7+
8+
class Database
9+
{
10+
private mixed $connection = null;
11+
12+
private static $instance = null;
13+
14+
private function __construct()
15+
{
16+
17+
}
18+
19+
public function __clone()
20+
{
21+
22+
}
23+
24+
private static function getInstance():?static
25+
{
26+
if(static::$instance === null){
27+
static::$instance = new static();
28+
}
29+
return static::$instance;
30+
}
31+
32+
public static function connection(IConnector $connector)
33+
{
34+
return static::getInstance()->connection = $connector->getConnection();
35+
}
36+
}

App/Models/Bar.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Connectors\Mongodb;
6+
use App\Database;
7+
use MongoDB\Client;
8+
9+
class Bar
10+
{
11+
12+
protected $table_name = 'test';
13+
14+
protected ?Client $database = null;
15+
16+
public function __construct()
17+
{
18+
$this->database = Database::connection(new Mongodb());
19+
}
20+
21+
22+
public function store(array $data)
23+
{
24+
$collection = (new Client())->zanko->test;
25+
26+
$product = $collection->insertOne($data);
27+
28+
if($product->getInsertedCount() > 0){
29+
return 'Product created';
30+
}
31+
}
32+
}

App/Models/Foo.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Connectors\Mysql;
6+
use App\Database;
7+
use ZankoKhaledi\PhpSimpleRouter\HTTPResponse;
8+
9+
class Foo
10+
{
11+
12+
protected string $table_name = 'test';
13+
14+
protected ?\PDO $database = null;
15+
16+
public function __construct()
17+
{
18+
$this->database = Database::connection(new Mysql());
19+
}
20+
21+
public function all()
22+
{
23+
$db = $this->database;
24+
25+
$stmt = $db->prepare("SELECT * FROM {$this->table_name}");
26+
27+
$stmt->execute();
28+
29+
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
30+
}
31+
32+
33+
public function create(array $data)
34+
{
35+
$db = $this->database;
36+
37+
try {
38+
$db->beginTransaction();
39+
40+
$stmt = $db->prepare("INSERT INTO {$this->table_name}(name) VALUES(:name)");
41+
42+
$stmt->execute([
43+
':name' => $data['name']
44+
]);
45+
46+
$db->commit();
47+
}catch (\Exception $exception){
48+
$db->rollBack();
49+
50+
return $exception->getMessage();
51+
}
52+
53+
if($stmt->rowCount()){
54+
return "user created.";
55+
}
56+
}
57+
}

Test.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use PHPUnit\Framework\TestCase;
5+
use ZankoKhaledi\PhpSimpleRouter\Router;
6+
7+
require_once __DIR__ . '/App/Controllers/FooController.php';
8+
9+
class Test extends TestCase
10+
{
11+
12+
protected string $baseUri = 'http://localhost:8000';
13+
14+
public function test_get_route()
15+
{
16+
$request = Router::assertRequest($this->baseUri);
17+
$response = $request->assertGet('/foo', []);
18+
19+
$this->assertEquals(200, $response->status());
20+
$this->assertJson($response->json());
21+
$this->assertSame(json_encode([
22+
'name' => 'Zanko'
23+
]), $response->body());
24+
}
25+
26+
27+
public function test_zanko_post()
28+
{
29+
$request = Router::assertRequest($this->baseUri);
30+
$response = $request->assertPost('/foo', [
31+
'form_params' => [
32+
'name' => 'Teddy'
33+
]
34+
]);
35+
36+
$this->assertEquals(201, $response->status());
37+
$this->assertJson($response->json());
38+
$this->assertSame(json_encode([
39+
'name' => 'Foo'
40+
]), $response->json());
41+
}
42+
43+
}

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"license": "MIT",
44
"require": {
55
"php": ">= 8.1",
6-
"guzzlehttp/guzzle": "7.0",
6+
"guzzlehttp/guzzle": "^7.0",
77
"phpunit/phpunit" : "9.*"
88
},
99
"keywords": [
@@ -15,8 +15,12 @@
1515
],
1616
"autoload": {
1717
"psr-4": {
18-
"ZankoKhaledi\\PhpSimpleRouter\\": "src/"
19-
}
18+
"ZankoKhaledi\\PhpSimpleRouter\\": "src/",
19+
"App\\" : "App/"
20+
},
21+
"files": [
22+
"config/app.php"
23+
]
2024
},
2125
"authors": [
2226
{

0 commit comments

Comments
 (0)