forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackend.php
725 lines (667 loc) · 25.6 KB
/
Backend.php
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
<?php
/**
* File containing the Backend for in-memory storage engine
*
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/
namespace eZ\Publish\Core\Persistence\InMemory;
use eZ\Publish\Core\Persistence;
use eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
use eZ\Publish\Core\Base\Exceptions\NotFoundException as NotFound;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
use eZ\Publish\SPI\Persistence\Content\FieldTypeConstraints;
use eZ\Publish\SPI\Persistence\ValueObject;
use LogicException;
use RuntimeException;
use Exception;
/**
* The Storage Engine backend for in memory storage
* Reads input from js files in provided directory and fills in memory db store.
*
* The in memory db store and also json representation have a one to one mapping to defined value objects.
* But only their plain properties, associations are not handled and all data is stored in separate "buckets" similar
* to how it would be in a RDBMS servers.
*/
class Backend
{
/**
* @var array
*/
protected $data = array();
/**
* For use to revert back to inital state
* @var array
*/
protected $initialData = array();
/**
* Stack of data for transactions
*
* Current data is always kept in $data, this is for rollbacks and transaction count.
*
* @var array
*/
protected $transactionDataStack = array();
/**
* Construct backend and assign data
*
* Use:
* new Backend( json_decode( file_get_contents( __DIR__ . '/data.json' ), true ) );
*
* @param array $data Data where key is type like "Content" or "Content\\Type" which then have to map to
* Value objects in eZ\Publish\SPI\Persistence\*, data is an array of hash values with same structure as
* the corresponding value object.
* Foreign keys: In some cases value objects does not contain these as they are internal, so this
* needs to be handled in InMemory handlers by assigning keys like "_typeId" on
* Type\FieldDefintion hash values for instance. These will be stored and can be
* matched with find(), but will not be returned as part of VO so purely internal.
*/
public function __construct( array $data )
{
$this->data = $data + $this->data;
$this->initialData = $this->data;
}
/**
* Reset data structure
*/
public function resetData()
{
$this->data = $this->initialData;
}
/**
* Creates data in in memory store
*
* @param string $type
* @param array $data
* @param boolean $autoIncrement
* @param string $idColumn By default, id column is 'id', but this can be customized here (e.g. for 'contentId')
*
* @return object
* @throws InvalidArgumentValue On invalid $type
* @throws LogicException If $autoIncrement is false but $data does not include an id
* @throws LogicException If provided id already exists (and if defined, data contain same status property value)
*/
public function create( $type, array $data, $autoIncrement = true, $idColumn = 'id' )
{
if ( !is_scalar( $type ) || !isset( $this->data[$type] ) )
throw new InvalidArgumentValue( 'type', $type );
if ( $autoIncrement )
{
$data[$idColumn] = $this->getNextId( $type, $idColumn );
}
else if ( !$data[$idColumn] )
{
throw new LogicException( '\'create\' logic error, $autoIncrement is false but no id is provided' );
}
foreach ( $this->data[$type] as $item )
{
if (
// Same identifier
$item[$idColumn] == $data[$idColumn] &&
(
// and "status" matches
( isset( $item['status'] ) && $item['status'] == $data['status'] ) ||
// or "_status" matches
( isset( $item['_status'] ) && $item['_status'] == $data['_status'] ) ||
// or no status available
!( isset( $item['status'] ) || isset( $item['_status'] ) )
)
)
throw new LogicException( "'create' logic error, provided id already exist" );
}
/*foreach ( $data as $prop => $value )
{
if ( $value === null )
throw new InvalidArgumentValue( 'data', "'$prop' on '$type' was of value NULL" );
}*/
$this->data[$type][] = $data;
return $this->toValue( $type, $data );
}
/**
* Reads data from in memory store
*
* @param string $type
* @param int|string $id
* @param string $idColumn
*
* @return object
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue On invalid $type
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If data does not exist
* @throws LogicException If several items exists with same id
*/
public function load( $type, $id, $idColumn = 'id' )
{
if ( !is_scalar( $type ) || !isset( $this->data[$type] ) )
throw new InvalidArgumentValue( 'type', $type );
$return = null;
$found = false;
foreach ( $this->data[$type] as $item )
{
if ( $item[$idColumn] != $id )
continue;
if ( $return )
throw new LogicException( "Logic error, more than one item exist with id: {$id}" );
$return = $this->toValue( $type, $item );
$found = true;
}
if ( !$found )
throw new NotFound( $type, $id );
return $return;
}
/**
* Find data from in memory store for a specific type that matches $match (empty array will match all)
*
* Note does not support joins, so only properties on $type is matched.
*
* @param string $type
* @param array $match A multi level array with property => value to match against
* @param array $joinInfo Optional info on how to join in other objects to become part of a
* aggregate where $type is root.
* Format:
* array( '<property>' => array(
* 'type' => '<foreign-type>',
* 'match' => array( '<foreign-key-property>' => '<key-property>' ) ),
* ['sub' => <$joinInfo>]
* )
* Example (joining Location when finding Content):
* array( 'locations' => array(
* 'type' => 'Content\\Location',
* 'match' => array( 'contentId' => 'id' ) )
* )
* Value of 'sub' follows exactly same format as $joinInfo allowing recursive joining.
* @uses rawFind()
*
* @return object[]
*/
public function find( $type, array $match = array(), array $joinInfo = array() )
{
$items = $this->rawFind( $type, $match, $joinInfo );
foreach ( $items as $key => $item )
$items[$key] = $this->toValue( $type, $item, $joinInfo );
return $items;
}
/**
* Updates data in in memory store
*
* @param string $type
* @param int|string $id
* @param array $data
* @param boolean $union Specifies if data should be merged with existing data or not
*
* @uses updateByMatch()
*
* @return boolean False if data does not exist and can not be updated
*/
public function update( $type, $id, array $data, $union = true, $idColumn = 'id' )
{
return $this->updateByMatch( $type, array( $idColumn => $id ), $data, $union, $idColumn );
}
/**
* Updates data in in memory store by match
*
* Useful in cases where a specific state of an object should be updated,
* Type with version=0 for instance.
*
* @param string $type
* @param array $match A flat array with property => value to match against
* @param array $data
* @param boolean $union Specifies if data should be merged with existing data or not
*
* @throws InvalidArgumentValue On invalid $type
*
* @return boolean False if data does not exist and can not be updated
*/
public function updateByMatch( $type, array $match, array $data, $union = true, $idColumn = 'id' )
{
if ( !is_scalar( $type ) || !isset( $this->data[$type] ) )
throw new InvalidArgumentValue( 'type', $type );
// Make sure id isn't changed
unset( $data[$idColumn] );
/*foreach ( $data as $prop => $value )
{
if ( $value === null )
throw new InvalidArgumentValue( 'data', "'$prop' on '$type' was of value NULL" );
}*/
$return = false;
foreach ( $this->data[$type] as $key => $item )
{
if ( $this->match( $item, $match ) )
{
if ( $union )
$this->data[$type][$key] = $data + $this->data[$type][$key];
else
$this->data[$type][$key] = $data;
$return = true;
}
}
return $return;
}
/**
* Deletes data in in memory store
*
* @param string $type
* @param int|string $id
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If data does not exist
* @uses deleteByMatch()
*/
public function delete( $type, $id, $idColumn = 'id' )
{
$this->deleteByMatch( $type, array( $idColumn => $id ) );
}
/**
* Deletes data in in memory store
*
* Useful in cases where a specific state of an object should be updated,
* Type with version=0 for instance.
*
* @param string $type
* @param array $match A flat array with property => value to match against
*
* @throws InvalidArgumentValue On invalid $type
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If no data to delete have been found
*/
public function deleteByMatch( $type, array $match )
{
if ( !is_scalar( $type ) || !isset( $this->data[$type] ) )
throw new InvalidArgumentValue( 'type', $type );
$found = false;
foreach ( $this->data[$type] as $key => $item )
{
if ( $this->match( $item, $match ) )
{
unset( $this->data[$type][$key] );
$found = true;
}
}
if ( !$found )
throw new NotFound( $type, $match );
}
/**
* Find count of objects of a given type matching a simple $match (empty array will match all)
*
* Note does not support joins, so only properties on $type is matched.
*
* @param string $type
* @param array $match A flat array with property => value to match against
* @param array $joinInfo See {@link find()}
*
* @uses rawFind()
*
* @return int
*/
public function count( $type, array $match = array(), array $joinInfo = array() )
{
return count( $this->rawFind( $type, $match, $joinInfo ) );
}
/**
* Begin transaction
*
* Begins an transaction, make sure you'll call commit or rollback when done,
* otherwise work will be lost.
*/
public function beginTransaction()
{
// copy current data to transaction stack
$this->transactionDataStack[] = $this->data;
}
/**
* Commit transaction
*
* Commit transaction, or throw exceptions if no transactions has been started.
*
* @throws \RuntimeException If no transaction has been started
*/
public function commit()
{
if ( empty( $this->transactionDataStack ) )
throw new RuntimeException( "No transactions in progress" );
// remove one level of data from transaction stack
array_pop( $this->transactionDataStack );
}
/**
* Rollback transaction
*
* Rollback transaction, or throw exceptions if no transactions has been started.
*
* @throws \RuntimeException If no transaction has been started
*/
public function rollback()
{
if ( empty( $this->transactionDataStack ) )
throw new RuntimeException( "No transactions in progress" );
// pop last data set from transaction stack back to live $data set to wipe out changes
$this->data = array_pop( $this->transactionDataStack );
}
/**
* Find data from in memory store for a specific type that matches $match (empty array will match all)
*
* Note does not support joins, so only properties on $type is matched.
*
* @param string $type
* @param array $match A multi level array with property => value to match against
* @param array $joinInfo See {@link find()}
*
* @return array[]
* @throws InvalidArgumentValue On invalid $type
* @throws LogicException When there is a collision between match rules in $joinInfo and $match
*/
protected function rawFind( $type, array $match = array(), array $joinInfo = array() )
{
if ( !is_scalar( $type ) || !isset( $this->data[$type] ) )
throw new InvalidArgumentValue( 'type', $type );
$items = array();
foreach ( $this->data[$type] as $item )
{
foreach ( $joinInfo as $joinProperty => $joinItem )
{
foreach ( $joinItem['match'] as $joinMatchKey => $joinMatchProperty )
{
$joinItem['match'][$joinMatchKey] = $item[$joinMatchProperty];
if ( isset( $match[$joinProperty][$joinMatchKey] ) )
throw new LogicException( "\$match[$joinProperty][$joinMatchKey] logic error, collision with match in \$joinInfo" );
}
$item[$joinProperty] = $this->rawFind(
$joinItem['type'],
$joinItem['match'],
( isset( $joinItem['sub'] ) ? $joinItem['sub'] : array() )
);
}
if ( $this->match( $item, $match ) )
$items[] = $item;
}
return $items;
}
/**
* Checks if a $item (a raw VO item) matches $match recursively
*
* @param array $item
* @param array $match
*
* @return boolean
*/
private function match( array $item, array $match )
{
foreach ( $match as $matchProperty => $matchValue )
{
if ( $matchProperty === 'or' )
{
// ignore empty and invalid 'or' expression
if ( empty( $matchValue ) || !is_array( $matchValue ) )
{
continue;
}
// valid 'or' expression, continue on first match
else
{
foreach ( $matchValue as $subMatchValue )
{
if ( $this->match( $item, $subMatchValue ) )
continue 2;
}
return false;
}
}
else if ( !isset( $item[$matchProperty] ) )
{
return false;
}
else if ( is_array( $item[$matchProperty] ) )
{
// sub match. When $matchValue is array, assume it's a joined
// list of value objects and look if one of them matches
if ( is_array( $matchValue ) )
{
foreach ( $item[$matchProperty] as $subItem )
{
if ( $this->match( $subItem, $matchValue ) )
continue 2;
}
return false;
}
// otherwise check if match value is part of array
else if ( !in_array( $matchValue, $item[$matchProperty] ) )
{
return false;
}
}
// A property trying to match a list of values
// Like an SQL IN() statement
else if ( is_array( $matchValue ) )
{
if ( !in_array( $item[$matchProperty], $matchValue ) )
return false;
}
// Use of wildcards like in SQL, at the start and/or end of $matchValue
// i.e. %/5/% (for pathString)
else if ( ( $ends = substr( $matchValue, -1 ) === "%" ) || substr( $matchValue, 0, 1 ) === "%" )
{
$starts = substr( $matchValue, 0, 1 ) === "%";
if ( $starts ) $matchValue = substr( $matchValue, 1 );
if ( $ends ) $matchValue = substr( $matchValue, 0, -1 );
if ( $matchValue === $item[$matchProperty] )
return false;
$pos = strpos( $item[$matchProperty], $matchValue );
if ( $pos === false )
return false;
if ( !$starts && $pos !== 0 )
return false;
if ( !$ends && $pos !== ( strlen( $item[$matchProperty] ) - strlen( $matchValue ) ) )
return false;
}
// plain equal match
else if ( $item[$matchProperty] != $matchValue )
{
return false;
}
}
return true;
}
/**
* Finds the max id number and that +1
*
* Makes sure no id conflicts occur if data for some reason contains gaps in id numbers.
*
* @param string $type
*
* @return int
*/
private function getNextId( $type, $idColumn = 'id' )
{
$id = 0;
foreach ( $this->data[$type] as $item )
{
$id = max( $id, $item[$idColumn] );
}
return $id + 1;
}
/**
* @return string
*/
static protected function getInstallationDir()
{
static $installDir = null;
if ( $installDir === null )
{
$config = require 'config.php';
$installDir = $config['service']['parameters']['install_dir'];
}
return $installDir;
}
/**
* @var \eZ\Publish\Core\Persistence\TransformationProcessor
*/
protected $transformationProcessor;
/**
* @return \eZ\Publish\Core\Persistence\TransformationProcessor
*/
public function getTransformationProcessor()
{
if ( !isset( $this->transformationProcessor ) )
{
$rules = array();
foreach ( glob( __DIR__ . '/../Tests/TransformationProcessor/_fixtures/transformations/*.tr' ) as $file )
{
$rules[] = str_replace( self::getInstallationDir(), '', $file );
}
$this->transformationProcessor = new DefinitionBased(
new Persistence\TransformationProcessor\DefinitionBased\Parser( self::getInstallationDir() ),
new Persistence\TransformationProcessor\PcreCompiler( new Persistence\Utf8Converter() ),
$rules
);
}
return $this->transformationProcessor;
}
/**
* Creates Value object based on array value from Backend.
*
* @param string $type
* @param array $data
* @param array $joinInfo See {@link find()}
*
* @return object
*/
protected function toValue( $type, array $data, array $joinInfo = array() )
{
$className = "eZ\\Publish\\SPI\\Persistence\\$type";
$obj = new $className;
foreach ( $obj as $prop => &$value )
{
if ( isset( $data[$prop] ) )
{
if ( $type === "Content\\Field" && $prop === "value" && ! $data["value"] instanceof FieldValue )
{
$fieldTypeNS = $this->getFieldTypeNamespace( $obj );
$fieldValueClassName = "$fieldTypeNS\\Value";
$fieldTypeValue = new $fieldValueClassName;
foreach ( $data["value"] as $fieldValuePropertyName => $fieldValuePropertyValue )
{
$fieldTypeValue->$fieldValuePropertyName = $fieldValuePropertyValue;
}
$fieldTypeClassName = "$fieldTypeNS\\Type";
/** @var $fieldType \eZ\Publish\Core\FieldType\FieldType */
$fieldType = new $fieldTypeClassName();
$fieldType->setTransformationProcessor( $this->getTransformationProcessor() );
$value = $fieldType->toPersistenceValue( $fieldTypeValue );
}
else if ( $type === "Content\\Type\\FieldDefinition" && $prop === "fieldTypeConstraints" && !$data["fieldTypeConstraints"] instanceof FieldTypeConstraints )
{
$value = new FieldTypeConstraints;
foreach ( $data["fieldTypeConstraints"] as $constraintName => $constraintValue )
{
$value->$constraintName = $constraintValue;
}
}
else if ( $type === "Content\\Type\\FieldDefinition" && $prop === "defaultValue" && !$data["defaultValue"] instanceof FieldValue )
{
$value = new FieldValue;
foreach ( $data["defaultValue"] as $propertyName => $propertyValue )
{
$value->$propertyName = $propertyValue;
}
}
else if ( $type === "Content\\UrlAlias" && $prop === "id" )
{
// id should be <parent>-<hash>, but as there is no property for link in VO and we need it in handler,
// returning everything here
// Note: before returning in handler id must be overwritten with <parent>-<hash>
$value = array(
"id" => $data["id"],
"parent" => $data["parent"],
"link" => $data["link"],
);
}
else
{
$value = $data[$prop];
}
}
}
try
{
return $this->joinToValue( $obj, $joinInfo );
}
catch ( Exception $e )
{
throw new RuntimeException( "Could not join value using: " . var_export( $data, true ), 0, $e );
}
}
/**
* Creates value objects on join properties
*
* @param \eZ\Publish\SPI\Persistence\ValueObject $item
* @param array $joinInfo See {@link find()}
*
* @return ValueObject
*/
private function joinToValue( ValueObject $item, array $joinInfo = array() )
{
foreach ( $joinInfo as $property => $info )
{
if ( isset( $info['skip'] ) && $info['skip'] )
continue;
if ( isset( $info['single'] ) && $info['single'] )
{
$value =& $item->$property;
if ( !empty( $value ) )
{
$value = $this->toValue(
$info['type'],
$value[0],
( isset( $info['sub'] ) ? $info['sub'] : array() )
);
}
else
{
throw new RuntimeException( "$property is supposed to be single(1), found none!" );
}
continue;
}
foreach ( $item->$property as &$joinItem )
{
$joinItem = $this->toValue(
$info['type'],
$joinItem,
( isset( $info['sub'] ) ? $info['sub'] : array() )
);
}
}
return $item;
}
/**
* @param \eZ\Publish\SPI\Persistence\ValueObject $obj
*
* @return string
*/
protected function getFieldTypeNamespace( $obj )
{
if ( isset( $this->tempFieldTypeMapping[ $obj->type ] ) )
return $this->tempFieldTypeMapping[ $obj->type ];
throw new \Exception( "Following FieldType is not supported by InMemory storage {$obj->type}" );
}
/**
* @var array
*/
private $tempFieldTypeMapping = array(
'ezstring' => 'eZ\\Publish\\Core\\FieldType\\TextLine',
'ezinteger' => 'eZ\\Publish\\Core\\FieldType\\Integer',
'ezauthor' => 'eZ\\Publish\\Core\\FieldType\\Author',
'ezfloat' => 'eZ\\Publish\\Core\\FieldType\\Float',
'eztext' => 'eZ\\Publish\\Core\\FieldType\\TextBlock',
'ezboolean' => 'eZ\\Publish\\Core\\FieldType\\Checkbox',
'ezdatetime' => 'eZ\\Publish\\Core\\FieldType\\DateAndTime',
'ezkeyword' => 'eZ\\Publish\\Core\\FieldType\\Keyword',
'ezurl' => 'eZ\\Publish\\Core\\FieldType\\Url',
'ezcountry' => 'eZ\\Publish\\Core\\FieldType\\Country',
'ezbinaryfile' => 'eZ\\Publish\\Core\\FieldType\\BinaryFile',
'ezmedia' => 'eZ\\Publish\\Core\\FieldType\\Media',
'ezxmltext' => 'eZ\\Publish\\Core\\FieldType\\XmlText',
'ezobjectrelationlist' => 'eZ\\Publish\\Core\\FieldType\\RelationList',
'ezselection' => 'eZ\\Publish\\Core\\FieldType\\Selection',
'ezsrrating' => 'eZ\\Publish\\Core\\FieldType\\Rating',
'ezimage' => 'eZ\\Publish\\Core\\FieldType\\Image',
'ezobjectrelation' => 'eZ\\Publish\\Core\\FieldType\\Relation',
);
}