]> Raphaël G. Git Repositories - airbundle/blob - Command.php
Rename rapsysair:calendar2 command to rapsysair:calendar
[airbundle] / Command.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle;
13
14 use Doctrine\Persistence\ManagerRegistry;
15
16 use Symfony\Component\Console\Command\Command as BaseCommand;
17 use Symfony\Component\Routing\RouterInterface;
18 use Symfony\Contracts\Translation\TranslatorInterface;
19
20 use Rapsys\AirBundle\RapsysAirBundle;
21
22 use Rapsys\PackBundle\Util\SluggerUtil;
23
24 /**
25 * {@inheritdoc}
26 */
27 class Command extends BaseCommand {
28 /**
29 * {@inheritdoc}
30 *
31 * Creates new command
32 *
33 * @param ManagerRegistry $doctrine The doctrine instance
34 * @param RouterInterface $router The router instance
35 * @param SluggerUtil $slugger The slugger instance
36 * @param TranslatorInterface $translator The translator instance
37 * @param string $locale The default locale
38 */
39 public function __construct(protected ManagerRegistry $doctrine, protected string $locale, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected ?string $name = null) {
40 //Fix name
41 $this->name = $this->name ?? static::getName();
42
43 //Call parent constructor
44 parent::__construct($this->name);
45
46 //With description
47 if (!empty($this->description)) {
48 //Set description
49 $this->setDescription($this->description);
50 }
51
52 //With help
53 if (!empty($this->help)) {
54 //Set help
55 $this->setHelp($this->help);
56 }
57
58 //Get router context
59 $context = $this->router->getContext();
60
61 //Set hostname
62 $context->setHost($_ENV['RAPSYSAIR_HOSTNAME']);
63
64 //Set scheme
65 $context->setScheme($_ENV['RAPSYSAIR_SCHEME'] ?? 'https');
66 }
67
68 /**
69 * {@inheritdoc}
70 *
71 * Return the command name
72 */
73 public function getName(): string {
74 //With namespace
75 if ($npos = strrpos(static::class, '\\')) {
76 //Set name pos
77 $npos++;
78 //Without namespace
79 } else {
80 $npos = 0;
81 }
82
83 //With trailing command
84 if (substr(static::class, -strlen('Command'), strlen('Command')) === 'Command') {
85 //Set bundle pos
86 $bpos = strlen(static::class) - $npos - strlen('Command');
87 //Without bundle
88 } else {
89 //Set bundle pos
90 $bpos = strlen(static::class) - $npos;
91 }
92
93 //Return command alias
94 return RapsysAirBundle::getAlias().':'.strtolower(substr(static::class, $npos, $bpos));
95 }
96 }