| 1 | <?php |
| 2 | |
| 3 | namespace wcf\data; |
| 4 | |
| 5 | use wcf\system\exception\ClassNotFoundException; |
| 6 | use wcf\system\exception\ImplementationException; |
| 7 | use wcf\system\exception\SystemException; |
| 8 | use wcf\system\SingletonFactory; |
| 9 | |
| 10 | /** |
| 11 | * Abstract class for all processible data holder classes. |
| 12 | * |
| 13 | * @author Marcel Werk |
| 14 | * @copyright 2001-2019 WoltLab GmbH |
| 15 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 16 | * |
| 17 | * @property-read string|null $className name of the PHP class whose object(s) act as processor |
| 18 | */ |
| 19 | class ProcessibleDatabaseObject extends DatabaseObject |
| 20 | { |
| 21 | /** |
| 22 | * name of the interface the processor of this database object should implement |
| 23 | * @var string |
| 24 | */ |
| 25 | protected static $processorInterface = ''; |
| 26 | |
| 27 | /** |
| 28 | * processor this database object |
| 29 | * @var object |
| 30 | */ |
| 31 | protected $processor; |
| 32 | |
| 33 | /** |
| 34 | * Returns the processor this database object. |
| 35 | * |
| 36 | * @return object |
| 37 | * @throws SystemException |
| 38 | */ |
| 39 | public function getProcessor() |
| 40 | { |
| 41 | if ($this->processor === null) { |
| 42 | if ($this->className) { |
| 43 | if (!\class_exists($this->className)) { |
| 44 | throw new ClassNotFoundException($this->className); |
| 45 | } |
| 46 | if (!\is_subclass_of($this->className, static::$processorInterface)) { |
| 47 | throw new ImplementationException($this->className, static::$processorInterface); |
| 48 | } |
| 49 | |
| 50 | if (\is_subclass_of($this->className, SingletonFactory::class)) { |
| 51 | $this->processor = \call_user_func([$this->className, 'getInstance']); |
| 52 | } else { |
| 53 | if (!\is_subclass_of($this->className, IDatabaseObjectProcessor::class)) { |
| 54 | throw new ImplementationException($this->className, IDatabaseObjectProcessor::class); |
| 55 | } |
| 56 | |
| 57 | $this->processor = new $this->className($this); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return $this->processor; |
| 63 | } |
| 64 | } |