1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\Module;
use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\ConnectionCollection;
use ICanBoogie\Module;
/**
* Model collection.
*
* Extends the ActiveRecord model collection with the models defined by the modules.
*/
class ModelCollection extends ActiveRecord\ModelCollection
{
/**
* @var ModuleCollection
*/
protected $modules;
/**
* @param ConnectionCollection $connections Connections manager.
* @param ModuleCollection $modules ModuleCollection manager.
* @param array $definitions Model definitions.
*/
public function __construct(ConnectionCollection $connections, ModuleCollection $modules, array $definitions = [])
{
$this->modules = $modules;
parent::__construct($connections, $definitions);
}
/**
* Checks if a model exists by first checking if the module it belongs to is enabled and that
* it actually defines the model.
*
* @param string $id
*
* @return bool
*/
public function offsetExists($id)
{
list($module_id, $model_id) = explode('/', $id) + [ 1 => 'primary' ];
if (!isset($this->modules[$module_id]))
{
return parent::offsetExists($id);
}
$descriptor = $this->modules->descriptors[$module_id];
return isset($descriptor[Descriptor::MODELS][$model_id]);
}
/**
* Gets the specified model of the specified module.
*
* The pattern used to request a model is `<module_id>[/<model_id>]` where `<module_id>` is
* the identifier of the module and `<model_id>` is the identifier of the module's model. The
* `<model_id>` part is optional and defaults to `primary`.
*
* @param string $id Identifier of the model.
*
* @return ActiveRecord\Model
*/
public function offsetGet($id)
{
if (isset($this->instances[$id]))
{
return $this->instances[$id];
}
list($module_id, $model_id) = explode('/', $id) + [ 1 => 'primary' ];
if (!isset($this->modules[$module_id]))
{
return parent::offsetGet($id);
}
return $this->instances[$id] = $this->modules[$module_id]->model($model_id);
}
}