Commit 1cb7901e authored by Matías Pagano's avatar Matías Pagano

MVC para fede

parent 7bff3793
FROM php:5.6.31-apache
RUN apt-get update && apt-get install -y php5-mysql vim libmcrypt-dev && docker-php-ext-install mysqli pdo pdo_mysql mcrypt
Ejemplo de MVC con PHP y TWIG.
* Clonar.
* Levantar con docker: docker-compose up .
* Se carga la base que se encuentra en sql/ al iniciar.
* Se tiene el sitio en localhost.
\ No newline at end of file
{
"require": {
"twig/twig": "1.*"
}
}
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "ec6122536d2fd287fbfed1e23ebb8601",
"packages": [
{
"name": "twig/twig",
"version": "v1.16.0",
"source": {
"type": "git",
"url": "https://github.com/fabpot/Twig.git",
"reference": "8ce37115802e257a984a82d38254884085060024"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/fabpot/Twig/zipball/8ce37115802e257a984a82d38254884085060024",
"reference": "8ce37115802e257a984a82d38254884085060024",
"shasum": ""
},
"require": {
"php": ">=5.2.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.16-dev"
}
},
"autoload": {
"psr-0": {
"Twig_": "lib/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
},
{
"name": "Armin Ronacher",
"email": "armin.ronacher@active-4.com",
"role": "Project Founder"
},
{
"name": "Twig Team",
"homepage": "https://github.com/fabpot/Twig/graphs/contributors",
"role": "Contributors"
}
],
"description": "Twig, the flexible, fast, and secure template language for PHP",
"homepage": "http://twig.sensiolabs.org",
"keywords": [
"templating"
],
"time": "2014-07-05 12:19:05"
}
],
"packages-dev": [
],
"aliases": [
],
"minimum-stability": "stable",
"stability-flags": [
],
"prefer-stable": false,
"platform": [
],
"platform-dev": [
]
}
<?php
/**
* Description of ResourceController
*
* @author fede
*/
class ResourceController {
private static $instance;
public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
}
public function listResources(){
$resources = ResourceRepository::getInstance()->listAll();
$view = new SimpleResourceList();
$view->show($resources);
}
public function home(){
$view = new Home();
$view->show();
}
}
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
require_once('controller/ResourceController.php');
require_once('model/PDORepository.php');
require_once('model/ResourceRepository.php');
require_once('model/Resource.php');
require_once('view/TwigView.php');
require_once('view/SimpleResourceList.php');
require_once('view/Home.php');
if(isset($_GET["action"]) && $_GET["action"] == 'listResources'){
ResourceController::getInstance()->listResources();
}else{
ResourceController::getInstance()->home();
}
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of PDORepository
*
* @author fede
*/
abstract class PDORepository {
const USERNAME = "user";
const PASSWORD = "pass";
const HOST ="db";
const DB = "base";
private function getConnection(){
$u=self::USERNAME;
$p=self::PASSWORD;
$db=self::DB;
$host=self::HOST;
$connection = new PDO("mysql:dbname=$db;host=$host", $u, $p);
return $connection;
}
protected function queryList($sql, $args){
$connection = $this->getConnection();
$stmt = $connection->prepare($sql);
$stmt->execute($args);
return $stmt->fetchAll();
}
}
<?php
/**
* Description of Resource
*
* @author fede
*/
class Resource {
private $name;
private $url;
public function __construct($name, $url) {
$this->name = $name;
$this->url = $url;
}
public function getName() {
return $this->name;
}
public function getUrl() {
return $this->url;
}
}
<?php
/**
* Description of ResourceRepository
*
* @author fede
*/
class ResourceRepository extends PDORepository {
private static $instance;
public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
}
public function listAll() {
$answer = $this->queryList("select * from usuarios where estado=?;", ['Confirmado']);
$final_answer = [];
foreach ($answer as &$element) {
$final_answer[] = new Resource($element['apellido'].", ".$element['nombre'], $element['id']);
}
return $final_answer;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<ul>
<li><a href="./?action=listResources">Recursos</a></li>
</ul>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Recursos</title>
</head>
<body>
<h1>Recursos</h1>
<ul>
{% for item in resources %}
<li><a href="{{ item.url }}">{{ item.name }}</a></li>
{% endfor %}
</ul>
</body>
</html>
\ No newline at end of file
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit86b94df5367853ee0843a6565b180dcf::getLoader();
This diff is collapsed.
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Twig_' => array($vendorDir . '/twig/twig/lib'),
);
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);
<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit86b94df5367853ee0843a6565b180dcf
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit86b94df5367853ee0843a6565b180dcf', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit86b94df5367853ee0843a6565b180dcf', 'loadClassLoader'));
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);