Autodoc
  • Namespace
  • Class
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • ActiveRecordCache
  • CollectDependenciesEvent
  • Connection
  • Connections
  • DateTimePropertySupport
  • Helpers
  • Hooks
  • Model
  • Models
  • Query
  • RunTimeActiveRecordCache
  • Statement
  • Table

Interfaces

  • ActiveRecordCacheInterface

Traits

  • CreatedAtProperty
  • DateTimeProperty
  • UpdatedAtProperty

Exceptions

  • ActiveRecordException
  • ConnectionAlreadyEstablished
  • ConnectionNotDefined
  • ConnectionNotEstablished
  • ModelAlreadyInstantiated
  • ModelNotDefined
  • RecordNotFound
  • ScopeNotDefined
  • StatementInvalid

Functions

  • get_model
  1 <?php
  2 
  3 /*
  4  * This file is part of the ICanBoogie package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 10  */
 11 
 12 namespace ICanBoogie\ActiveRecord;
 13 
 14 use ICanBoogie\PropertyNotDefined;
 15 
 16 /**
 17  * Connection collection.
 18  *
 19  * @property-read array[string]array $definitions Connection definitions.
 20  * @property-read Database $established Established connections.
 21  */
 22 class Connections implements \ArrayAccess, \IteratorAggregate
 23 {
 24     /**
 25      * Connection definitions.
 26      *
 27      * @var array[string]array
 28      */
 29     private $definitions;
 30 
 31     /**
 32      * Established connections.
 33      *
 34      * @var array[string]Database
 35      */
 36     private $established = [];
 37 
 38     /**
 39      * Initialize the {@link $definitions} property.
 40      *
 41      * @param array $definitions Connection definitions.
 42      */
 43     public function __construct(array $definitions)
 44     {
 45         foreach ($definitions as $id => $definition)
 46         {
 47             $this[$id] = $definition;
 48         }
 49     }
 50 
 51     /**
 52      * Returns the read-only properties {@link $definitions} and {@link $established}.
 53      *
 54      * @param string $property
 55      *
 56      * @return mixed
 57      */
 58     public function __get($property)
 59     {
 60         switch ($property)
 61         {
 62             case 'definitions': return $this->definitions;
 63             case 'established': return $this->established;
 64         }
 65 
 66         throw new PropertyNotDefined([ $property, $this ]);
 67     }
 68 
 69     /**
 70      * Checks if a connection definition exists.
 71      */
 72     public function offsetExists($id)
 73     {
 74         return isset($this->definitions[$id]);
 75     }
 76 
 77     /**
 78      * Sets the definition of a connection.
 79      *
 80      * @throws ConnectionAlreadyEstablished in attempt to set the definition of an already
 81      * established connection.
 82      */
 83     public function offsetSet($id, $definition)
 84     {
 85         if (isset($this->established[$id]))
 86         {
 87             throw new ConnectionAlreadyEstablished($id);
 88         }
 89 
 90         if (is_string($definition))
 91         {
 92             $definition = [ 'dsn' => $definition ];
 93         }
 94 
 95         if (empty($definition['dsn']))
 96         {
 97             throw new \InvalidArgumentException("<q>dsn</q> is empty or not defined.");
 98         }
 99 
100         $this->definitions[$id] = $definition;
101     }
102 
103     /**
104      * @throws ConnectionAlreadyEstablished in attempt to unset the definition of an already
105      * established connection.
106      */
107     public function offsetUnset($id)
108     {
109         if (isset($this->established[$id]))
110         {
111             throw new ConnectionAlreadyEstablished($id);
112         }
113 
114         unset($this->definitions[$id]);
115     }
116 
117     /**
118      * Returns a connection to the specified database.
119      *
120      * If the connection has not been established yet, it is created on the fly.
121      *
122      * @param string $id The name of the connection to get.
123      *
124      * @return Database
125      *
126      * @throws ConnectionNotDefined when the connection requested is not defined.
127      * @throws ConnectionNotEstablished when the connection failed.
128      */
129     public function offsetGet($id)
130     {
131         if (isset($this->established[$id]))
132         {
133             return $this->established[$id];
134         }
135 
136         if (!$this->offsetExists($id))
137         {
138             throw new ConnectionNotDefined($id);
139         }
140 
141         $options = $this->definitions[$id] + [
142 
143             'dsn' => null,
144             'username' => 'root',
145             'password' => null
146         ];
147 
148         $options['options'][Connection::ID] = $id;
149 
150         #
151         # we catch connection exceptions and rethrow them in order to avoid displaying sensible
152         # information such as the username or password.
153         #
154 
155         try
156         {
157             return $this->established[$id] = new Connection($options['dsn'], $options['username'], $options['password'], $options['options']);
158         }
159         catch (\PDOException $e)
160         {
161             throw new ConnectionNotEstablished("Connection not established: " . $e->getMessage() . ".", 500, $e);
162         }
163     }
164 
165     /**
166      * Returns an iterator for established connections.
167      */
168     public function getIterator()
169     {
170         return new \ArrayIterator($this->established);
171     }
172 }
173 
174 /*
175  * EXCEPTIONS
176  */
177 
178 /**
179  * Exception thrown when a connection is not defined.
180  */
181 class ConnectionNotDefined extends ActiveRecordException
182 {
183     public function __construct($id, $code=500, \Exception $previous=null)
184     {
185         parent::__construct("Connection not defined: {$id}.", $code, $previous);
186     }
187 }
188 
189 /**
190  * Exception thrown when a connection cannot be established.
191  */
192 class ConnectionNotEstablished extends ActiveRecordException
193 {
194 
195 }
196 
197 /**
198  * Exception thrown in attempt to set the definition of an already established connection.
199  */
200 class ConnectionAlreadyEstablished extends ActiveRecordException
201 {
202     public function __construct($id, $code=500, \Exception $previous=null)
203     {
204         parent::__construct("Connection already established: {$id}.", $code, $previous);
205     }
206 }
Autodoc API documentation generated by ApiGen 2.8.0