Source of file ContextProperties.php

Size: 5,024 Bytes - Last Modified: 2023-11-16T22:56:02+01:00

/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/core-bundle/src/Traits/ContextProperties.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
<?php
namespace TIC\CoreBundle\Traits;

/**
 * Initialisation de propriétés d'après le contexte de la classe courante.
 * 
 * Utile pour déterminer automatiquement les noms des éléments associés :
 *   - racine commune du namespace (bundle ou app)
 *   - classe de l'objet manipulé (Entity)
 *   - classe du formulaire à utiliser (FormType)
 *   - sous-dossier contenant les templates des vues
 *   - préfixe pour le nommage des routes du controller
 *   - préfixe pour le nommage des tokens de traduction
 */
trait ContextProperties
{
	protected $ctxClass;            /** nom complet de la classe courante    (ex: 'App\Controller\Admin\FooBarController') */
	protected $ctxType;             /** type de la classe courante           (ex: 'Controller', 'Command') */
	protected $ctxRoot;             /** espace de nommage de base du bundle  (ex: 'App', 'TIC\DemoBundle') */
	protected $ctxName;             /** nom du controlleur en camel case     (ex: 'Default', 'User', 'Book', 'FooBar', ...) */
	protected $ctxPath;             /** nom du sous-dossier du controlleur   (ex: '', 'Admin', 'Front', ...) */

	protected $ctxBundle;           /** alias du bundle pour repo, views...  (ex: null, '@TICDemo') */
	protected $ctxSnake;            /** nom du controlleur en snake case     (ex: 'default', 'book', 'foo_bar') */
	protected $ctxRoute;            /** préfixe utilisé pour les routes      (ex: 'app_foobar_', 'admin_foobar_', 'ticdemo_foobar_') */
	protected $ctxTrans;            /** préfixe utilisé pour les tokens      (ex: 'app.foobar.', 'admin.foobar.', 'ticdemo.foobar.') */
	protected $ctxMesg = 'item';    /** nom dans les tokens génériques       (ex: 'item', 'user', 'book", 'foobar') */
	protected $ctxForm;             /** classname du FormType correspondant  (ex: 'App\Form\FooBarType', 'App\Form\Admin\BookType', 'TIC\DemoBundle\Form\ItemType') */

	/**
	 * DI avec auto-wiring (inutile de définir le "call" sur le service grace à l'attribut @required).
	 * @see https://symfony.com/doc/current/service_container/injection_types.html#setter-injection
	 * @see https://symfony.com/doc/current/service_container/autowiring.html#autowiring-other-methods-e-g-setters-and-public-typed-properties
	 *
	 * @required
	 */
	public function setContextProperties(?string $classType = null): void
	{
		if (null === $this->ctxClass) $this->ctxClass = static::class;
		
		// expression régulière pour l'extraction des composants du namespace
		if (null === $classType) $classType = 'Controller|Command|Form|Type|Repository|Model|Service';
		$re = '/^(.+)\\\\('.$classType.')\\\\(.+\\\\)?([^\\\\]+)('.$classType.')$/';
		
		// analyse des parties du nom complet de la classe courante
		if (\preg_match($re, $this->ctxClass, $match)) {
			if (null === $this->ctxType) $this->ctxType = $match[2];
			if (null === $this->ctxRoot) $this->ctxRoot = $match[1];
			if (null === $this->ctxName) $this->ctxName = $match[4];
			if (null === $this->ctxPath) $this->ctxPath = \trim($match[3], "\\");
		}
		// détermination de la référence de bundle
		if (null === $this->ctxBundle)
			$this->ctxBundle = \preg_match('/^(.+)Bundle$/', $this->ctxRoot, $match) ? '@'.\str_replace("\\",'',$match[1]) : null;
		// détermination du nom de base en snake case
		if (null === $this->ctxSnake)
			$this->ctxSnake  = \strtolower(\trim(\preg_replace("/([A-Z][^A-Z])/", '_\1', $this->ctxName), '_'));
		
		// détermination du préfixe des noms de routes
		if (null === $this->ctxRoute) {
			if ($this->ctxBundle) {
				$this->ctxRoute = \strtolower(\str_replace('@', '', $this->ctxBundle));
				if (! empty($this->ctxPath)) $this->ctxRoute.= '_' . \strtolower($this->ctxPath);
			} else {
				$this->ctxRoute = empty($this->ctxPath) ? 'app' : \strtolower($this->ctxPath);
			}
			$this->ctxRoute.= '_' . \strtolower($this->ctxName) . '_';
		}
		
		// détermination du préfixe pour les tokens de traduction
		if (null === $this->ctxTrans)
			$this->ctxTrans = \strtr($this->ctxRoute, '_', '.');
		// détermination du nom pour les tokens de traduction génériques
		if (null === $this->ctxMesg)
			$this->ctxMesg = \strtolower($this->ctxName);
			
		// détermination de la class du FormType correspondant
		if (null === $this->ctxForm) {
			$formClass = array( $this->ctxRoot, "Form" );
			if (! empty($this->ctxPath)) $formClass[] = $this->ctxPath;
			$formClass[] = $this->ctxName . "Type";
			$this->ctxForm = \implode("\\", $formClass);
		}
		
#		dump($this->getContextProperties());
	}

	/**
	 * Retourne la liste des propriétés définies dans un tableaux (utile pour du debug).
	 */
	protected function getContextProperties(): array
	{
		return array(
			"class"  => $this->ctxClass,
			"type"   => $this->ctxType,
			"root"   => $this->ctxRoot,
			"name"   => $this->ctxName,
			"path"   => $this->ctxPath,
			"bundle" => $this->ctxBundle,
			"snake"  => $this->ctxSnake,
			"route"  => $this->ctxRoute,
			"trans"  => $this->ctxTrans,
			"mesg"   => $this->ctxMesg,
			"form"   => $this->ctxForm,
		);
	}

}