2006-09-30 13:54:12 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2006-09-30 13:54:12 +04:00
|
|
|
*/
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* Doctrine_Repository
|
|
|
|
* each record is added into Doctrine_Repository at the same time they are created,
|
|
|
|
* loaded from the database or retrieved from the cache
|
|
|
|
*
|
2006-12-29 17:01:31 +03:00
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Table
|
2006-12-29 17:01:31 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
class Doctrine_Table_Repository implements Countable, IteratorAggregate
|
|
|
|
{
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* @var object Doctrine_Table $table
|
|
|
|
*/
|
|
|
|
private $table;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* @var array $registry
|
|
|
|
* an array of all records
|
|
|
|
* keys representing record object identifiers
|
|
|
|
*/
|
|
|
|
private $registry = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*
|
|
|
|
* @param Doctrine_Table $table
|
|
|
|
*/
|
2008-01-12 22:49:11 +03:00
|
|
|
public function __construct(Doctrine_Mapper_Abstract $mapper)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2008-01-12 22:49:11 +03:00
|
|
|
$this->table = $mapper;
|
2006-09-30 13:54:12 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* getTable
|
|
|
|
*
|
|
|
|
* @return object Doctrine_Table
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getTable()
|
|
|
|
{
|
2006-09-30 13:54:12 +04:00
|
|
|
return $this->table;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* add
|
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record record to be added into registry
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function add(Doctrine_Record $record)
|
|
|
|
{
|
2006-09-30 13:54:12 +04:00
|
|
|
$oid = $record->getOID();
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($this->registry[$oid])) {
|
2006-09-30 13:54:12 +04:00
|
|
|
return false;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-09-30 13:54:12 +04:00
|
|
|
$this->registry[$oid] = $record;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* get
|
|
|
|
* @param integer $oid
|
|
|
|
* @throws Doctrine_Table_Repository_Exception
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function get($oid)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! isset($this->registry[$oid])) {
|
2006-09-30 13:54:12 +04:00
|
|
|
throw new Doctrine_Table_Repository_Exception("Unknown object identifier");
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-09-30 13:54:12 +04:00
|
|
|
return $this->registry[$oid];
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* count
|
|
|
|
* Doctrine_Registry implements interface Countable
|
|
|
|
* @return integer the number of records this registry has
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function count()
|
|
|
|
{
|
2006-09-30 13:54:12 +04:00
|
|
|
return count($this->registry);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* @param integer $oid object identifier
|
|
|
|
* @return boolean whether ot not the operation was successful
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function evict($oid)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! isset($this->registry[$oid])) {
|
2006-09-30 13:54:12 +04:00
|
|
|
return false;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-09-30 13:54:12 +04:00
|
|
|
unset($this->registry[$oid]);
|
|
|
|
return true;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* @return integer number of records evicted
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function evictAll()
|
|
|
|
{
|
2006-09-30 13:54:12 +04:00
|
|
|
$evicted = 0;
|
2008-01-05 22:55:56 +03:00
|
|
|
foreach ($this->registry as $oid => $record) {
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($this->evict($oid)) {
|
2006-09-30 13:54:12 +04:00
|
|
|
$evicted++;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-09-30 13:54:12 +04:00
|
|
|
}
|
|
|
|
return $evicted;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* getIterator
|
|
|
|
* @return ArrayIterator
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getIterator()
|
|
|
|
{
|
2006-09-30 13:54:12 +04:00
|
|
|
return new ArrayIterator($this->registry);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* contains
|
|
|
|
* @param integer $oid object identifier
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function contains($oid)
|
|
|
|
{
|
2006-09-30 13:54:12 +04:00
|
|
|
return isset($this->registry[$oid]);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* loadAll
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function loadAll()
|
|
|
|
{
|
2006-09-30 13:54:12 +04:00
|
|
|
$this->table->findAll();
|
|
|
|
}
|
2007-10-04 01:43:22 +04:00
|
|
|
}
|