Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. <?php
  2. // conexão
  3. require_once 'Registry.php';
  4.  
  5. $Conn = new PDO('pgsql:host=localhost port=5432 dbname=BANCO user=USER password=PASS');
  6.  
  7. $Registry = Registry::getInstance();
  8.  
  9. $Registry->set( 'PDO' , $Conn );
  10.  
  11. $path = $Conn->query('SET search_path TO squema1;');
  12. $Conn = Registry::getInstance()->get( 'PDO' );
  13.  
  14. $Conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 
  15.  
  16. ?>
  17.  
  18.  
  19.  
  20. <?php
  21.  
  22. /**
  23.  
  24.  * Exemplo de Registry
  25.  
  26.  */
  27.  
  28. class Registry {
  29.  
  30.         /**
  31.  
  32.          * Instância única de Registry
  33.  
  34.          * @var Registry
  35.  
  36.          */
  37.  
  38.         private static $instance;
  39.  
  40.  
  41.  
  42.         /**
  43.  
  44.          * Nosso registro
  45.  
  46.          * @var ArrayObject
  47.  
  48.          */
  49.  
  50.         private $storage;
  51.  
  52.  
  53.  
  54.         /**
  55.  
  56.          * Registry é um caso de uso de Singleton
  57.  
  58.          */
  59.  
  60.         protected function __construct() {
  61.  
  62.                 $this->storage = new ArrayObject();
  63.  
  64.         }
  65.  
  66.  
  67.  
  68.         /**
  69.  
  70.          * Recupera um registro utilizando sua chave
  71.  
  72.          * @param string $key
  73.  
  74.          * @return mixed O valor armazenado
  75.  
  76.          * @throws RuntimeException Se não houver um registro para a chave especificada
  77.  
  78.          */
  79.  
  80.         public function get( $key ) {
  81.  
  82.                 if ( $this->storage->offsetExists( $key ) ) {
  83.  
  84.                         return $this->storage->offsetGet( $key );
  85.  
  86.                 } else {
  87.  
  88.                         throw new RuntimeException( sprintf( 'Não existe um registro para a chave "%s".' , $key ) );
  89.  
  90.                 }
  91.  
  92.         }
  93.  
  94.  
  95.  
  96.         /**
  97.  
  98.          * Recupera a instância única de Registry
  99.  
  100.          * @return Registry
  101.  
  102.          */
  103.  
  104.         public static function getInstance() {
  105.  
  106.                 if ( !self::$instance )
  107.  
  108.                         self::$instance = new Registry();
  109.  
  110.  
  111.  
  112.                 return self::$instance;
  113.  
  114.         }
  115.  
  116.  
  117.  
  118.         /**
  119.  
  120.          * Registra um valor à uma chave
  121.  
  122.          * @param string $key A chave que será utilizada no registro
  123.  
  124.          * @param mixed $value O valor que será registrado
  125.  
  126.          * @throws LogicException Se a chave já estiver registrada
  127.  
  128.          */
  129.  
  130.         public function set( $key , $value ) {
  131.  
  132.                 if ( !$this->storage->offsetExists( $key ) ) {
  133.  
  134.                         $this->storage->offsetSet( $key , $value );
  135.  
  136.                 } else {
  137.  
  138.                         throw new LogicException( sprintf( 'Já existe um registro para a chave "%s".' , $key ) );
  139.  
  140.                 }
  141.  
  142.         }
  143.  
  144.  
  145.  
  146.         /**
  147.  
  148.          * Remove o registro de uma chave específica
  149.  
  150.          * @param string $key A chave que será removida
  151.  
  152.          * @throws RuntimeException Se não houver um registro para a chave especificada
  153.  
  154.          */
  155.  
  156.         public function unregister( $key ) {
  157.  
  158.                 if ( $this->storage->offsetExists( $key ) ) {
  159.  
  160.                         $this->storage->offsetUnset( $key );
  161.  
  162.                 } else {
  163.  
  164.                         throw new RuntimeException( sprintf( 'Não existe um registro para a chave "%s".' , $key ) );
  165.  
  166.                 }
  167.  
  168.         }
  169.  
  170. }
  171.  
  172. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement