Remove all uses of databox_field::get_instance

This commit is contained in:
Benoît Burnichon
2015-07-08 01:20:45 +02:00
parent 82f19ee55d
commit 8ee5279d58
12 changed files with 72 additions and 101 deletions

View File

@@ -34,7 +34,7 @@ class FieldsController extends Controller
foreach ($data as $jsonField) { foreach ($data as $jsonField) {
try { try {
$field = \databox_field::get_instance($this->app, $databox, $jsonField['id']); $field = $metaStructure->get_element($jsonField['id']);
if ($field->get_name() !== $jsonField['name']) { if ($field->get_name() !== $jsonField['name']) {
$this->validateNameField($metaStructure, $jsonField); $this->validateNameField($metaStructure, $jsonField);
@@ -191,7 +191,7 @@ class FieldsController extends Controller
public function getField($sbas_id, $id) public function getField($sbas_id, $id)
{ {
$databox = $this->findDataboxById((int) $sbas_id); $databox = $this->findDataboxById((int) $sbas_id);
$field = \databox_field::get_instance($this->app, $databox, $id); $field = $databox->get_meta_structure()->get_element($id);
return $this->app->json($field->toArray()); return $this->app->json($field->toArray());
} }
@@ -199,7 +199,7 @@ class FieldsController extends Controller
public function updateField(Request $request, $sbas_id, $id) public function updateField(Request $request, $sbas_id, $id)
{ {
$databox = $this->findDataboxById((int) $sbas_id); $databox = $this->findDataboxById((int) $sbas_id);
$field = \databox_field::get_instance($this->app, $databox, $id); $field = $databox->get_meta_structure()->get_element($id);
$data = $this->getFieldJsonFromRequest($request); $data = $this->getFieldJsonFromRequest($request);
$this->validateTagField($data); $this->validateTagField($data);
@@ -218,7 +218,7 @@ class FieldsController extends Controller
public function deleteField($sbas_id, $id) public function deleteField($sbas_id, $id)
{ {
$databox = $this->findDataboxById((int) $sbas_id); $databox = $this->findDataboxById((int) $sbas_id);
\databox_field::get_instance($this->app, $databox, $id)->delete(); $databox->get_meta_structure()->get_element($id)->delete();
return new Response('', 204); return new Response('', 204);
} }

View File

@@ -126,6 +126,6 @@ class TooltipController extends Controller
{ {
$databox = $this->findDataboxById((int)$sbas_id); $databox = $this->findDataboxById((int)$sbas_id);
return \databox_field::get_instance($this->app, $databox, $field_id); return $databox->get_meta_structure()->get_element($field_id);
} }
} }

View File

@@ -473,10 +473,10 @@ class SearchEngineOptions
break; break;
case in_array($key, ['date_fields', 'fields']): case in_array($key, ['date_fields', 'fields']):
$value = array_map(function ($serialized) use ($app) { $value = array_map(function ($serialized) use ($app) {
$data = explode('_', $serialized); $data = explode('_', $serialized);
return \databox_field::get_instance($app, $app->findDataboxById($data[0]), $data[1]); return $app->findDataboxById($data[0])->get_meta_structure()->get_element($data[1]);
}, $value); }, $value);
break; break;
case in_array($key, ['collections', 'business_fields']): case in_array($key, ['collections', 'business_fields']):
$value = array_map(function ($base_id) use ($app) { $value = array_map(function ($base_id) use ($app) {

View File

@@ -87,11 +87,12 @@ class caption_record implements caption_interface, cache_cacheableInterface
$rec_fields = array(); $rec_fields = array();
if ($fields) { if ($fields) {
$databox_descriptionStructure = $this->databox->get_meta_structure();
foreach ($fields as $row) { foreach ($fields as $row) {
$databox_meta_struct = databox_field::get_instance($this->app, $this->databox, $row['structure_id']); $databox_field = $databox_descriptionStructure->get_element($row['structure_id']);
$metadata = new caption_field($this->app, $databox_meta_struct, $this->record); $metadata = new caption_field($this->app, $databox_field, $this->record);
$rec_fields[$databox_meta_struct->get_id()] = $metadata; $rec_fields[$databox_field->get_id()] = $metadata;
} }
} }
$this->fields = $rec_fields; $this->fields = $rec_fields;

View File

@@ -10,6 +10,7 @@
*/ */
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Databox\DataboxFieldRepositoryInterface;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Exception\InvalidArgumentException; use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Status\StatusStructure; use Alchemy\Phrasea\Status\StatusStructure;
@@ -620,7 +621,6 @@ class databox extends base
} }
/** /**
*
* @return databox_descriptionStructure|databox_field[] * @return databox_descriptionStructure|databox_field[]
*/ */
public function get_meta_structure() public function get_meta_structure()
@@ -629,31 +629,10 @@ class databox extends base
return $this->meta_struct; return $this->meta_struct;
} }
try { /** @var DataboxFieldRepositoryInterface $fieldRepository */
$metaStructData = $this->get_data_from_cache(self::CACHE_META_STRUCT); $fieldRepository = $this->app['repo.fields.factory']($this);
if (!is_array($metaStructData)) {
throw new Exception('Invalid metaStructData');
}
} catch (\Exception $e) {
$metaStructData = array();
$sql = 'SELECT id, `name` FROM metadatas_structure ORDER BY sorter ASC'; $this->meta_struct = new databox_descriptionStructure($fieldRepository->findAll());
$stmt = $this->get_connection()->prepare($sql);
$stmt->execute();
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
if ($rs) {
$metaStructData = $rs;
$this->set_data_to_cache($metaStructData, self::CACHE_META_STRUCT);
}
}
$this->meta_struct = new databox_descriptionStructure();
foreach ($metaStructData as $row) {
$this->meta_struct->add_element(databox_field::get_instance($this->app, $this, $row['id']));
}
return $this->meta_struct; return $this->meta_struct;
} }

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Assert\Assertion;
class databox_descriptionStructure implements IteratorAggregate, Countable class databox_descriptionStructure implements IteratorAggregate, Countable
{ {
/** @var databox_field[] */ /** @var databox_field[] */
@@ -17,10 +19,22 @@ class databox_descriptionStructure implements IteratorAggregate, Countable
/** /**
* Cache array for the get element by name function * Cache array for the get element by name function
* *
* @var databox_field[] * @var array
*/ */
protected $cache_name_id = []; protected $cache_name_id = [];
/**
* @param databox_field[] $fields
*/
public function __construct($fields = [])
{
Assertion::allIsInstanceOf($fields, databox_field::class);
foreach ($fields as $field) {
$this->add_element($field);
}
}
/** /**
* @return Iterator * @return Iterator
*/ */

View File

@@ -194,35 +194,6 @@ class databox_field implements cache_cacheableInterface
return $this->aggregable != 0; return $this->aggregable != 0;
} }
/**
* @param Application $app
* @param \databox $databox
* @param int $id
*
* @return \databox_field
*/
public static function get_instance(Application $app, databox $databox, $id)
{
$cache_key = 'field_' . $id;
$instance_id = $databox->get_sbas_id() . '_' . $id;
if (! isset(self::$_instance[$instance_id])) {
try {
$field = $databox->get_data_from_cache($cache_key);
if (!$field instanceof self) {
trigger_error('Cache type returned mismatch', E_WARNING);
throw new \Exception('Retrieved $field value is invalid');
}
} catch (\Exception $e) {
$field = new self($app, $databox, $id);
$databox->set_data_to_cache($field, $cache_key);
}
self::$_instance[$instance_id] = $field;
}
$field =& self::$_instance[$instance_id];
$field->app = $app;
return $field;
}
public function hydrate(Application $app) public function hydrate(Application $app)
{ {
$this->app = $app; $this->app = $app;
@@ -934,7 +905,7 @@ class databox_field implements cache_cacheableInterface
$databox->delete_data_from_cache(databox::CACHE_META_STRUCT); $databox->delete_data_from_cache(databox::CACHE_META_STRUCT);
return self::get_instance($app, $databox, $id); return $databox->get_meta_structure()->get_element($id);
} }
public static function generateName($name) public static function generateName($name)

View File

@@ -10,6 +10,7 @@
*/ */
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Assert\Assertion;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
class patch_360alpha2b extends patchAbstract class patch_360alpha2b extends patchAbstract
@@ -49,6 +50,8 @@ class patch_360alpha2b extends patchAbstract
*/ */
public function apply(base $databox, Application $app) public function apply(base $databox, Application $app)
{ {
Assertion::isInstanceOf($databox, databox::class);
/** @var databox $databox */
/** /**
* Fail if upgrade has previously failed, no problem * Fail if upgrade has previously failed, no problem
*/ */
@@ -115,16 +118,13 @@ class patch_360alpha2b extends patchAbstract
VALUES (null, :record_id, :meta_struct_id, :value)'; VALUES (null, :record_id, :meta_struct_id, :value)';
$stmt = $databox->get_connection()->prepare($sql); $stmt = $databox->get_connection()->prepare($sql);
$databox_fields = []; $databox_fields = $databox->get_meta_structure();
foreach ($rs as $row) { foreach ($rs as $row) {
$meta_struct_id = $row['meta_struct_id']; $meta_struct_id = $row['meta_struct_id'];
if ( ! isset($databox_fields[$meta_struct_id])) { $databox_field = $databox_fields->get_element($meta_struct_id);
$databox_fields[$meta_struct_id] = \databox_field::get_instance($app, $databox, $meta_struct_id); $values = \caption_field::get_multi_values($row['value'], $databox_field->get_separator());
}
$values = \caption_field::get_multi_values($row['value'], $databox_fields[$meta_struct_id]->get_separator());
foreach ($values as $value) { foreach ($values as $value) {
$params = [ $params = [

View File

@@ -1000,7 +1000,7 @@ class record_adapter implements RecordInterface, cache_cacheableInterface
throw new Exception('Metadata value should be scalar'); throw new Exception('Metadata value should be scalar');
} }
$databox_field = databox_field::get_instance($this->app, $databox, $params['meta_struct_id']); $databox_field = $databox->get_meta_structure()->get_element($params['meta_struct_id']);
$caption_field = new caption_field($this->app, $databox_field, $this); $caption_field = new caption_field($this->app, $databox_field, $this);
@@ -1047,14 +1047,16 @@ class record_adapter implements RecordInterface, cache_cacheableInterface
* *
* @return record_adapter * @return record_adapter
*/ */
public function set_metadatas(Array $metadatas, $force_readonly = false) public function set_metadatas(array $metadatas, $force_readonly = false)
{ {
$databox_descriptionStructure = $this->get_databox()->get_meta_structure();
foreach ($metadatas as $param) { foreach ($metadatas as $param) {
if (!is_array($param)) { if (!is_array($param)) {
throw new Exception_InvalidArgument('Invalid metadatas argument'); throw new Exception_InvalidArgument('Invalid metadatas argument');
} }
$db_field = \databox_field::get_instance($this->app, $this->get_databox(), $param['meta_struct_id']); $db_field = $databox_descriptionStructure->get_element($param['meta_struct_id']);
if ($db_field->is_readonly() === true && !$force_readonly) { if ($db_field->is_readonly() === true && !$force_readonly) {
continue; continue;

View File

@@ -29,7 +29,9 @@ class MetadataBagTest extends \PhraseanetTestCase
*/ */
public function testToMetadataArray() public function testToMetadataArray()
{ {
$structure = self::$DI['collection']->get_databox()->get_meta_structure(); /** @var \collection $collection */
$collection = self::$DI['collection'];
$structure = $collection->get_databox()->get_meta_structure();
$valueMono = new Mono('mono value'); $valueMono = new Mono('mono value');
$valueMulti = new Multi(['multi', 'value']); $valueMulti = new Multi(['multi', 'value']);

View File

@@ -4,6 +4,7 @@ namespace Alchemy\Tests\Phrasea\Controller\Admin;
use PHPExiftool\Driver\Tag\IPTC\ObjectName; use PHPExiftool\Driver\Tag\IPTC\ObjectName;
use Alchemy\Phrasea\Vocabulary\Controller as VocabularyController; use Alchemy\Phrasea\Vocabulary\Controller as VocabularyController;
use Symfony\Component\HttpKernel\Client;
/** /**
* @group functional * @group functional
@@ -217,7 +218,8 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
public function testCreateField() public function testCreateField()
{ {
$databoxes = self::$DI['app']->getDataboxes(); $databoxes = $this->getApplication()->getDataboxes();
/** @var \databox $databox */
$databox = array_shift($databoxes); $databox = array_shift($databoxes);
$body = json_encode([ $body = json_encode([
@@ -246,10 +248,12 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
'vocabulary-restricted' => true, 'vocabulary-restricted' => true,
]); ]);
self::$DI['client']->request("POST", sprintf("/admin/fields/%d/fields", $databox->get_sbas_id()), [], [], [], $body); /** @var Client $client */
$client = self::$DI['client'];
$client->request("POST", sprintf("/admin/fields/%d/fields", $databox->get_sbas_id()), [], [], [], $body);
$response = self::$DI['client']->getResponse()->getContent(); $response = $client->getResponse()->getContent();
$this->assertEquals("application/json", self::$DI['client']->getResponse()->headers->get("content-type")); $this->assertEquals("application/json", $client->getResponse()->headers->get("content-type"));
$data = json_decode($response, true); $data = json_decode($response, true);
@@ -261,7 +265,7 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
$this->assertEquals(json_decode($body, true), $dataWithoutIds); $this->assertEquals(json_decode($body, true), $dataWithoutIds);
$field = \databox_field::get_instance(self::$DI['app'], $databox, $data['id']); $field = $databox->get_meta_structure()->get_element($data['id']);
$field->delete(); $field->delete();
} }
@@ -325,10 +329,12 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
public function testDeleteField() public function testDeleteField()
{ {
$databoxes = self::$DI['app']->getDataboxes(); $app = $this->getApplication();
$databoxes = $app->getDataboxes();
/** @var \databox $databox */
$databox = array_shift($databoxes); $databox = array_shift($databoxes);
$field = \databox_field::create(self::$DI['app'], $databox, 'testfield' . mt_rand(), false); $field = \databox_field::create($app, $databox, 'testfield' . mt_rand(), false);
$fieldId = $field->get_id(); $fieldId = $field->get_id();
$data = $field->toArray(); $data = $field->toArray();
@@ -336,14 +342,16 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
$data['business'] = true; $data['business'] = true;
$data['vocabulary-type'] = 'User'; $data['vocabulary-type'] = 'User';
self::$DI['client']->request("DELETE", sprintf("/admin/fields/%d/fields/%d", $databox->get_sbas_id(), $field->get_id()), [], [], [], json_encode($data)); /** @var Client $client */
$client = self::$DI['client'];
$client->request("DELETE", sprintf("/admin/fields/%d/fields/%d", $databox->get_sbas_id(), $field->get_id()), [], [], [], json_encode($data));
$response = self::$DI['client']->getResponse()->getContent(); $response = $client->getResponse()->getContent();
$this->assertEquals('', $response); $this->assertEquals('', $response);
$this->assertEquals(204, self::$DI['client']->getResponse()->getStatusCode()); $this->assertEquals(204, $client->getResponse()->getStatusCode());
try { try {
\databox_field::get_instance(self::$DI['app'], $databox, $fieldId); $databox->get_meta_structure()->get_element($fieldId);
$this->fail('Should have raise an exception'); $this->fail('Should have raise an exception');
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@@ -8,11 +8,11 @@ use Alchemy\Phrasea\Application;
*/ */
class databox_fieldTest extends \PhraseanetTestCase class databox_fieldTest extends \PhraseanetTestCase
{ {
/** /** @var databox_field */
* @var databox_field
*/
protected $object_mono; protected $object_mono;
/** @var databox_field */
protected $object_multi; protected $object_multi;
/** @var databox */
protected $databox; protected $databox;
protected $name_mono; protected $name_mono;
protected $name_multi; protected $name_multi;
@@ -20,7 +20,10 @@ class databox_fieldTest extends \PhraseanetTestCase
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->databox = self::$DI['record_1']->get_databox();
/** @var record_adapter $record_1 */
$record_1 = self::$DI['record_1'];
$this->databox = $record_1->get_databox();
$this->name_mono = 'Field Test Mono'; $this->name_mono = 'Field Test Mono';
$this->name_multi = 'Field Test Multi'; $this->name_multi = 'Field Test Multi';
@@ -53,15 +56,6 @@ class databox_fieldTest extends \PhraseanetTestCase
parent::tearDown(); parent::tearDown();
} }
public function testGet_instance()
{
$instance = databox_field::get_instance(self::$DI['app'], $this->databox, $this->object_mono->get_id());
$this->assertEquals($this->object_mono->get_id(), $instance->get_id());
$instance = databox_field::get_instance(self::$DI['app'], $this->databox, $this->object_multi->get_id());
$this->assertEquals($this->object_multi->get_id(), $instance->get_id());
}
/** /**
* @todo Implement testSet_databox(). * @todo Implement testSet_databox().
*/ */