This repository has been archived by the owner on Jan 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Repository.php
924 lines (826 loc) · 27.8 KB
/
Repository.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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
<?php
/**
* Repository class
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/
namespace eZ\Publish\Core\Repository;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\API\Repository\Values\ValueObject;
use eZ\Publish\API\Repository\Values\User\User;
use eZ\Publish\API\Repository\Values\User\Limitation;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
use eZ\Publish\SPI\Limitation\Type as LimitationType;
use Exception;
use RuntimeException;
/**
* Repository class
* @package eZ\Publish\Core\Repository
*/
class Repository implements RepositoryInterface
{
/**
* Repository Handler object
*
* @var \eZ\Publish\SPI\Persistence\Handler
*/
protected $persistenceHandler;
/**
* Currently logged in user object for permission purposes
*
* @var \eZ\Publish\API\Repository\Values\User\User
*/
protected $currentUser;
/**
* Counter for the current sudo nesting level {@see sudo()}.
*
* @var int
*/
private $sudoNestingLevel = 0;
/**
* Instance of content service
*
* @var \eZ\Publish\API\Repository\ContentService
*/
protected $contentService;
/**
* Instance of section service
*
* @var \eZ\Publish\API\Repository\SectionService
*/
protected $sectionService;
/**
* Instance of role service
*
* @var \eZ\Publish\API\Repository\RoleService
*/
protected $roleService;
/**
* Instance of search service
*
* @var \eZ\Publish\API\Repository\SearchService
*/
protected $searchService;
/**
* Instance of user service
*
* @var \eZ\Publish\API\Repository\UserService
*/
protected $userService;
/**
* Instance of language service
*
* @var \eZ\Publish\API\Repository\LanguageService
*/
protected $languageService;
/**
* Instance of location service
*
* @var \eZ\Publish\API\Repository\LocationService
*/
protected $locationService;
/**
* Instance of Trash service
*
* @var \eZ\Publish\API\Repository\TrashService
*/
protected $trashService;
/**
* Instance of content type service
*
* @var \eZ\Publish\API\Repository\ContentTypeService
*/
protected $contentTypeService;
/**
* Instance of object state service
*
* @var \eZ\Publish\API\Repository\ObjectStateService
*/
protected $objectStateService;
/**
* Instance of field type service
*
* @var \eZ\Publish\API\Repository\FieldTypeService
*/
protected $fieldTypeService;
/**
* Instance of name schema resolver service
*
* @var \eZ\Publish\Core\Repository\NameSchemaService
*/
protected $nameSchemaService;
/**
* Instance of relation processor service
*
* @var \eZ\Publish\Core\Repository\RelationProcessor
*/
protected $relationProcessor;
/**
* Instance of URL alias service
*
* @var \eZ\Publish\Core\Repository\URLAliasService
*/
protected $urlAliasService;
/**
* Instance of URL wildcard service
*
* @var \eZ\Publish\Core\Repository\URLWildcardService
*/
protected $urlWildcardService;
/**
* Service settings, first level key is service name
*
* @var array
*/
protected $serviceSettings;
/**
* Instance of domain mapper
*
* @var \eZ\Publish\Core\Repository\DomainMapper
*/
protected $domainMapper;
/**
* Instance of permissions criterion handler
*
* @var \eZ\Publish\Core\Repository\PermissionsCriterionHandler
*/
protected $permissionsCriterionHandler;
/**
* Array of arrays of commit events indexed by the transaction count.
*
* @var array
*/
protected $commitEventsQueue = array();
/**
* @var int
*/
protected $transactionDepth = 0;
/**
* @var int
*/
private $transactionCount = 0;
/**
* Constructor
*
* Construct repository object with provided storage engine
*
* @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
* @param array $serviceSettings
* @param \eZ\Publish\API\Repository\Values\User\User|null $user
*/
public function __construct( PersistenceHandler $persistenceHandler, array $serviceSettings = array(), User $user = null )
{
$this->persistenceHandler = $persistenceHandler;
$this->serviceSettings = $serviceSettings + array(
'content' => array(),
'contentType' => array(),
'location' => array(),
'section' => array(),
'role' => array(),
'user' => array(
'anonymousUserID' => 10
),
'language' => array(),
'trash' => array(),
'io' => array(),
'objectState' => array(),
'search' => array(),
'fieldType' => array(),
'urlAlias' => array(),
'urlWildcard' => array(),
'nameSchema' => array(),
'languages' => array()
);
if ( !empty( $this->serviceSettings['languages'] ) )
{
$this->serviceSettings['language']['languages'] = $this->serviceSettings['languages'];
}
if ( $user !== null )
$this->setCurrentUser( $user );
}
/**
* Get current user
*
* @return \eZ\Publish\API\Repository\Values\User\User
*/
public function getCurrentUser()
{
if ( !$this->currentUser instanceof User )
{
$this->currentUser = $this->getUserService()->loadUser(
$this->serviceSettings["user"]["anonymousUserID"]
);
}
return $this->currentUser;
}
/**
* Sets the current user to the given $user.
*
* @param \eZ\Publish\API\Repository\Values\User\User $user
*
* @return void
*/
public function setCurrentUser( User $user )
{
if ( !$user->id )
throw new InvalidArgumentValue( '$user->id', $user->id );
$this->currentUser = $user;
}
/**
* Allows API execution to be performed with full access sand-boxed
*
* The closure sandbox will do a catch all on exceptions and rethrow after
* re-setting the sudo flag.
*
* Example use:
* $location = $repository->sudo(
* function ( $repo ) use ( $locationId )
* {
* return $repo->getLocationService()->loadLocation( $locationId )
* }
* );
*
* @access private This function is not official API atm, and can change anytime.
*
* @param \Closure $callback
* @param \eZ\Publish\API\Repository\Repository $outerRepository
*
* @throws \RuntimeException Thrown on recursive sudo() use.
* @throws \Exception Re throws exceptions thrown inside $callback
* @return mixed
*/
public function sudo( \Closure $callback, RepositoryInterface $outerRepository = null )
{
$this->sudoNestingLevel++;
try
{
$returnValue = $callback( $outerRepository !== null ? $outerRepository : $this );
}
catch ( Exception $e )
{
$this->sudoNestingLevel--;
throw $e;
}
$this->sudoNestingLevel--;
return $returnValue;
}
/**
* Check if user has access to a given module / function
*
* Low level function, use canUser instead if you have objects to check against.
*
* @param string $module
* @param string $function
* @param \eZ\Publish\API\Repository\Values\User\User $user
*
* @return boolean|array Bool if user has full or no access, array if limitations if not
*/
public function hasAccess( $module, $function, User $user = null )
{
// Full access if sudo nesting level is set by {@see sudo()}
if ( $this->sudoNestingLevel > 0 )
return true;
if ( $user === null )
$user = $this->getCurrentUser();
// Uses SPI to avoid triggering permission checks in Role/User service
$permissionSets = array();
$roleService = $this->getRoleService();
$spiRoleAssignments = $this->persistenceHandler->userHandler()->loadRoleAssignmentsByGroupId( $user->id, true );
foreach ( $spiRoleAssignments as $spiRoleAssignment )
{
$permissionSet = array( 'limitation' => null, 'policies' => array() );
$spiRole = $this->persistenceHandler->userHandler()->loadRole( $spiRoleAssignment->roleId );
foreach ( $spiRole->policies as $spiPolicy )
{
if ( $spiPolicy->module === '*' && $spiRoleAssignment->limitationIdentifier === null )
return true;
if ( $spiPolicy->module !== $module && $spiPolicy->module !== '*' )
continue;
if ( $spiPolicy->function === '*' && $spiRoleAssignment->limitationIdentifier === null )
return true;
if ( $spiPolicy->function !== $function && $spiPolicy->function !== '*' )
continue;
if ( $spiPolicy->limitations === '*' && $spiRoleAssignment->limitationIdentifier === null )
return true;
$permissionSet['policies'][] = $roleService->buildDomainPolicyObject( $spiPolicy );
}
if ( !empty( $permissionSet['policies'] ) )
{
if ( $spiRoleAssignment->limitationIdentifier !== null )
$permissionSet['limitation'] = $roleService
->getLimitationType( $spiRoleAssignment->limitationIdentifier )
->buildValue( $spiRoleAssignment->values );
$permissionSets[] = $permissionSet;
}
}
if ( !empty( $permissionSets ) )
return $permissionSets;
return false;// No policies matching $module and $function, or they contained limitations
}
/**
* Check if user has access to a given action on a given value object
*
* Indicates if the current user is allowed to perform an action given by the function on the given
* objects.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If any of the arguments are invalid
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If value of the LimitationValue is unsupported
*
* @param string $module The module, aka controller identifier to check permissions on
* @param string $function The function, aka the controller action to check permissions on
* @param \eZ\Publish\API\Repository\Values\ValueObject $object The object to check if the user has access to
* @param mixed $targets The location, parent or "assignment" value object, or an array of the same
*
* @return boolean
*/
public function canUser( $module, $function, ValueObject $object, $targets = null )
{
$permissionSets = $this->hasAccess( $module, $function );
if ( $permissionSets === false || $permissionSets === true )
{
return $permissionSets;
}
if ( $targets instanceof ValueObject )
{
$targets = array( $targets );
}
else if ( $targets !== null && !is_array( $targets ) )
{
throw new InvalidArgumentType(
"\$targets",
"null|\\eZ\\Publish\\API\\Repository\\Values\\ValueObject|\\eZ\\Publish\\API\\Repository\\Values\\ValueObject[]",
$targets
);
}
$roleService = $this->getRoleService();
$currentUser = $this->getCurrentUser();
foreach ( $permissionSets as $permissionSet )
{
/**
* First deal with Role limitation if any
*
* Here we accept ACCESS_GRANTED and ACCESS_ABSTAIN, the latter in cases where $object and $targets
* are not supported by limitation.
*
* @var \eZ\Publish\API\Repository\Values\User\Limitation[] $permissionSet
*/
if ( $permissionSet['limitation'] instanceof Limitation )
{
$type = $roleService->getLimitationType( $permissionSet['limitation']->getIdentifier() );
$accessVote = $type->evaluate( $permissionSet['limitation'], $currentUser, $object, $targets );
if ( $accessVote === LimitationType::ACCESS_DENIED )
continue;
}
/**
* Loop over all policies
*
* These are already filtered by hasAccess and given hasAccess did not return boolean
* there must be some, so only return true if one of them says yes.
*
* @var \eZ\Publish\API\Repository\Values\User\Policy $policy
*/
foreach ( $permissionSet['policies'] as $policy )
{
$limitations = $policy->getLimitations();
/**
* Return true if policy gives full access (aka no limitations)
*/
if ( $limitations === '*' )
return true;
/**
* Loop over limitations, all must return ACCESS_GRANTED for policy to pass.
* If limitations was empty array this means same as '*'
*/
$limitationsPass = true;
foreach ( $limitations as $limitation )
{
$type = $roleService->getLimitationType( $limitation->getIdentifier() );
$accessVote = $type->evaluate( $limitation, $currentUser, $object, $targets );
/**
* For policy limitation atm only support ACCESS_GRANTED
*
* Reasoning: Right now, use of a policy limitation not valid for a policy is per definition a
* BadState. To reach this you would have to configure the "limitationMap" wrongly, like using
* Node (Location) limitation on state/assign. So in this case Role Limitations will return
* ACCESS_ABSTAIN (== no access here), and other limitations will throw InvalidArgument above,
* both cases forcing dev to investigate to find miss configuration. This might be relaxed in
* the future if valid use cases for ACCESS_ABSTAIN on policy limitations becomes known.
*/
if ( $accessVote !== LimitationType::ACCESS_GRANTED )
{
$limitationsPass = false;
break;// Break to next policy, all limitations must pass
}
}
if ( $limitationsPass )
return true;
}
}
return false;// None of the limitation sets wanted to let you in, sorry!
}
/**
* Get Content Service
*
* Get service object to perform operations on Content objects and it's aggregate members.
*
* @return \eZ\Publish\API\Repository\ContentService
*/
public function getContentService()
{
if ( $this->contentService !== null )
return $this->contentService;
$this->contentService = new ContentService(
$this,
$this->persistenceHandler,
$this->getDomainMapper(),
$this->getRelationProcessor(),
$this->getNameSchemaService(),
$this->serviceSettings['content']
);
return $this->contentService;
}
/**
* Get Content Language Service
*
* Get service object to perform operations on Content language objects
*
* @return \eZ\Publish\API\Repository\LanguageService
*/
public function getContentLanguageService()
{
if ( $this->languageService !== null )
return $this->languageService;
$this->languageService = new LanguageService(
$this,
$this->persistenceHandler->contentLanguageHandler(),
$this->serviceSettings['language']
);
return $this->languageService;
}
/**
* Get Content Type Service
*
* Get service object to perform operations on Content Type objects and it's aggregate members.
* ( Group, Field & FieldCategory )
*
* @return \eZ\Publish\API\Repository\ContentTypeService
*/
public function getContentTypeService()
{
if ( $this->contentTypeService !== null )
return $this->contentTypeService;
$this->contentTypeService = new ContentTypeService(
$this,
$this->persistenceHandler->contentTypeHandler(),
$this->getDomainMapper(),
$this->serviceSettings['contentType']
);
return $this->contentTypeService;
}
/**
* Get Content Location Service
*
* Get service object to perform operations on Location objects and subtrees
*
* @return \eZ\Publish\API\Repository\LocationService
*/
public function getLocationService()
{
if ( $this->locationService !== null )
return $this->locationService;
$this->locationService = new LocationService(
$this,
$this->persistenceHandler,
$this->getDomainMapper(),
$this->getNameSchemaService(),
$this->getPermissionsCriterionHandler(),
$this->serviceSettings['location']
);
return $this->locationService;
}
/**
* Get Content Trash service
*
* Trash service allows to perform operations related to location trash
* (trash/untrash, load/list from trash...)
*
* @return \eZ\Publish\API\Repository\TrashService
*/
public function getTrashService()
{
if ( $this->trashService !== null )
return $this->trashService;
$this->trashService = new TrashService(
$this,
$this->persistenceHandler,
$this->getNameSchemaService(),
$this->serviceSettings['trash']
);
return $this->trashService;
}
/**
* Get Content Section Service
*
* Get Section service that lets you manipulate section objects
*
* @return \eZ\Publish\API\Repository\SectionService
*/
public function getSectionService()
{
if ( $this->sectionService !== null )
return $this->sectionService;
$this->sectionService = new SectionService(
$this,
$this->persistenceHandler->sectionHandler(),
$this->serviceSettings['section']
);
return $this->sectionService;
}
/**
* Get User Service
*
* Get service object to perform operations on Users and UserGroup
*
* @return \eZ\Publish\API\Repository\UserService
*/
public function getUserService()
{
if ( $this->userService !== null )
return $this->userService;
$this->userService = new UserService(
$this,
$this->persistenceHandler->userHandler(),
$this->serviceSettings['user']
);
return $this->userService;
}
/**
* Get URLAliasService
*
* @return \eZ\Publish\API\Repository\URLAliasService
*/
public function getURLAliasService()
{
if ( $this->urlAliasService !== null )
return $this->urlAliasService;
$this->urlAliasService = new URLAliasService(
$this,
$this->persistenceHandler->urlAliasHandler(),
$this->serviceSettings['urlAlias']
);
return $this->urlAliasService;
}
/**
* Get URLWildcardService
*
* @return \eZ\Publish\API\Repository\URLWildcardService
*/
public function getURLWildcardService()
{
if ( $this->urlWildcardService !== null )
return $this->urlWildcardService;
$this->urlWildcardService = new URLWildcardService(
$this,
$this->persistenceHandler->urlWildcardHandler(),
$this->serviceSettings['urlWildcard']
);
return $this->urlWildcardService;
}
/**
* Get ObjectStateService
*
* @return \eZ\Publish\API\Repository\ObjectStateService
*/
public function getObjectStateService()
{
if ( $this->objectStateService !== null )
return $this->objectStateService;
$this->objectStateService = new ObjectStateService(
$this, $this->persistenceHandler->objectStateHandler(),
$this->serviceSettings['objectState']
);
return $this->objectStateService;
}
/**
* Get RoleService
*
* @return \eZ\Publish\API\Repository\RoleService
*/
public function getRoleService()
{
if ( $this->roleService !== null )
return $this->roleService;
$this->roleService = new RoleService(
$this,
$this->persistenceHandler->userHandler(),
$this->serviceSettings['role']
);
return $this->roleService;
}
/**
* Get SearchService
*
* @return \eZ\Publish\API\Repository\SearchService
*/
public function getSearchService()
{
if ( $this->searchService !== null )
return $this->searchService;
$this->searchService = new SearchService(
$this,
$this->persistenceHandler->searchHandler(),
$this->persistenceHandler->locationSearchHandler(),
$this->getDomainMapper(),
$this->getPermissionsCriterionHandler(),
$this->serviceSettings['search']
);
return $this->searchService;
}
/**
* Get FieldTypeService
*
* @return \eZ\Publish\API\Repository\FieldTypeService
*/
public function getFieldTypeService()
{
if ( $this->fieldTypeService !== null )
return $this->fieldTypeService;
$this->fieldTypeService = new FieldTypeService( $this->serviceSettings['fieldType'] );
return $this->fieldTypeService;
}
/**
* Get NameSchemaResolverService
*
* @access private Internal service for the Core Services
*
* @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
*
* @return \eZ\Publish\Core\Repository\NameSchemaService
*/
protected function getNameSchemaService()
{
if ( $this->nameSchemaService !== null )
return $this->nameSchemaService;
$this->nameSchemaService = new NameSchemaService( $this, $this->serviceSettings['nameSchema'] );
return $this->nameSchemaService;
}
/**
* Get RelationProcessor
*
* @access private Internal service for the Core Services
*
* @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
*
* @return \eZ\Publish\Core\Repository\RelationProcessor
*/
protected function getRelationProcessor()
{
if ( $this->relationProcessor !== null )
return $this->relationProcessor;
$this->relationProcessor = new RelationProcessor( $this, $this->persistenceHandler );
return $this->relationProcessor;
}
/**
* Get RelationProcessor
*
* @access private Internal service for the Core Services
*
* @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
*
* @return \eZ\Publish\Core\Repository\DomainMapper
*/
protected function getDomainMapper()
{
if ( $this->domainMapper !== null )
return $this->domainMapper;
$this->domainMapper = new DomainMapper(
$this,
$this->persistenceHandler->contentTypeHandler(),
$this->persistenceHandler->contentLanguageHandler()
);
return $this->domainMapper;
}
/**
* Get PermissionsCriterionHandler
*
* @access private Internal service for the Core Services
*
* @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
*
* @return \eZ\Publish\Core\Repository\PermissionsCriterionHandler
*/
protected function getPermissionsCriterionHandler()
{
return $this->permissionsCriterionHandler !== null ?
$this->permissionsCriterionHandler :
$this->permissionsCriterionHandler = new PermissionsCriterionHandler( $this );
}
/**
* Begin transaction
*
* Begins an transaction, make sure you'll call commit or rollback when done,
* otherwise work will be lost.
*/
public function beginTransaction()
{
$this->persistenceHandler->beginTransaction();
++$this->transactionDepth;
$this->commitEventsQueue[++$this->transactionCount] = array();
}
/**
* Commit transaction
*
* Commit transaction, or throw exceptions if no transactions has been started.
*
* @throws RuntimeException If no transaction has been started
*/
public function commit()
{
try
{
$this->persistenceHandler->commit();
--$this->transactionDepth;
if ( $this->transactionDepth === 0 )
{
$queueCountDown = count( $this->commitEventsQueue );
foreach ( $this->commitEventsQueue as $eventsQueue )
{
--$queueCountDown;
if ( empty( $eventsQueue ) )
continue;
$eventCountDown = count( $eventsQueue );
foreach ( $eventsQueue as $event )
{
--$eventCountDown;
// event expects a boolean param, if true it means it is last event (for commit use)
$event( $queueCountDown === 0 && $eventCountDown === 0 );
}
}
$this->commitEventsQueue = array();
}
}
catch ( Exception $e )
{
throw new RuntimeException( $e->getMessage(), 0, $e );
}
}
/**
* Rollback transaction
*
* Rollback transaction, or throw exceptions if no transactions has been started.
*
* @throws RuntimeException If no transaction has been started
*/
public function rollback()
{
try
{
$this->persistenceHandler->rollback();
--$this->transactionDepth;
unset( $this->commitEventsQueue[$this->transactionCount] );
}
catch ( Exception $e )
{
throw new RuntimeException( $e->getMessage(), 0, $e );
}
}
/**
* Enqueue an event to be triggered at commit or directly if no transaction has started
*
* @param Callable $event
*/
public function commitEvent( $event )
{
if ( $this->transactionDepth !== 0 )
{
$this->commitEventsQueue[$this->transactionCount][] = $event;
}
else
{
// event expects a boolean param, if true it means it is last event (for commit use)
$event( true );
}
}
/**
* Only for internal use.
*
* Creates a \DateTime object for $timestamp in the current time zone
*
* @param int $timestamp
*
* @return \DateTime
*/
public function createDateTime( $timestamp = null )
{
$dateTime = new \DateTime();
if ( $timestamp !== null )
{
$dateTime->setTimestamp( $timestamp );
}
return $dateTime;
}
}