Host Interfaces Model
Code
host-interface
├── .env
├── Dockerfile
├── database
│ ├── config.php
│ ├── database.php
│ ├── schema.mwb
│ └── schema.sql
├── docker-compose.yml
├── install
│ └── index.php
├── model
│ ├── host.php
│ └── interface.php
└── test.php
version: "3"
services:
web:
container_name: web
image: ifpb/php:7.3-apache-pdo
build: .
networks:
- app-tier
ports:
- 8080:80
volumes:
- ./:/var/www/html/
depends_on:
- mysql
mysql:
container_name: mysql
image: mysql:8.0
command:
- --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
networks:
- app-tier
ports:
- 3306:3306
adminer:
container_name: adminer
image: adminer:4.7.5
restart: always
networks:
- app-tier
ports:
- 8081:8080
depends_on:
- mysql
networks:
app-tier:
driver: bridge
FROM php:7.3-apache
RUN apt -y update \
&& apt install -y \
mariadb-client
# pdo_mysql package
RUN docker-php-ext-install pdo_mysql
.env:
MYSQL_ROOT_PASSWORD=secret
MYSQL_DATABASE=example
MYSQL_USER=devuser
MYSQL_PASSWORD=devpass
Database
DROP DATABASE IF EXISTS `computer`;
CREATE DATABASE `computer`;
USE `computer`;
DROP TABLE IF EXISTS `host`;
CREATE TABLE `host` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `host`
(`name`)
VALUES
('pc01');
DROP TABLE IF EXISTS `interface`;
CREATE TABLE `interface` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`ip` varchar(100) NOT NULL,
`mask` varchar(100) NOT NULL,
`mac` varchar(100) NOT NULL,
`host_id` int NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `ip_uniq` (`ip`),
INDEX `host_id` (`host_id`),
CONSTRAINT `host_id_fk`
FOREIGN KEY (`host_id`)
REFERENCES `host` (`id`)
);
INSERT INTO `interface`
(`name`, `ip`, `mask`, `mac`, `host_id`)
VALUES
('eth0', '216.58.222.100', '255.255.255.0', '08:00:27:8B:80:A3', 1),
('eth2', '10.0.1.10', '255.255.255.0', '08:00:27:8B:80:A4', 1);
Install
<?php
require_once(__DIR__.'/../database/config.php');
try {
$schema = file_get_contents('../database/schema.sql');
$connection = new PDO(DB.":host=".DBHOST, DBUSER, DBPWD);
$connection->exec($schema);
echo "Database installed!";
// header('Location: home.php');;
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
<?php
const DB = 'mysql';
const DBHOST = 'mysql';
const DBNAME = 'computer';
const DBUSER = 'root';
const DBPWD = 'secret';
PDO Model
Database Model
<?php
namespace Database;
use \PDO;
require_once('config.php');
class Database {
protected $connection;
function __construct(){
$dsn = DB.":dbname=".DBNAME.";host=".DBHOST;
try {
$this->connection = new PDO($dsn, DBUSER, DBPWD);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
}
Host Model
<?php
namespace Model;
use Database\Database;
use \PDO;
require_once __DIR__."/../database/database.php";
class Host extends Database {
public function create($name) {
$sql = "INSERT INTO host (name) VALUES ('${name}')";
try {
$this->connection->exec($sql);
return $this->connection->lastInsertId();
} catch(PDOExecption $e) {
$this->connection->rollback();
print "Error!: " . $e->getMessage();
return null;
}
}
public function read($id) {
$sql = "SELECT * FROM host WHERE id = ${id}";
$pdoStm = $this->connection->query($sql);
return $pdoStm ? $pdoStm->fetch(PDO::FETCH_ASSOC) : null;
}
public function readByName($name) {
$sql = "SELECT * FROM host WHERE name='${name}'";
$pdoStm = $this->connection->query($sql);
return $pdoStm ? $pdoStm->fetch(PDO::FETCH_ASSOC) : null;
}
public function readAll() {
$sql = "SELECT * FROM host";
$pdoStm = $this->connection->query($sql);
return $pdoStm ? $pdoStm->fetchAll(PDO::FETCH_ASSOC) : null;
}
public function readOrCreate($name) {
$result = $this->readByName($name);
if ($result) {
return $result;
} else {
return $this->create($name);
}
}
public function update($id, $name) {
$sql = "UPDATE host
SET name='${name}'
WHERE id=${id}";
try {
return $this->connection->exec($sql);
} catch(PDOExecption $e) {
$this->connection->rollback();
print "Error!: " . $e->getMessage();
}
}
public function remove($id) {
$sql = "DELETE FROM host WHERE id=${id}";
try {
return $this->connection->exec($sql);
} catch(PDOExecption $e) {
$this->connection->rollback();
print "Error!: " . $e->getMessage();
}
}
}
Interface Model
<?php
namespace Model;
use Database\Database;
use \PDO;
require_once __DIR__."/../database/database.php";
class NetInterface extends Database {
public function create($name, $ip, $mask, $mac, $host_id) {
$sql = "INSERT INTO interface (name, ip, mask, mac, host_id) VALUES ('${name}', '${ip}', '${mask}', '${mac}', ${host_id})";
try {
$this->connection->exec($sql);
return $this->connection->lastInsertId();
} catch(PDOExecption $e) {
$this->connection->rollback();
print "Error!: " . $e->getMessage();
return null;
}
}
public function read($id) {
$sql = "SELECT * FROM interface WHERE id = ${id}";
$pdoStm = $this->connection->query($sql);
return $pdoStm ? $pdoStm->fetch(PDO::FETCH_ASSOC) : null;
}
public function readByName($name) {
$sql = "SELECT * FROM interface WHERE name='${name}'";
$pdoStm = $this->connection->query($sql);
return $pdoStm ? $pdoStm->fetch(PDO::FETCH_ASSOC) : null;
}
public function readAll() {
$sql = "SELECT * FROM interface";
$pdoStm = $this->connection->query($sql);
return $pdoStm ? $pdoStm->fetchAll(PDO::FETCH_ASSOC) : null;
}
public function readOrCreate($name, $ip, $mask, $mac, $host_id) {
$result = $this->readByName($name);
if ($result) {
return $result;
} else {
return $this->create($name, $ip, $mask, $mac, $host_id);
}
}
public function update($id, $name, $ip, $mask, $mac, $host_id) {
$sql = "UPDATE interface
SET name='${name}', ip='${ip}', mask='${mask}', mac='${mac}', host_id=${host_id}
WHERE id=${id}";
try {
return $this->connection->exec($sql);
} catch(PDOExecption $e) {
$this->connection->rollback();
print "Error!: " . $e->getMessage();
}
}
public function remove($id) {
$sql = "DELETE FROM interface WHERE id=${id}";
try {
return $this->connection->exec($sql);
} catch(PDOExecption $e) {
$this->connection->rollback();
print "Error!: " . $e->getMessage();
}
}
}
How to CRUD
<pre>
<?php
use Model\Host;
use Model\NetInterface;
require_once('model/host.php');
require_once('model/interface.php');
$host = new Host();
$interface = new NetInterface();
// Create
$hostId = $host->create('pc2');
var_dump($hostId); //=> string(1) "2"
$ethId = $interface->create('eth0', '10.0.10.10', '255.255.255.0', '08:00:27:8B:80:A5', 2);
var_dump($ethId); //=> string(1) "3"
// Read
var_dump($host->read($hostId));
//=>
// array(2) {
// ["id"]=>string(1) "2"
// ["name"]=>string(3) "pc2"
// }
// Update
$host->update($hostId, 'pc02', 2);
var_dump($host->read($hostId));
//=>
// array(2) {
// ["id"]=>string(1) "2"
// ["name"]=>string(4) "pc02"
// }
// Delete
var_dump($host->remove($hostId)); //=> int(1)
var_dump($host->read($hostId)); //=> bool(false)
var_dump($host->read($ethId)); //=> bool(false)
?>
</pre>