[TASK] Fix spelling and streamline @deprecated notations
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Reflection / ClassSchema.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Reflection;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Doctrine\Common\Annotations\AnnotationReader;
18 use TYPO3\CMS\Core\SingletonInterface;
19 use TYPO3\CMS\Core\Utility\ClassNamingUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\StringUtility;
22 use TYPO3\CMS\Extbase\Annotation\IgnoreValidation;
23 use TYPO3\CMS\Extbase\Annotation\Inject;
24 use TYPO3\CMS\Extbase\Annotation\ORM\Cascade;
25 use TYPO3\CMS\Extbase\Annotation\ORM\Lazy;
26 use TYPO3\CMS\Extbase\Annotation\ORM\Transient;
27 use TYPO3\CMS\Extbase\Annotation\Validate;
28 use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
29 use TYPO3\CMS\Extbase\DomainObject\AbstractValueObject;
30 use TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface;
31 use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility;
32 use TYPO3\CMS\Extbase\Validation\Exception\InvalidTypeHintException;
33 use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException;
34 use TYPO3\CMS\Extbase\Validation\ValidatorResolver;
35
36 /**
37 * A class schema
38 *
39 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
40 */
41 class ClassSchema
42 {
43 /**
44 * Available model types
45 */
46 const MODELTYPE_ENTITY = 1;
47 const MODELTYPE_VALUEOBJECT = 2;
48
49 /**
50 * Name of the class this schema is referring to
51 *
52 * @var string
53 */
54 protected $className;
55
56 /**
57 * Model type of the class this schema is referring to
58 *
59 * @var int
60 */
61 protected $modelType = self::MODELTYPE_ENTITY;
62
63 /**
64 * Whether a repository exists for the class this schema is referring to
65 *
66 * @var bool
67 */
68 protected $aggregateRoot = false;
69
70 /**
71 * The name of the property holding the uuid of an entity, if any.
72 *
73 * @var string
74 */
75 protected $uuidPropertyName;
76
77 /**
78 * Properties of the class which need to be persisted
79 *
80 * @var array
81 */
82 protected $properties = [];
83
84 /**
85 * The properties forming the identity of an object
86 *
87 * @var array
88 */
89 protected $identityProperties = [];
90
91 /**
92 * Indicates if the class is a singleton or not.
93 *
94 * @var bool
95 */
96 private $isSingleton;
97
98 /**
99 * @var bool
100 */
101 private $isController;
102
103 /**
104 * @var array
105 */
106 private $methods;
107
108 /**
109 * @var array
110 */
111 private $tags;
112
113 /**
114 * @var array
115 */
116 private $injectProperties = [];
117
118 /**
119 * @var array
120 */
121 private $injectMethods = [];
122
123 /**
124 * Constructs this class schema
125 *
126 * @param string $className Name of the class this schema is referring to
127 * @throws \TYPO3\CMS\Extbase\Reflection\Exception\UnknownClassException
128 * @throws \ReflectionException
129 */
130 public function __construct($className)
131 {
132 $this->className = $className;
133
134 $reflectionClass = new \ReflectionClass($className);
135
136 $this->isSingleton = $reflectionClass->implementsInterface(SingletonInterface::class);
137 $this->isController = $reflectionClass->implementsInterface(ControllerInterface::class);
138
139 if ($reflectionClass->isSubclassOf(AbstractEntity::class)) {
140 $this->modelType = static::MODELTYPE_ENTITY;
141
142 $possibleRepositoryClassName = ClassNamingUtility::translateModelNameToRepositoryName($className);
143 if (class_exists($possibleRepositoryClassName)) {
144 $this->setAggregateRoot(true);
145 }
146 }
147
148 if ($reflectionClass->isSubclassOf(AbstractValueObject::class)) {
149 $this->modelType = static::MODELTYPE_VALUEOBJECT;
150 }
151
152 $docCommentParser = new DocCommentParser(true);
153 $docCommentParser->parseDocComment($reflectionClass->getDocComment());
154 $this->tags = $docCommentParser->getTagsValues();
155
156 $this->reflectProperties($reflectionClass);
157 $this->reflectMethods($reflectionClass);
158 }
159
160 /**
161 * @param \ReflectionClass $reflectionClass
162 */
163 protected function reflectProperties(\ReflectionClass $reflectionClass)
164 {
165 $annotationReader = new AnnotationReader();
166
167 foreach ($reflectionClass->getProperties() as $reflectionProperty) {
168 $propertyName = $reflectionProperty->getName();
169
170 $this->properties[$propertyName] = [
171 'default' => $reflectionProperty->isDefault(),
172 'private' => $reflectionProperty->isPrivate(),
173 'protected' => $reflectionProperty->isProtected(),
174 'public' => $reflectionProperty->isPublic(),
175 'static' => $reflectionProperty->isStatic(),
176 'type' => null, // Extbase
177 'elementType' => null, // Extbase
178 'annotations' => [],
179 'tags' => [],
180 'validators' => []
181 ];
182
183 $docCommentParser = new DocCommentParser(true);
184 $docCommentParser->parseDocComment($reflectionProperty->getDocComment());
185 foreach ($docCommentParser->getTagsValues() as $tag => $values) {
186 $this->properties[$propertyName]['tags'][strtolower($tag)] = $values;
187 }
188
189 $this->properties[$propertyName]['annotations']['inject'] = false;
190 $this->properties[$propertyName]['annotations']['lazy'] = false;
191 $this->properties[$propertyName]['annotations']['transient'] = false;
192 $this->properties[$propertyName]['annotations']['type'] = null;
193 $this->properties[$propertyName]['annotations']['cascade'] = null;
194 $this->properties[$propertyName]['annotations']['dependency'] = null;
195
196 $annotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
197
198 /** @var array|Validate[] $validateAnnotations */
199 $validateAnnotations = array_filter($annotations, function ($annotation) {
200 return $annotation instanceof Validate;
201 });
202
203 if (count($validateAnnotations) > 0) {
204 $validatorResolver = GeneralUtility::makeInstance(ValidatorResolver::class);
205
206 foreach ($validateAnnotations as $validateAnnotation) {
207 $validatorObjectName = $validatorResolver->resolveValidatorObjectName($validateAnnotation->validator);
208
209 $this->properties[$propertyName]['validators'][] = [
210 'name' => $validateAnnotation->validator,
211 'options' => $validateAnnotation->options,
212 'className' => $validatorObjectName,
213 ];
214 }
215 }
216
217 if ($docCommentParser->isTaggedWith('validate')) {
218 trigger_error(
219 sprintf(
220 'Property %s::%s is tagged with @validate which is deprecated and will be removed in TYPO3 v10.0.',
221 $reflectionClass->getName(),
222 $reflectionProperty->getName()
223 ),
224 E_USER_DEPRECATED
225 );
226
227 $validatorResolver = GeneralUtility::makeInstance(ValidatorResolver::class);
228
229 $validateValues = $docCommentParser->getTagValues('validate');
230 foreach ($validateValues as $validateValue) {
231 $validatorConfiguration = $validatorResolver->parseValidatorAnnotation($validateValue);
232
233 foreach ($validatorConfiguration['validators'] ?? [] as $validator) {
234 $validatorObjectName = $validatorResolver->resolveValidatorObjectName($validator['validatorName']);
235
236 $this->properties[$propertyName]['validators'][] = [
237 'name' => $validator['validatorName'],
238 'options' => $validator['validatorOptions'],
239 'className' => $validatorObjectName,
240 ];
241 }
242 }
243 }
244
245 if ($annotationReader->getPropertyAnnotation($reflectionProperty, Lazy::class) instanceof Lazy) {
246 $this->properties[$propertyName]['annotations']['lazy'] = true;
247 }
248
249 if ($docCommentParser->isTaggedWith('lazy')) {
250 $this->properties[$propertyName]['annotations']['lazy'] = true;
251 trigger_error(
252 sprintf(
253 'Property %s::%s is tagged with @lazy which is deprecated and will be removed in TYPO3 v10.0.',
254 $reflectionClass->getName(),
255 $reflectionProperty->getName()
256 ),
257 E_USER_DEPRECATED
258 );
259 }
260
261 if ($annotationReader->getPropertyAnnotation($reflectionProperty, Transient::class) instanceof Transient) {
262 $this->properties[$propertyName]['annotations']['transient'] = true;
263 }
264
265 if ($docCommentParser->isTaggedWith('transient')) {
266 $this->properties[$propertyName]['annotations']['transient'] = true;
267 trigger_error(
268 sprintf(
269 'Property %s::%s is tagged with @transient which is deprecated and will be removed in TYPO3 v10.0.',
270 $reflectionClass->getName(),
271 $reflectionProperty->getName()
272 ),
273 E_USER_DEPRECATED
274 );
275 }
276
277 if ($propertyName !== 'settings'
278 && ($annotationReader->getPropertyAnnotation($reflectionProperty, Inject::class) instanceof Inject)
279 ) {
280 try {
281 $varValue = ltrim($docCommentParser->getTagValues('var')[0], '\\');
282 $this->properties[$propertyName]['annotations']['inject'] = true;
283 $this->properties[$propertyName]['annotations']['type'] = $varValue;
284 $this->properties[$propertyName]['annotations']['dependency'] = $varValue;
285
286 $this->injectProperties[] = $propertyName;
287 } catch (\Exception $e) {
288 }
289 }
290
291 if ($propertyName !== 'settings' && $docCommentParser->isTaggedWith('inject')) {
292 trigger_error(
293 sprintf(
294 'Property %s::%s is tagged with @inject which is deprecated and will be removed in TYPO3 v10.0.',
295 $reflectionClass->getName(),
296 $reflectionProperty->getName()
297 ),
298 E_USER_DEPRECATED
299 );
300 try {
301 $varValues = $docCommentParser->getTagValues('var');
302 $this->properties[$propertyName]['annotations']['inject'] = true;
303 $this->properties[$propertyName]['annotations']['type'] = ltrim($varValues[0], '\\');
304 $this->properties[$propertyName]['annotations']['dependency'] = ltrim($varValues[0], '\\');
305
306 if (!$reflectionProperty->isPublic()) {
307 trigger_error(
308 sprintf(
309 'Property %s::%s is not public and tagged with @inject which is deprecated and will stop working in TYPO3 v10.0.',
310 $reflectionClass->getName(),
311 $reflectionProperty->getName()
312 ),
313 E_USER_DEPRECATED
314 );
315 }
316
317 $this->injectProperties[] = $propertyName;
318 } catch (\Exception $e) {
319 }
320 }
321
322 if ($docCommentParser->isTaggedWith('var') && !$docCommentParser->isTaggedWith('transient')) {
323 try {
324 $cascadeAnnotationValues = $docCommentParser->getTagValues('cascade');
325 $this->properties[$propertyName]['annotations']['cascade'] = $cascadeAnnotationValues[0];
326 } catch (\Exception $e) {
327 }
328
329 if ($this->properties[$propertyName]['annotations']['cascade'] !== null) {
330 trigger_error(
331 sprintf(
332 'Property %s::%s is tagged with @cascade which is deprecated and will be removed in TYPO3 v10.0.',
333 $reflectionClass->getName(),
334 $reflectionProperty->getName()
335 ),
336 E_USER_DEPRECATED
337 );
338 }
339
340 if (($annotation = $annotationReader->getPropertyAnnotation($reflectionProperty, Cascade::class)) instanceof Cascade) {
341 /** @var Cascade $annotation */
342 $this->properties[$propertyName]['annotations']['cascade'] = $annotation->value;
343 }
344
345 try {
346 $type = TypeHandlingUtility::parseType(implode(' ', $docCommentParser->getTagValues('var')));
347 } catch (\Exception $e) {
348 $type = [
349 'type' => null,
350 'elementType' => null
351 ];
352 }
353
354 $this->properties[$propertyName]['type'] = $type['type'] ? ltrim($type['type'], '\\') : null;
355 $this->properties[$propertyName]['elementType'] = $type['elementType'] ? ltrim($type['elementType'], '\\') : null;
356 }
357
358 if ($docCommentParser->isTaggedWith('uuid')) {
359 $this->setUuidPropertyName($propertyName);
360 }
361
362 if ($docCommentParser->isTaggedWith('identity')) {
363 $this->markAsIdentityProperty($propertyName);
364 }
365 }
366 }
367
368 /**
369 * @param \ReflectionClass $reflectionClass
370 */
371 protected function reflectMethods(\ReflectionClass $reflectionClass)
372 {
373 $annotationReader = new AnnotationReader();
374
375 foreach ($reflectionClass->getMethods() as $reflectionMethod) {
376 $methodName = $reflectionMethod->getName();
377
378 $this->methods[$methodName] = [];
379 $this->methods[$methodName]['private'] = $reflectionMethod->isPrivate();
380 $this->methods[$methodName]['protected'] = $reflectionMethod->isProtected();
381 $this->methods[$methodName]['public'] = $reflectionMethod->isPublic();
382 $this->methods[$methodName]['static'] = $reflectionMethod->isStatic();
383 $this->methods[$methodName]['abstract'] = $reflectionMethod->isAbstract();
384 $this->methods[$methodName]['params'] = [];
385 $this->methods[$methodName]['tags'] = [];
386 $this->methods[$methodName]['annotations'] = [];
387 $this->methods[$methodName]['isAction'] = StringUtility::endsWith($methodName, 'Action');
388
389 $docCommentParser = new DocCommentParser(true);
390 $docCommentParser->parseDocComment($reflectionMethod->getDocComment());
391
392 $argumentValidators = [];
393
394 $annotations = $annotationReader->getMethodAnnotations($reflectionMethod);
395
396 /** @var array|Validate[] $validateAnnotations */
397 $validateAnnotations = array_filter($annotations, function ($annotation) {
398 return $annotation instanceof Validate;
399 });
400
401 if ($this->isController && $this->methods[$methodName]['isAction'] && count($validateAnnotations) > 0) {
402 $validatorResolver = GeneralUtility::makeInstance(ValidatorResolver::class);
403
404 foreach ($validateAnnotations as $validateAnnotation) {
405 $validatorName = $validateAnnotation->validator;
406 $validatorObjectName = $validatorResolver->resolveValidatorObjectName($validatorName);
407
408 $argumentValidators[$validateAnnotation->param][] = [
409 'name' => $validatorName,
410 'options' => $validateAnnotation->options,
411 'className' => $validatorObjectName,
412 ];
413 }
414 }
415
416 foreach ($docCommentParser->getTagsValues() as $tag => $values) {
417 if ($tag === 'ignorevalidation') {
418 trigger_error(
419 sprintf(
420 'Method %s::%s is tagged with @ignorevalidation which is deprecated and will be removed in TYPO3 v10.0.',
421 $reflectionClass->getName(),
422 $reflectionMethod->getName()
423 ),
424 E_USER_DEPRECATED
425 );
426 }
427 if ($tag === 'validate' && $this->isController && $this->methods[$methodName]['isAction']) {
428 trigger_error(
429 sprintf(
430 'Method %s::%s is tagged with @validate which is deprecated and will be removed in TYPO3 v10.0.',
431 $reflectionClass->getName(),
432 $reflectionMethod->getName()
433 ),
434 E_USER_DEPRECATED
435 );
436
437 $validatorResolver = GeneralUtility::makeInstance(ValidatorResolver::class);
438
439 foreach ($values as $validate) {
440 $methodValidatorDefinition = $validatorResolver->parseValidatorAnnotation($validate);
441
442 foreach ($methodValidatorDefinition['validators'] as $validator) {
443 $validatorObjectName = $validatorResolver->resolveValidatorObjectName($validator['validatorName']);
444
445 $argumentValidators[$methodValidatorDefinition['argumentName']][] = [
446 'name' => $validator['validatorName'],
447 'options' => $validator['validatorOptions'],
448 'className' => $validatorObjectName,
449 ];
450 }
451 }
452 }
453 $this->methods[$methodName]['tags'][$tag] = array_map(function ($value) use ($tag) {
454 // not stripping the dollar sign for @validate annotations is just
455 // a quick fix for a regression introduced in 9.0.0.
456 // This exception to the rules will vanish once the resolving of
457 // validators will take place inside this class and not in the
458 // controller during runtime.
459 return $tag === 'validate' ? $value : ltrim($value, '$');
460 }, $values);
461 }
462 unset($methodValidatorDefinition);
463
464 foreach ($annotations as $annotation) {
465 if ($annotation instanceof IgnoreValidation) {
466 $this->methods[$methodName]['tags']['ignorevalidation'][] = $annotation->argumentName;
467 }
468 }
469
470 $this->methods[$methodName]['description'] = $docCommentParser->getDescription();
471
472 foreach ($reflectionMethod->getParameters() as $parameterPosition => $reflectionParameter) {
473 /* @var $reflectionParameter \ReflectionParameter */
474
475 $parameterName = $reflectionParameter->getName();
476
477 $this->methods[$methodName]['params'][$parameterName] = [];
478 $this->methods[$methodName]['params'][$parameterName]['position'] = $parameterPosition; // compat
479 $this->methods[$methodName]['params'][$parameterName]['byReference'] = $reflectionParameter->isPassedByReference(); // compat
480 $this->methods[$methodName]['params'][$parameterName]['array'] = $reflectionParameter->isArray(); // compat
481 $this->methods[$methodName]['params'][$parameterName]['optional'] = $reflectionParameter->isOptional();
482 $this->methods[$methodName]['params'][$parameterName]['allowsNull'] = $reflectionParameter->allowsNull(); // compat
483 $this->methods[$methodName]['params'][$parameterName]['class'] = null; // compat
484 $this->methods[$methodName]['params'][$parameterName]['type'] = null;
485 $this->methods[$methodName]['params'][$parameterName]['nullable'] = $reflectionParameter->allowsNull();
486 $this->methods[$methodName]['params'][$parameterName]['default'] = null;
487 $this->methods[$methodName]['params'][$parameterName]['hasDefaultValue'] = $reflectionParameter->isDefaultValueAvailable();
488 $this->methods[$methodName]['params'][$parameterName]['defaultValue'] = null; // compat
489 $this->methods[$methodName]['params'][$parameterName]['dependency'] = null; // Extbase DI
490 $this->methods[$methodName]['params'][$parameterName]['validators'] = [];
491
492 if ($reflectionParameter->isDefaultValueAvailable()) {
493 $this->methods[$methodName]['params'][$parameterName]['default'] = $reflectionParameter->getDefaultValue();
494 $this->methods[$methodName]['params'][$parameterName]['defaultValue'] = $reflectionParameter->getDefaultValue(); // compat
495 }
496
497 if (($reflectionType = $reflectionParameter->getType()) instanceof \ReflectionType) {
498 $this->methods[$methodName]['params'][$parameterName]['type'] = (string)$reflectionType;
499 $this->methods[$methodName]['params'][$parameterName]['nullable'] = $reflectionType->allowsNull();
500 }
501
502 if (($parameterClass = $reflectionParameter->getClass()) instanceof \ReflectionClass) {
503 $this->methods[$methodName]['params'][$parameterName]['class'] = $parameterClass->getName();
504 $this->methods[$methodName]['params'][$parameterName]['type'] = ltrim($parameterClass->getName(), '\\');
505 } else {
506 $methodTagsAndValues = $this->methods[$methodName]['tags'];
507 if (isset($methodTagsAndValues['param'][$parameterPosition])) {
508 $explodedParameters = explode(' ', $methodTagsAndValues['param'][$parameterPosition]);
509 if (count($explodedParameters) >= 2) {
510 if (TypeHandlingUtility::isSimpleType($explodedParameters[0])) {
511 // ensure that short names of simple types are resolved correctly to the long form
512 // this is important for all kinds of type checks later on
513 $typeInfo = TypeHandlingUtility::parseType($explodedParameters[0]);
514
515 $this->methods[$methodName]['params'][$parameterName]['type'] = ltrim($typeInfo['type'], '\\');
516 } else {
517 $this->methods[$methodName]['params'][$parameterName]['type'] = ltrim($explodedParameters[0], '\\');
518 }
519 }
520 }
521 }
522
523 // Extbase DI
524 if ($reflectionParameter->getClass() instanceof \ReflectionClass
525 && ($reflectionMethod->isConstructor() || $this->hasInjectMethodName($reflectionMethod))
526 ) {
527 $this->methods[$methodName]['params'][$parameterName]['dependency'] = $reflectionParameter->getClass()->getName();
528 }
529
530 // Extbase Validation
531 if (isset($argumentValidators[$parameterName])) {
532 if ($this->methods[$methodName]['params'][$parameterName]['type'] === null) {
533 throw new InvalidTypeHintException(
534 'Missing type information for parameter "$' . $parameterName . '" in ' . $this->className . '->' . $methodName . '(): Either use an @param annotation or use a type hint.',
535 1515075192
536 );
537 }
538
539 $this->methods[$methodName]['params'][$parameterName]['validators'] = $argumentValidators[$parameterName];
540 unset($argumentValidators[$parameterName]);
541 }
542 }
543
544 // Extbase Validation
545 foreach ($argumentValidators as $parameterName => $validators) {
546 $validatorNames = array_column($validators, 'name');
547
548 throw new InvalidValidationConfigurationException(
549 'Invalid validate annotation in ' . $this->className . '->' . $methodName . '(): The following validators have been defined for missing param "$' . $parameterName . '": ' . implode(', ', $validatorNames),
550 1515073585
551 );
552 }
553
554 // Extbase
555 $this->methods[$methodName]['injectMethod'] = false;
556 if ($this->hasInjectMethodName($reflectionMethod)
557 && count($this->methods[$methodName]['params']) === 1
558 && reset($this->methods[$methodName]['params'])['dependency'] !== null
559 ) {
560 $this->methods[$methodName]['injectMethod'] = true;
561 $this->injectMethods[] = $methodName;
562 }
563 }
564 }
565
566 /**
567 * Returns the class name this schema is referring to
568 *
569 * @return string The class name
570 */
571 public function getClassName(): string
572 {
573 return $this->className;
574 }
575
576 /**
577 * Adds (defines) a specific property and its type.
578 *
579 * @param string $name Name of the property
580 * @param string $type Type of the property
581 * @param bool $lazy Whether the property should be lazy-loaded when reconstituting
582 * @param string $cascade Strategy to cascade the object graph.
583 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
584 */
585 public function addProperty($name, $type, $lazy = false, $cascade = '')
586 {
587 trigger_error(
588 'This method will be removed in TYPO3 v10.0, properties will be automatically added on ClassSchema construction.',
589 E_USER_DEPRECATED
590 );
591 $type = TypeHandlingUtility::parseType($type);
592 $this->properties[$name] = [
593 'type' => $type['type'],
594 'elementType' => $type['elementType'],
595 'lazy' => $lazy,
596 'cascade' => $cascade
597 ];
598 }
599
600 /**
601 * Returns the given property defined in this schema. Check with
602 * hasProperty($propertyName) before!
603 *
604 * @param string $propertyName
605 * @return array
606 */
607 public function getProperty($propertyName)
608 {
609 return isset($this->properties[$propertyName]) && is_array($this->properties[$propertyName])
610 ? $this->properties[$propertyName]
611 : [];
612 }
613
614 /**
615 * Returns all properties defined in this schema
616 *
617 * @return array
618 */
619 public function getProperties()
620 {
621 return $this->properties;
622 }
623
624 /**
625 * Sets the model type of the class this schema is referring to.
626 *
627 * @param int $modelType The model type, one of the MODELTYPE_* constants.
628 * @throws \InvalidArgumentException
629 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
630 */
631 public function setModelType($modelType)
632 {
633 trigger_error(
634 'This method will be removed in TYPO3 v10.0, modelType will be automatically set on ClassSchema construction.',
635 E_USER_DEPRECATED
636 );
637 if ($modelType < self::MODELTYPE_ENTITY || $modelType > self::MODELTYPE_VALUEOBJECT) {
638 throw new \InvalidArgumentException('"' . $modelType . '" is an invalid model type.', 1212519195);
639 }
640 $this->modelType = $modelType;
641 }
642
643 /**
644 * Returns the model type of the class this schema is referring to.
645 *
646 * @return int The model type, one of the MODELTYPE_* constants.
647 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
648 */
649 public function getModelType()
650 {
651 trigger_error(
652 'This method will be removed in TYPO3 v10.0.',
653 E_USER_DEPRECATED
654 );
655 return $this->modelType;
656 }
657
658 /**
659 * Marks the class if it is root of an aggregate and therefore accessible
660 * through a repository - or not.
661 *
662 * @param bool $isRoot TRUE if it is the root of an aggregate
663 */
664 public function setAggregateRoot($isRoot)
665 {
666 $this->aggregateRoot = $isRoot;
667 }
668
669 /**
670 * Whether the class is an aggregate root and therefore accessible through
671 * a repository.
672 *
673 * @return bool TRUE if it is managed
674 */
675 public function isAggregateRoot(): bool
676 {
677 return $this->aggregateRoot;
678 }
679
680 /**
681 * If the class schema has a certain property.
682 *
683 * @param string $propertyName Name of the property
684 * @return bool
685 */
686 public function hasProperty($propertyName): bool
687 {
688 return array_key_exists($propertyName, $this->properties);
689 }
690
691 /**
692 * Sets the property marked as uuid of an object with @uuid
693 *
694 * @param string $propertyName
695 * @throws \InvalidArgumentException
696 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
697 */
698 public function setUuidPropertyName($propertyName)
699 {
700 trigger_error(
701 'Tagging properties with @uuid is deprecated and will be removed in TYPO3 v10.0.',
702 E_USER_DEPRECATED
703 );
704 if (!array_key_exists($propertyName, $this->properties)) {
705 throw new \InvalidArgumentException('Property "' . $propertyName . '" must be added to the class schema before it can be marked as UUID property.', 1233863842);
706 }
707 $this->uuidPropertyName = $propertyName;
708 }
709
710 /**
711 * Gets the name of the property marked as uuid of an object
712 *
713 * @return string
714 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
715 */
716 public function getUuidPropertyName()
717 {
718 trigger_error(
719 'Tagging properties with @uuid is deprecated and will be removed in TYPO3 v10.0.',
720 E_USER_DEPRECATED
721 );
722 return $this->uuidPropertyName;
723 }
724
725 /**
726 * Marks the given property as one of properties forming the identity
727 * of an object. The property must already be registered in the class
728 * schema.
729 *
730 * @param string $propertyName
731 * @throws \InvalidArgumentException
732 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
733 */
734 public function markAsIdentityProperty($propertyName)
735 {
736 trigger_error(
737 'Tagging properties with @identity is deprecated and will be removed in TYPO3 v10.0.',
738 E_USER_DEPRECATED
739 );
740 if (!array_key_exists($propertyName, $this->properties)) {
741 throw new \InvalidArgumentException('Property "' . $propertyName . '" must be added to the class schema before it can be marked as identity property.', 1233775407);
742 }
743 if ($this->properties[$propertyName]['annotations']['lazy'] === true) {
744 throw new \InvalidArgumentException('Property "' . $propertyName . '" must not be makred for lazy loading to be marked as identity property.', 1239896904);
745 }
746 $this->identityProperties[$propertyName] = $this->properties[$propertyName]['type'];
747 }
748
749 /**
750 * Gets the properties (names and types) forming the identity of an object.
751 *
752 * @return array
753 * @see markAsIdentityProperty()
754 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
755 */
756 public function getIdentityProperties()
757 {
758 trigger_error(
759 'Tagging properties with @identity is deprecated and will be removed in TYPO3 v10.0.',
760 E_USER_DEPRECATED
761 );
762 return $this->identityProperties;
763 }
764
765 /**
766 * @return bool
767 */
768 public function hasConstructor(): bool
769 {
770 return isset($this->methods['__construct']);
771 }
772
773 /**
774 * @param string $name
775 * @return array
776 */
777 public function getMethod(string $name): array
778 {
779 return $this->methods[$name] ?? [];
780 }
781
782 /**
783 * @return array
784 */
785 public function getMethods(): array
786 {
787 return $this->methods;
788 }
789
790 /**
791 * @param \ReflectionMethod $reflectionMethod
792 * @return bool
793 */
794 protected function hasInjectMethodName(\ReflectionMethod $reflectionMethod): bool
795 {
796 $methodName = $reflectionMethod->getName();
797 if ($methodName === 'injectSettings' || !$reflectionMethod->isPublic()) {
798 return false;
799 }
800
801 if (
802 strpos($reflectionMethod->getName(), 'inject') === 0
803 ) {
804 return true;
805 }
806
807 return false;
808 }
809
810 /**
811 * @return bool
812 * @internal
813 */
814 public function isModel(): bool
815 {
816 return $this->isEntity() || $this->isValueObject();
817 }
818
819 /**
820 * @return bool
821 * @internal
822 */
823 public function isEntity(): bool
824 {
825 return $this->modelType === static::MODELTYPE_ENTITY;
826 }
827
828 /**
829 * @return bool
830 * @internal
831 */
832 public function isValueObject(): bool
833 {
834 return $this->modelType === static::MODELTYPE_VALUEOBJECT;
835 }
836
837 /**
838 * @return bool
839 */
840 public function isSingleton(): bool
841 {
842 return $this->isSingleton;
843 }
844
845 /**
846 * @param string $methodName
847 * @return bool
848 */
849 public function hasMethod(string $methodName): bool
850 {
851 return isset($this->methods[$methodName]);
852 }
853
854 /**
855 * @return array
856 */
857 public function getTags(): array
858 {
859 return $this->tags;
860 }
861
862 /**
863 * @return bool
864 */
865 public function hasInjectProperties(): bool
866 {
867 return count($this->injectProperties) > 0;
868 }
869
870 /**
871 * @return bool
872 */
873 public function hasInjectMethods(): bool
874 {
875 return count($this->injectMethods) > 0;
876 }
877
878 /**
879 * @return array
880 */
881 public function getInjectMethods(): array
882 {
883 $injectMethods = [];
884 foreach ($this->injectMethods as $injectMethodName) {
885 $injectMethods[$injectMethodName] = reset($this->methods[$injectMethodName]['params'])['dependency'];
886 }
887
888 return $injectMethods;
889 }
890
891 /**
892 * @return array
893 */
894 public function getInjectProperties(): array
895 {
896 $injectProperties = [];
897 foreach ($this->injectProperties as $injectPropertyName) {
898 $injectProperties[$injectPropertyName] = $this->properties[$injectPropertyName]['annotations']['dependency'];
899 }
900
901 return $injectProperties;
902 }
903
904 /**
905 * @return array
906 */
907 public function getConstructorArguments(): array
908 {
909 if (!$this->hasConstructor()) {
910 return [];
911 }
912
913 return $this->methods['__construct']['params'];
914 }
915 }