assessmentee.com

Home | Start test | Contact
 

Activerecord | Adapter | Decorator | DependencyInjectionContainer | Facade | Factory | Hydration | Inheritance | Iterator | Mapper | MVC | Observer | Prototype | Proxy | Registry | ServiceLocator | Singleton | Specification | Strategy | TableGateway | ZF1_TableGateway | ZF2_TableGateway |




    <?php

/**
 * Zend Framework I table gateway reconstructed implementation
 * The database adapter is set via a factory design pattern
 * The adapter is stored and retrieved via a singleton pattern.
 *
 */
abstract class Db_Adapter_Abstract
{

    protected $connection = null;
    protected $config;

    public function __construct($config)
    {
	$this->config = $config;
    }

    abstract protected function connect();

    public function query($sql)
    {
	$this->connect();
	return $this->connection->query($sql);
    }

}

abstract class Db_Adapter_Pdo_Abstract extends Db_Adapter_Abstract
{

    protected function dsn()
    {
	$dsn = $this->config;
	unset($dsn['username']);
	unset($dsn['password']);
	foreach ($dsn as $key => $val) {
	    $dsn[$key] = "$key=$val";
	}
	return $this->pdoType . ':' . implode(';', $dsn);
    }

    protected function connect()
    {
	$this->connection = new PDO(
		$this->dsn(), $this->config['username'], $this->config['password']
	);
    }

}

class Db_Adapter_Pdo_Mysql extends Db_Adapter_Pdo_Abstract
{

    protected $pdoType = 'mysql';

    protected function dsn()
    {
	// tweak for MySQL specifics
	$dsn = parent::dsn();
	return $dsn;
    }

    protected function connect()
    {
	// tweak for MySQL specifics
	parent::connect();
    }

}

class Db
{

    const FETCH_ASSOC = 2;

    public static function Factory($adapter, $config = array())
    {
	$adapterName = 'Db_Adapter_' . str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter))));
	// launch instance of Db_Adapter_Pdo_Mysql
	return new $adapterName($config);
    }

}

abstract class DB_Table_abstract
{

    protected $db;

    public function __construct()
    {
	$this->db = Registry::get($this->use_adapter);
    }

    public function fetchAll()
    {
	return $this->fetch("select * from " . $this->name);
    }

    protected function fetch($select)
    {
	$stmt = $this->db->query($select);

	$data = $stmt->fetchAll(Db::FETCH_ASSOC);
	return $data;
    }

}

class User extends DB_Table_Abstract
{

    protected $name = 'user';
    protected $use_adapter = 'db';

    public function __construct()
    {
	return parent::__construct();
    }

}

class Registry extends ArrayObject
{

    private static $registry = null;

    public static function getInstance()
    {
	if (self::$registry === null) {
	    self::$registry = new Registry;
	}
	return self::$registry;
    }

    public static function set($index, $value)
    {
	$instance = self::getInstance();
	$instance->offsetSet($index, $value);
    }

    public static function get($index)
    {
	$instance = self::getInstance();
	return $instance->offsetGet($index);
    }

}

$params = array(
    'adapter' => 'PDO_MYSQL',
    'host' => 'localhost',
    'username' => 'test',
    'password' => 'test',
    'dbname' => 'test');

$dbAdapter = Db::factory('PDO_MYSQL', $params);
Registry::set('db', $dbAdapter);

$user = new User;
$users = $user->fetchAll();
foreach ($users as $user) {
    echo $user['name'] . "<br />";
}

    

Outputs:


Larry
Clara



 

© 2018 assessmentee.com