From 99b15b7eba36f522b2e111b1e1379ae94ff4c3b0 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Fri, 26 Jul 2013 18:16:52 +0300 Subject: [PATCH] remove locator --- ipf/orm/access.php | 4 +- ipf/orm/collection.php | 6 -- ipf/orm/configurable.php | 2 +- ipf/orm/hydrator.php | 2 +- ipf/orm/hydrator/abstract.php | 4 +- ipf/orm/hydrator/recorddriver.php | 6 +- ipf/orm/locator.php | 93 ------------------------------- ipf/orm/locator/injectable.php | 57 ------------------- ipf/orm/manager.php | 2 - ipf/orm/null.php | 29 +++++++++- ipf/orm/record.php | 43 +++++++------- ipf/orm/record/iterator.php | 12 ++-- ipf/orm/table.php | 6 +- ipf/orm/validator.php | 4 +- 14 files changed, 64 insertions(+), 206 deletions(-) delete mode 100644 ipf/orm/locator.php delete mode 100644 ipf/orm/locator/injectable.php diff --git a/ipf/orm/access.php b/ipf/orm/access.php index 39ae574..d0a87d2 100644 --- a/ipf/orm/access.php +++ b/ipf/orm/access.php @@ -1,6 +1,6 @@ _table; diff --git a/ipf/orm/configurable.php b/ipf/orm/configurable.php index 887b2b8..b629ec0 100644 --- a/ipf/orm/configurable.php +++ b/ipf/orm/configurable.php @@ -1,6 +1,6 @@ _collections[] = $coll; } - public function getNullPointer() + public function getNullPointer() { - return self::$_null; + return IPF_ORM_Null::getInstance(); } public function getElement(array $data, $component) diff --git a/ipf/orm/locator.php b/ipf/orm/locator.php deleted file mode 100644 index aaf4998..0000000 --- a/ipf/orm/locator.php +++ /dev/null @@ -1,93 +0,0 @@ - $resource) { - if ($resource instanceof IPF_ORM_Locator_Injectable) { - $resource->setLocator($this); - } - $this->_resources[$name] = $resource; - } - } - self::$_instances[] = $this; - } - - public static function instance() - { - if (empty(self::$_instances)) { - $obj = new IPF_ORM_Locator(); - } - return current(self::$_instances); - } - - public function setClassPrefix($prefix) - { - $this->_classPrefix = $prefix; - } - - public function getClassPrefix() - { - return $this->_classPrefix; - } - - public function contains($name) - { - return isset($this->_resources[$name]); - } - - public function bind($name, $value) - { - $this->_resources[$name] = $value; - - return $this; - } - - public function locate($name) - { - if (isset($this->_resources[$name])) { - return $this->_resources[$name]; - } else { - $className = $name; - - if ( ! class_exists($className)) { - - $name = explode('.', $name); - $name = array_map('strtolower', $name); - $name = array_map('ucfirst', $name); - $name = implode('_', $name); - - $className = $this->_classPrefix . $name; - - if ( ! class_exists($className)) { - throw new IPF_ORM_Exception_Locator("Couldn't locate resource " . $className); - } - } - - $this->_resources[$name] = new $className(); - - if ($this->_resources[$name] instanceof IPF_ORM_Locator_Injectable) { - $this->_resources[$name]->setLocator($this); - } - - return $this->_resources[$name]; - } - - throw new IPF_ORM_Exception_Locator("Couldn't locate resource " . $name); - } - - public function count() - { - return count($this->_resources); - } - - public function getIterator() - { - return new ArrayIterator($this->_resources); - } -} diff --git a/ipf/orm/locator/injectable.php b/ipf/orm/locator/injectable.php deleted file mode 100644 index 69460d9..0000000 --- a/ipf/orm/locator/injectable.php +++ /dev/null @@ -1,57 +0,0 @@ -_locator = $locator; - return $this; - } - - public function getLocator() - { - if ( ! isset($this->_locator)) { - $this->_locator = IPF_ORM_Locator::instance(); - - } - return $this->_locator; - } - - public function locate($name) - { - if (isset($this->_resources[$name])) { - if (is_object($this->_resources[$name])) { - return $this->_resources[$name]; - } else { - // get the name of the concrete implementation - $concreteImpl = $this->_resources[$name]; - - return $this->getLocator()->locate($concreteImpl); - } - } else { - return $this->getLocator()->locate($name); - } - } - - public function bind($name, $resource) - { - $this->_resources[$name] = $resource; - - return $this; - } - - public static function initNullObject(IPF_ORM_Null $null) - { - self::$_null = $null; - } - - public static function getNullObject() - { - return self::$_null; - } -} \ No newline at end of file diff --git a/ipf/orm/manager.php b/ipf/orm/manager.php index ddeaaf7..74d68fe 100644 --- a/ipf/orm/manager.php +++ b/ipf/orm/manager.php @@ -11,8 +11,6 @@ class IPF_ORM_Manager extends IPF_ORM_Configurable implements Countable, Iterato private function __construct() { - IPF_ORM_Locator_Injectable::initNullObject(new IPF_ORM_Null); - $this->attributes = array( IPF_ORM::ATTR_LOAD_REFERENCES => true, IPF_ORM::ATTR_IDXNAME_FORMAT => "%s_idx", diff --git a/ipf/orm/null.php b/ipf/orm/null.php index 20e813d..6347618 100644 --- a/ipf/orm/null.php +++ b/ipf/orm/null.php @@ -1,13 +1,36 @@ _data[$column] = $default; $this->_modified[] = $column; $this->_state = IPF_ORM_Record::STATE_TDIRTY; @@ -182,9 +182,9 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab if (isset($tmp[$fieldName])) { $data[$fieldName] = $tmp[$fieldName]; } else if (array_key_exists($fieldName, $tmp)) { - $data[$fieldName] = self::$_null; + $data[$fieldName] = IPF_ORM_Null::getInstance(); } else if (!isset($this->_data[$fieldName])) { - $data[$fieldName] = self::$_null; + $data[$fieldName] = IPF_ORM_Null::getInstance(); } unset($tmp[$fieldName]); } @@ -210,7 +210,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab $name = $name[0]; } if ($exists) { - if (isset($this->_data[$name]) && $this->_data[$name] !== self::$_null) { + if (isset($this->_data[$name]) && !IPF_ORM_Null::isNull($this->_data[$name])) { $this->_id[$name] = $this->_data[$name]; } } @@ -219,7 +219,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab $names = $this->_table->getIdentifier(); foreach ($names as $name) { - if ($this->_data[$name] === self::$_null) { + if (IPF_ORM_Null::isNull($this->_data[$name])) { $this->_id[$name] = null; } else { $this->_id[$name] = $this->_data[$name]; @@ -248,7 +248,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab foreach ($this->_data as $k => $v) { if ($v instanceof IPF_ORM_Record && $this->_table->getTypeOf($k) != 'object') { unset($vars['_data'][$k]); - } elseif ($v === self::$_null) { + } elseif (IPF_ORM_Null::isNull($v)) { unset($vars['_data'][$k]); } else { switch ($this->_table->getTypeOf($k)) { @@ -424,7 +424,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab if ( ! isset($this->_data[$fieldName])) { throw new IPF_ORM_Exception('Unknown property '. $fieldName); } - if ($this->_data[$fieldName] === self::$_null) { + if (IPF_ORM_Null::isNull($this->_data[$fieldName])) { return null; } @@ -444,14 +444,13 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab public function get($fieldName, $load = true) { - $value = self::$_null; + $value = IPF_ORM_Null::getInstance(); if (isset($this->_data[$fieldName])) { - // check if the value is the IPF_ORM_Null object located in self::$_null) - if ($this->_data[$fieldName] === self::$_null && $load) { + if (IPF_ORM_Null::isNull($this->_data[$fieldName]) && $load) { $this->load(); } - if ($this->_data[$fieldName] === self::$_null) { + if (IPF_ORM_Null::isNull($this->_data[$fieldName])) { $value = null; } else { $value = $this->_data[$fieldName]; @@ -503,7 +502,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab if ($this->_isValueModified($type, $old, $value)) { if ($value === null) { - $value = self::$_null; + $value = IPF_ORM_Null::getInstance(); } $this->_data[$fieldName] = $value; @@ -546,7 +545,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab $rel = $this->_table->getRelation($name); if ($value === null) { - $value = self::$_null; + $value = IPF_ORM_Null::getInstance(); } // one-to-many or one-to-one relation @@ -561,7 +560,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab return $this; } } else { - if ($value !== self::$_null) { + if (!IPF_ORM_Null::isNull($value)) { $relatedTable = $value->getTable(); $foreignFieldName = $relatedTable->getFieldName($rel->getForeign()); $localFieldName = $this->_table->getFieldName($rel->getLocal()); @@ -605,9 +604,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab if (isset($this->_values[$fieldName])) { return true; } - if (isset($this->_references[$fieldName]) && - $this->_references[$fieldName] !== self::$_null) { - + if (isset($this->_references[$fieldName]) && !IPF_ORM_Null::isNull($this->_references[$fieldName])) { return true; } return false; @@ -620,7 +617,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab } else if (isset($this->_references[$name])) { if ($this->_references[$name] instanceof IPF_ORM_Record) { $this->_pendingDeletes[] = $this->$name; - $this->_references[$name] = self::$_null; + $this->_references[$name] = IPF_ORM_Null::getInstance(); } elseif ($this->_references[$name] instanceof IPF_ORM_Collection) { $this->_pendingDeletes[] = $this->$name; $this->_references[$name]->setData(array()); @@ -695,7 +692,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab foreach ($modifiedFields as $field) { $type = $this->_table->getTypeOf($field); - if ($this->_data[$field] === self::$_null) { + if (IPF_ORM_Null::isNull($this->_data[$field])) { $a[$field] = null; continue; } @@ -766,7 +763,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab $a = array(); foreach ($this as $column => $value) { - if ($value === self::$_null || is_object($value)) { + if (IPF_ORM_Null::isNull($value) || is_object($value)) { $value = null; } @@ -780,7 +777,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab if ($deep) { foreach ($this->_references as $key => $relation) { - if (! $relation instanceof IPF_ORM_Null) { + if (!IPF_ORM_Null::isNull($relation)) { $a[$key] = $relation->toArray($deep, $prefixKey); } } @@ -911,7 +908,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab $modified = array(); foreach ($data as $key => $val) { - if ( ! ($val instanceof IPF_ORM_Null)) { + if (!IPF_ORM_Null::isNull($val)) { $ret->_modified[] = $key; } } @@ -1185,7 +1182,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab if ($deep) { foreach ($this->_references as $name => $reference) { - if ( ! ($reference instanceof IPF_ORM_Null)) { + if (!IPF_ORM_Null::isNull($reference)) { $reference->free($deep); } } diff --git a/ipf/orm/record/iterator.php b/ipf/orm/record/iterator.php index c070cfb..505a990 100644 --- a/ipf/orm/record/iterator.php +++ b/ipf/orm/record/iterator.php @@ -3,26 +3,22 @@ class IPF_ORM_Record_Iterator extends ArrayIterator { private $record; - private static $null; + public function __construct(IPF_ORM_Record $record) { $this->record = $record; parent::__construct($record->getData()); } - public static function initNullObject(IPF_ORM_Null $null) - { - self::$null = $null; - } - public function current() { $value = parent::current(); - if ($value === self::$null) { + if (IPF_ORM_Null::isNull($value)) { return null; } else { return $value; } } -} \ No newline at end of file +} + diff --git a/ipf/orm/table.php b/ipf/orm/table.php index 9ea227d..325c90d 100644 --- a/ipf/orm/table.php +++ b/ipf/orm/table.php @@ -844,7 +844,7 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable public function enumValue($fieldName, $index) { - if ($index instanceof IPF_ORM_Null) { + if (IPF_ORM_Null::isNull($index)) { return $index; } @@ -942,8 +942,8 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable public function prepareValue($fieldName, $value, $typeHint = null) { - if ($value === self::$_null) { - return self::$_null; + if (IPF_ORM_Null::isNull($value)) { + return $value; } else if ($value === null) { return null; } else { diff --git a/ipf/orm/validator.php b/ipf/orm/validator.php index 497678e..526471e 100644 --- a/ipf/orm/validator.php +++ b/ipf/orm/validator.php @@ -1,6 +1,6 @@ getIncremented(); -- 2.49.0