Created
March 29, 2021 19:46
-
-
Save a-r-m-i-n/ab780623e7f7b262ac2f92e24ced9524 to your computer and use it in GitHub Desktop.
CreateInstanceTrait for TYPO3 CMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Core\Utility\StringUtility; | |
use TYPO3\CMS\Extbase\Object\ObjectManager; | |
trait CreateInstanceTrait | |
{ | |
/** @return parent */ | |
public static function createInstance() | |
{ | |
if (StringUtility::endsWith(static::class, 'Repository')) { | |
$objectManager = GeneralUtility::makeInstance(ObjectManager::class); | |
return $objectManager->get(static::class); | |
} | |
return GeneralUtility::makeInstance(static::class); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyAwesomeRepository extends Repository | |
{ | |
use CreateInstanceTrait; | |
} | |
$repo = MyAwesomeRepository::createInstance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment