Commit 700a5d25 authored by speedprogs_de's avatar speedprogs_de

completed object builder, moved directory "ExtensionProvider" to "Provider"

git-svn-id: https://svn.typo3.org/TYPO3v4/Extensions/terfe/branches/ter_fe2@51622 735d13b6-9817-0410-8766-e36946ffe9aa
parent 1d5eaf03
......@@ -131,7 +131,7 @@
* @param integer $versionNumber Version of the extension
* @return integer Result count
*/
public function countByExtKeyAndVersion($extKey, $versionNumber) {
public function countByExtKeyAndVersionNumber($extKey, $versionNumber) {
$query = $this->createQuery();
$query->matching(
$query->logicalAnd(
......
......@@ -29,40 +29,40 @@
class Tx_TerFe2_Object_ObjectBuilder implements t3lib_Singleton {
/**
* @var Tx_Extbase_Persistence_Mapper_DataMapper
* @var Tx_Extbase_Reflection_Service
*/
protected $dataMapper;
protected $reflectionService;
/**
* @var Tx_Extbase_Persistence_Session
* @var Tx_Extbase_Persistence_IdentityMap
*/
protected $persistenceSession;
protected $identityMap;
/**
* @var array
*/
protected $objects;
protected $classSchemata;
/**
* Injects the object storage
* Injects the identity map
*
* @param Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper
* @param Tx_Extbase_Persistence_IdentityMap $identityMap
* @return void
*/
public function injectDataMapper(Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper) {
$this->dataMapper = $dataMapper;
public function injectIdentityMap(Tx_Extbase_Persistence_IdentityMap $identityMap) {
$this->identityMap = $identityMap;
}
/**
* Injects the persistence session
/**
* Injects the reflection service
*
* @param Tx_Extbase_Persistence_Session $persistenceSession
* @param Tx_Extbase_Reflection_Service $reflectionService
* @return void
*/
public function injectPersistenceSession(Tx_Extbase_Persistence_Session $persistenceSession) {
$this->persistenceSession = $persistenceSession;
public function injectReflectionService(Tx_Extbase_Reflection_Service $reflectionService) {
$this->reflectionService = $reflectionService;
}
......@@ -70,86 +70,88 @@
* Create an object from given class and attributes
*
* @param string $className Name of the class
* @param string $identifier String to uniquely identify an object
* @param array $attributes Array of all class attributes
* @return void
* @return Tx_Extbase_DomainObject_DomainObjectInterface Stored object
*/
public function create($className, $identifier, array $attributes) {
if (empty($className) || empty($identifier) || empty($attributes)) {
public function create($className, array $attributes) {
if (empty($className) || empty($attributes)) {
throw new Exception('No valid params given to create an object');
}
if (!empty($this->objects[$identifier])) {
return;
}
$object = reset($this->dataMapper->map($className, array($attributes)));
$this->objects[$identifier] = clone($object);
$this->persistenceSession->unregisterReconstitutedObject($object);
unset($object);
}
/**
* Check if an object exists in storage
*
* @param string $identifier String to uniquely identify an object
* @return boolean TRUE if exists
*/
public function has($identifier) {
if (empty($identifier)) {
throw new Exception('No valid identifier given to check for an object');
// Check identity map
$identifier = md5(json_encode($attributes));
if ($this->identityMap->hasIdentifier($identifier, $className)) {
return $this->identityMap->getObjectByIdentifier($identifier, $className);
}
return (!empty($this->objects[$identifier]));
}
/**
* Return a stored object
*
* @param string $identifier String to uniquely identify an object
* @return Tx_Extbase_DomainObject_DomainObjectInterface Stored object
*/
public function get($identifier) {
if (empty($identifier)) {
throw new Exception('No valid identifier given to return an object');
// Build object
$classSchema = $this->getClassSchema($className);
$object = new $className();
foreach ($attributes as $key => $value) {
$propertyName = t3lib_div::underscoredToLowerCamelCase($key);
$protertyInfo = $classSchema->getProperty($propertyName);
if (empty($protertyInfo) || stripos($protertyInfo['type'], 'Tx_') !== FALSE) {
continue;
}
$value = $this->convertValue($value, $protertyInfo['type']);
$method = 'set' . ucfirst($propertyName);
if (method_exists($object, $method)) {
$object->$method($value);
}
}
if (!empty($this->objects[$identifier])) {
return $this->objects[$identifier];
}
return NULL;
}
// Register object in identity map
$this->identityMap->registerObject($object, $identifier);
/**
* Remove a stored object
*
* @param string $identifier String to uniquely identify an object
* @return void
*/
public function remove($identifier) {
if (empty($identifier)) {
throw new Exception('No valid identifier given to remove an object');
}
unset($this->objects[$identifier]);
return $object;
}
/**
* Returns all stored objects
* Returns the schema of a class
*
* @return array All objects
* @param string $className Name of the class
* @return Tx_Extbase_Reflection_ClassSchema Class schema
*/
public function getAll() {
return $this->objects;
protected function getClassSchema($className) {
if (empty($className)) {
throw new Exception('No valid class name given to create a class schema');
}
if (empty($this->classSchemata[$className])) {
$this->classSchemata[$className] = $this->reflectionService->getClassSchema($className);
}
return $this->classSchemata[$className];
}
/**
* Remove all stored objects
*
* @return void
* Convert value into correct type
*
* @param mixed $value Value to convert
* @param string $type Type of the conversation
* @return mixed Converted value
*/
public function removeAll() {
unset($this->objects);
protected function convertValue($value, $type) {
switch ($type) {
case 'int':
case 'integer':
return (int) $value;
break;
case 'float':
return (float) $value;
break;
case 'bool':
case 'boolean':
return (boolean) $value;
break;
case 'array':
return (array) $value;
break;
case 'string':
default:
return (string) $value;
break;
}
}
}
......
<?php
/*******************************************************************
* Copyright notice
*
* (c) 2011 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
******************************************************************/
/**
* Extension provider using local files
*/
class Tx_TerFe2_Object_ObjectBuilder implements t3lib_Singleton {
/**
* @var Tx_Extbase_Object_ObjectManager
*/
protected $objectManager;
/**
* @var Tx_Extbase_Reflection_Service
*/
protected $reflectionService;
/**
* @var Tx_Extbase_Persistence_Mapper_DataMapFactory
*/
protected $dataMapFactory;
/**
* @var Tx_Extbase_Persistence_Mapper_DataMapper
*/
protected $dataMapper;
/**
* @var Tx_Extbase_Persistence_Session
*/
protected $persistenceSession;
/**
* @var array
*/
protected $classSchemata;
/**
* @var array
*/
protected $objects;
/**
* Injects the reflection service
*
* @param Tx_Extbase_Reflection_Service $reflectionService
* @return void
*/
public function injectReflectionService(Tx_Extbase_Reflection_Service $reflectionService) {
$this->reflectionService = $reflectionService;
}
/**
* Injects the object manager
*
* @param Tx_Extbase_Object_ObjectManager $objectManager
* @return void
*/
public function injectObjectManager(Tx_Extbase_Object_ObjectManager $objectManager) {
$this->objectManager = $objectManager;
}
/**
* Injects the datamap factory
*
* @param Tx_Extbase_Persistence_Mapper_DataMapFactory $dataMapFactory
* @return void
*/
public function injectDataMapFactory(Tx_Extbase_Persistence_Mapper_DataMapFactory $dataMapFactory) {
$this->dataMapFactory = $dataMapFactory;
}
/**
* Injects the object storage
*
* @param Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper
* @return void
*/
public function injectDataMapper(Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper) {
$this->dataMapper = $dataMapper;
}
/**
* Injects the persistence session
*
* @param Tx_Extbase_Persistence_Session $persistenceSession
* @return void
*/
public function injectPersistenceSession(Tx_Extbase_Persistence_Session $persistenceSession) {
$this->persistenceSession = $persistenceSession;
}
/**
* Create an object from given class and attributes
*
* @param string $className Name of the class
* @param string $identifier String to uniquely identify an object
* @param array $attributes Array of all class attributes
* @return Tx_Extbase_DomainObject_DomainObjectInterface Stored object
*/
public function create($className, $identifier, array $attributes) {
if (empty($className) || empty($identifier) || empty($attributes)) {
throw new Exception('No valid params given to create an object');
}
if (!empty($this->objects[$identifier])) {
return $this->objects[$identifier];
}
// Filter attributes
$classSchema = $this->getClassSchema($className);
foreach ($attributes as $key => $value) {
$propertyName = t3lib_div::underscoredToLowerCamelCase($key);
$proterty = $classSchema->getProperty($propertyName);
if (empty($proterty)) {
unset($attributes[$key]);
}
if (stripos($proterty['type'], 'Tx_') === 0) {
$attributes[$key] = $this->objectManager->get($proterty['type']);
}
}
// Build object
$object = reset($this->dataMapper->map($className, array($attributes)));
$this->objects[$identifier] = clone($object);
$this->persistenceSession->unregisterReconstitutedObject($object);
unset($object);
return $this->objects[$identifier];
}
/**
* Check if an object exists in storage
*
* @param string $identifier String to uniquely identify an object
* @return boolean TRUE if exists
*/
public function has($identifier) {
if (empty($identifier)) {
throw new Exception('No valid identifier given to check for an object');
}
return (!empty($this->objects[$identifier]));
}
/**
* Check if an stored object exists and has given class
*
* @param string $identifier String to uniquely identify an object
* @return boolean TRUE if exists
*/
public function hasClass($identifier, $className) {
if (empty($identifier) || empty($className)) {
throw new Exception('No valid identifier or class name given to check an object');
}
return ($this->getClass($identifier) === $className);
}
/**
* Return a stored object
*
* @param string $identifier String to uniquely identify an object
* @return Tx_Extbase_DomainObject_DomainObjectInterface Stored object
*/
public function get($identifier) {
if (empty($identifier)) {
throw new Exception('No valid identifier given to return an object');
}
if (!empty($this->objects[$identifier])) {
return $this->objects[$identifier];
}
return NULL;
}
/**
* Return the class of a stored object
*
* @param string $identifier String to uniquely identify an object
* @return string The class of a stored object
*/
public function getClass($identifier) {
if (empty($identifier)) {
throw new Exception('No valid identifier given to return an object class');
}
if (!empty($this->objects[$identifier])) {
return get_class($this->objects[$identifier]);
}
return '';
}
/**
* Returns all stored objects
*
* @return array All objects
*/
public function getAll() {
return $this->objects;
}
/**
* Remove a stored object
*
* @param string $identifier String to uniquely identify an object
* @return void
*/
public function remove($identifier) {
if (empty($identifier)) {
throw new Exception('No valid identifier given to remove an object');
}
unset($this->objects[$identifier]);
}
/**
* Remove all stored objects
*
* @return void
*/
public function removeAll() {
unset($this->objects);
}
/**
* Returns the schema of a class
*
* @param string $className Name of the class
* @return Tx_Extbase_Reflection_ClassSchema Class schema
*/
protected function getClassSchema($className) {
if (empty($className)) {
throw new Exception('No valid class name given to create a class schema');
}
if (empty($this->classSchemata[$className])) {
$this->classSchemata[$className] = $this->reflectionService->getClassSchema($className);
}
return $this->classSchemata[$className];
}
/**
* Convert value into correct type
*
* @param mixed $value Value to convert
* @param string $type Type of the conversation
* @return mixed Converted value
*/
protected function convertValue($value, $type) {
switch ($type) {
case 'int':
case 'integer':
return (int) $value;
break;
case 'float':
return (float) $value;
break;
case 'bool':
case 'boolean':
return (boolean) $value;
break;
case 'array':
return (array) $value;
break;
case 'string':
default:
return (string) $value;
break;
}
}
}
?>
\ No newline at end of file
......@@ -26,7 +26,7 @@
/**
* Abstract extension provider
*/
abstract class Tx_TerFe2_ExtensionProvider_AbstractProvider implements Tx_TerFe2_ExtensionProvider_ProviderInterface {
abstract class Tx_TerFe2_Provider_AbstractProvider implements Tx_TerFe2_Provider_ProviderInterface {
/**
* @var Tx_Extbase_Object_ObjectManagerInterface
......
......@@ -26,7 +26,7 @@
/**
* Extension provider for local extension manager
*/
class Tx_TerFe2_ExtensionProvider_ExtensionManagerProvider extends Tx_TerFe2_ExtensionProvider_AbstractProvider {
class Tx_TerFe2_Provider_ExtensionManagerProvider extends Tx_TerFe2_Provider_AbstractProvider {
/**
* @var integer
......
......@@ -26,7 +26,7 @@
/**
* Extension provider using local files
*/
class Tx_TerFe2_ExtensionProvider_FileProvider extends Tx_TerFe2_ExtensionProvider_AbstractProvider {
class Tx_TerFe2_Provider_FileProvider extends Tx_TerFe2_Provider_AbstractProvider {
/**
* @var string
......
......@@ -26,7 +26,7 @@
/**
* Interface for extension providers
*/
interface Tx_TerFe2_ExtensionProvider_ProviderInterface extends t3lib_Singleton {
interface Tx_TerFe2_Provider_ProviderInterface extends t3lib_Singleton {
/*
* @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
......
......@@ -26,7 +26,7 @@
/**
* Manager for extension providers
*/
class Tx_TerFe2_ExtensionProvider_ProviderManager implements t3lib_Singleton {
class Tx_TerFe2_Provider_ProviderManager implements t3lib_Singleton {
/**
* @var Tx_Extbase_Object_ObjectManagerInterface
......@@ -52,7 +52,7 @@
* Get an instance of a concrete extension provider
*
* @param string $name Name of the provider
* @return Tx_TerFe2_ExtensionProvider_ExtensionProviderInterface Extension provider
* @return Tx_TerFe2_Provider_ProviderInterface Extension provider
*/
public function getProvider($name) {
$name = strtolower(trim($name));
......@@ -71,8 +71,8 @@
}
$provider = $this->objectManager->get($configuration['class']);
if (!($provider instanceof Tx_TerFe2_ExtensionProvider_ProviderInterface)) {
throw new Exception('Provider "' . $name . '" does not implement the interface "Tx_TerFe2_ExtensionProvider_ProviderInterface"');
if (!($provider instanceof Tx_TerFe2_Provider_ProviderInterface)) {
throw new Exception('Provider "' . $name . '" does not implement the interface "Tx_TerFe2_Provider_ProviderInterface"');
}
if (!empty($configuration['configuration']) && method_exists($provider, 'setConfiguration')) {
$provider->setConfiguration($configuration['configuration']);
......
......@@ -26,7 +26,7 @@
/**
* Extension provider for soap requests
*/
class Tx_TerFe2_ExtensionProvider_SoapProvider extends Tx_TerFe2_ExtensionProvider_AbstractProvider {
class Tx_TerFe2_Provider_SoapProvider extends Tx_TerFe2_Provider_AbstractProvider {
/**
* @var string
......
......@@ -49,7 +49,7 @@
protected $objectManager;
/**
* @var Tx_TerFe2_ExtensionProvider_ProviderManager
* @var Tx_TerFe2_Provider_ProviderManager
*/
protected $providerManager;
......@@ -58,6 +58,26 @@
*/
protected $registry;
/**
* @var Tx_TerFe2_Object_ObjectBuilder
*/
protected $objectBuilder;