1: <?php
2: namespace TIC\CoreBundle\Traits;
3:
4: /**
5: * Initialisation de propriétés d'après le contexte de la classe courante.
6: *
7: * Utile pour déterminer automatiquement les noms des éléments associés :
8: * - racine commune du namespace (bundle ou app)
9: * - classe de l'objet manipulé (Entity)
10: * - classe du formulaire à utiliser (FormType)
11: * - sous-dossier contenant les templates des vues
12: * - préfixe pour le nommage des routes du controller
13: * - préfixe pour le nommage des tokens de traduction
14: */
15: trait ContextProperties
16: {
17: protected $ctxClass; /** nom complet de la classe courante (ex: 'App\Controller\Admin\FooBarController') */
18: protected $ctxType; /** type de la classe courante (ex: 'Controller', 'Command') */
19: protected $ctxRoot; /** espace de nommage de base du bundle (ex: 'App', 'TIC\DemoBundle') */
20: protected $ctxName; /** nom du controlleur en camel case (ex: 'Default', 'User', 'Book', 'FooBar', ...) */
21: protected $ctxPath; /** nom du sous-dossier du controlleur (ex: '', 'Admin', 'Front', ...) */
22:
23: protected $ctxBundle; /** alias du bundle pour repo, views... (ex: null, '@TICDemo') */
24: protected $ctxSnake; /** nom du controlleur en snake case (ex: 'default', 'book', 'foo_bar') */
25: protected $ctxRoute; /** préfixe utilisé pour les routes (ex: 'app_foobar_', 'admin_foobar_', 'ticdemo_foobar_') */
26: protected $ctxTrans; /** préfixe utilisé pour les tokens (ex: 'app.foobar.', 'admin.foobar.', 'ticdemo.foobar.') */
27: protected $ctxMesg = 'item'; /** nom dans les tokens génériques (ex: 'item', 'user', 'book", 'foobar') */
28: protected $ctxForm; /** classname du FormType correspondant (ex: 'App\Form\FooBarType', 'App\Form\Admin\BookType', 'TIC\DemoBundle\Form\ItemType') */
29:
30: /**
31: * DI avec auto-wiring (inutile de définir le "call" sur le service grace à l'attribut @required).
32: * @see https://symfony.com/doc/current/service_container/injection_types.html#setter-injection
33: * @see https://symfony.com/doc/current/service_container/autowiring.html#autowiring-other-methods-e-g-setters-and-public-typed-properties
34: *
35: * @required
36: */
37: public function setContextProperties(?string $classType = null): void
38: {
39: if (null === $this->ctxClass) $this->ctxClass = static::class;
40:
41: // expression régulière pour l'extraction des composants du namespace
42: if (null === $classType) $classType = 'Controller|Command|Form|Type|Repository|Model|Service';
43: $re = '/^(.+)\\\\('.$classType.')\\\\(.+\\\\)?([^\\\\]+)('.$classType.')$/';
44:
45: // analyse des parties du nom complet de la classe courante
46: if (\preg_match($re, $this->ctxClass, $match)) {
47: if (null === $this->ctxType) $this->ctxType = $match[2];
48: if (null === $this->ctxRoot) $this->ctxRoot = $match[1];
49: if (null === $this->ctxName) $this->ctxName = $match[4];
50: if (null === $this->ctxPath) $this->ctxPath = \trim($match[3], "\\");
51: }
52: // détermination de la référence de bundle
53: if (null === $this->ctxBundle)
54: $this->ctxBundle = \preg_match('/^(.+)Bundle$/', $this->ctxRoot, $match) ? '@'.\str_replace("\\",'',$match[1]) : null;
55: // détermination du nom de base en snake case
56: if (null === $this->ctxSnake)
57: $this->ctxSnake = \strtolower(\trim(\preg_replace("/([A-Z][^A-Z])/", '_\1', $this->ctxName), '_'));
58:
59: // détermination du préfixe des noms de routes
60: if (null === $this->ctxRoute) {
61: if ($this->ctxBundle) {
62: $this->ctxRoute = \strtolower(\str_replace('@', '', $this->ctxBundle));
63: if (! empty($this->ctxPath)) $this->ctxRoute.= '_' . \strtolower($this->ctxPath);
64: } else {
65: $this->ctxRoute = empty($this->ctxPath) ? 'app' : \strtolower($this->ctxPath);
66: }
67: $this->ctxRoute.= '_' . \strtolower($this->ctxName) . '_';
68: }
69:
70: // détermination du préfixe pour les tokens de traduction
71: if (null === $this->ctxTrans)
72: $this->ctxTrans = \strtr($this->ctxRoute, '_', '.');
73: // détermination du nom pour les tokens de traduction génériques
74: if (null === $this->ctxMesg)
75: $this->ctxMesg = \strtolower($this->ctxName);
76:
77: // détermination de la class du FormType correspondant
78: if (null === $this->ctxForm) {
79: $formClass = array( $this->ctxRoot, "Form" );
80: if (! empty($this->ctxPath)) $formClass[] = $this->ctxPath;
81: $formClass[] = $this->ctxName . "Type";
82: $this->ctxForm = \implode("\\", $formClass);
83: }
84:
85: # dump($this->getContextProperties());
86: }
87:
88: /**
89: * Retourne la liste des propriétés définies dans un tableaux (utile pour du debug).
90: */
91: protected function getContextProperties(): array
92: {
93: return array(
94: "class" => $this->ctxClass,
95: "type" => $this->ctxType,
96: "root" => $this->ctxRoot,
97: "name" => $this->ctxName,
98: "path" => $this->ctxPath,
99: "bundle" => $this->ctxBundle,
100: "snake" => $this->ctxSnake,
101: "route" => $this->ctxRoute,
102: "trans" => $this->ctxTrans,
103: "mesg" => $this->ctxMesg,
104: "form" => $this->ctxForm,
105: );
106: }
107:
108: }
109: