|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +/** | 
|  | 4 | + * This file is part of the Phalcon API. | 
|  | 5 | + * | 
|  | 6 | + * (c) Phalcon Team <[email protected]> | 
|  | 7 | + * | 
|  | 8 | + * For the full copyright and license information, please view | 
|  | 9 | + * the LICENSE file that was distributed with this source code. | 
|  | 10 | + */ | 
|  | 11 | + | 
|  | 12 | +declare(strict_types=1); | 
|  | 13 | + | 
|  | 14 | +namespace Phalcon\Api\Domain\Services; | 
|  | 15 | + | 
|  | 16 | +use Phalcon\Api\Domain\Services\Environment\EnvManager; | 
|  | 17 | +use Phalcon\Api\Domain\Services\Exceptions\InvalidConfigurationArguments; | 
|  | 18 | +use Phalcon\Cache\AdapterFactory; | 
|  | 19 | +use Phalcon\Cache\Cache; | 
|  | 20 | +use Phalcon\DataMapper\Pdo\Connection; | 
|  | 21 | +use Phalcon\Di\Di; | 
|  | 22 | +use Phalcon\Di\Service; | 
|  | 23 | +use Phalcon\Encryption\Security; | 
|  | 24 | +use Phalcon\Events\Manager as EventsManager; | 
|  | 25 | +use Phalcon\Filter\FilterFactory; | 
|  | 26 | +use Phalcon\Http\Message\Request; | 
|  | 27 | +use Phalcon\Http\Message\Response; | 
|  | 28 | +use Phalcon\Logger\Adapter\Stream; | 
|  | 29 | +use Phalcon\Logger\Logger; | 
|  | 30 | +use Phalcon\Mvc\Router; | 
|  | 31 | +use Phalcon\Storage\SerializerFactory; | 
|  | 32 | + | 
|  | 33 | +use function array_merge; | 
|  | 34 | +use function sprintf; | 
|  | 35 | + | 
|  | 36 | +class Container extends Di | 
|  | 37 | +{ | 
|  | 38 | +    /** @var string */ | 
|  | 39 | +    public const APPLICATION = 'application'; | 
|  | 40 | +    /** @var string */ | 
|  | 41 | +    public const CACHE = 'cache'; | 
|  | 42 | +    /** @var string */ | 
|  | 43 | +    public const CONNECTION = 'connection'; | 
|  | 44 | +    /** @var string */ | 
|  | 45 | +    public const EVENTS_MANAGER = 'eventsManager'; | 
|  | 46 | +    /** @var string */ | 
|  | 47 | +    public const FILTER = 'filter'; | 
|  | 48 | +    /** @var string */ | 
|  | 49 | +    public const LOGGER = 'logger'; | 
|  | 50 | +    /** @var string */ | 
|  | 51 | +    public const REQUEST = 'request'; | 
|  | 52 | +    /** @var string */ | 
|  | 53 | +    public const RESPONSE = 'response'; | 
|  | 54 | +    /** @var string */ | 
|  | 55 | +    public const ROUTER = 'router'; | 
|  | 56 | +    /** @var string */ | 
|  | 57 | +    public const SECURITY = 'security'; | 
|  | 58 | +    /** @var string */ | 
|  | 59 | +    public const TIME = 'time'; | 
|  | 60 | + | 
|  | 61 | +    /** | 
|  | 62 | +     * @throws InvalidConfigurationArguments | 
|  | 63 | +     */ | 
|  | 64 | +    public function __construct() | 
|  | 65 | +    { | 
|  | 66 | +        /** @var array<string, Service> $services */ | 
|  | 67 | +        $services = $this->services; | 
|  | 68 | + | 
|  | 69 | +        $this->services = array_merge( | 
|  | 70 | +            [ | 
|  | 71 | +                self::LOGGER         => $this->getServiceLogger(), | 
|  | 72 | +                self::CACHE          => $this->getServiceCache(), | 
|  | 73 | +                self::CONNECTION     => $this->getServiceConnection(), | 
|  | 74 | +                self::EVENTS_MANAGER => $this->getServiceEventsManger(), | 
|  | 75 | +                self::FILTER         => $this->getServiceFilter(), | 
|  | 76 | +                self::REQUEST        => $this->getServiceRequest(), | 
|  | 77 | +                self::RESPONSE       => $this->getServiceResponse(), | 
|  | 78 | +                self::ROUTER         => $this->getServiceRouter(), | 
|  | 79 | +                self::SECURITY       => $this->getServiceSecurity(), | 
|  | 80 | +            ], | 
|  | 81 | +            $services | 
|  | 82 | +        ); | 
|  | 83 | + | 
|  | 84 | +        parent::__construct(); | 
|  | 85 | +    } | 
|  | 86 | + | 
|  | 87 | +    /** | 
|  | 88 | +     * @return Service | 
|  | 89 | +     * @throws InvalidConfigurationArguments | 
|  | 90 | +     */ | 
|  | 91 | +    private function getServiceCache(): Service | 
|  | 92 | +    { | 
|  | 93 | +        $adapter = EnvManager::getString('CACHE_ADAPTER', 'redis'); | 
|  | 94 | +        $options = EnvManager::getCacheOptions(); | 
|  | 95 | +        return new Service( | 
|  | 96 | +            function () use ($adapter, $options) { | 
|  | 97 | +                return new Cache( | 
|  | 98 | +                    (new AdapterFactory(new SerializerFactory())) | 
|  | 99 | +                        ->newInstance($adapter, $options) | 
|  | 100 | +                ); | 
|  | 101 | +            }, | 
|  | 102 | +            true | 
|  | 103 | +        ); | 
|  | 104 | +    } | 
|  | 105 | + | 
|  | 106 | +    /** | 
|  | 107 | +     * @return Service | 
|  | 108 | +     */ | 
|  | 109 | +    private function getServiceConnection(): Service | 
|  | 110 | +    { | 
|  | 111 | +        return new Service( | 
|  | 112 | +            function () { | 
|  | 113 | +                $dbname   = EnvManager::getString('DB_NAME', 'phalcon'); | 
|  | 114 | +                $host     = EnvManager::getString('DB_HOST', 'rest-db'); | 
|  | 115 | +                $password = EnvManager::getString('DB_PASSWORD', 'secret'); | 
|  | 116 | +                $port     = (int)EnvManager::get('DB_PORT', 3306); | 
|  | 117 | +                $username = EnvManager::getString('DB_USER', 'phalcon'); | 
|  | 118 | +                $encoding = 'utf8'; | 
|  | 119 | +                $queries  = ['SET NAMES utf8mb4']; | 
|  | 120 | +                $dsn      = sprintf( | 
|  | 121 | +                    "mysql:host=%s;dbname=%s;charset=%s;port=%s", | 
|  | 122 | +                    $host, | 
|  | 123 | +                    $dbname, | 
|  | 124 | +                    $encoding, | 
|  | 125 | +                    $port | 
|  | 126 | +                ); | 
|  | 127 | + | 
|  | 128 | +                return new Connection( | 
|  | 129 | +                    $dsn, | 
|  | 130 | +                    $username, | 
|  | 131 | +                    $password, | 
|  | 132 | +                    [], | 
|  | 133 | +                    $queries | 
|  | 134 | +                ); | 
|  | 135 | +            }, | 
|  | 136 | +            true | 
|  | 137 | +        ); | 
|  | 138 | +    } | 
|  | 139 | + | 
|  | 140 | +    /** | 
|  | 141 | +     * @return Service | 
|  | 142 | +     */ | 
|  | 143 | +    private function getServiceEventsManger(): Service | 
|  | 144 | +    { | 
|  | 145 | +        return new Service( | 
|  | 146 | +            function () { | 
|  | 147 | +                $em = new EventsManager(); | 
|  | 148 | +                $em->enablePriorities(true); | 
|  | 149 | + | 
|  | 150 | +                return $em; | 
|  | 151 | +            } | 
|  | 152 | +        ); | 
|  | 153 | +    } | 
|  | 154 | + | 
|  | 155 | +    /** | 
|  | 156 | +     * @return Service | 
|  | 157 | +     */ | 
|  | 158 | +    private function getServiceFilter(): Service | 
|  | 159 | +    { | 
|  | 160 | +        return new Service( | 
|  | 161 | +            function () { | 
|  | 162 | +                return (new FilterFactory())->newInstance(); | 
|  | 163 | +            }, | 
|  | 164 | +            true | 
|  | 165 | +        ); | 
|  | 166 | +    } | 
|  | 167 | + | 
|  | 168 | +    /** | 
|  | 169 | +     * @return Service | 
|  | 170 | +     */ | 
|  | 171 | +    private function getServiceLogger(): Service | 
|  | 172 | +    { | 
|  | 173 | +        return new Service( | 
|  | 174 | +            function () { | 
|  | 175 | +                $fileName = EnvManager::getString('USER_LOG_FILENAME', 'rest'); | 
|  | 176 | +                $logPath  = EnvManager::getString('USER_LOG_PATH', 'storage/logs'); | 
|  | 177 | +                $logFile  = EnvManager::appPath($logPath) | 
|  | 178 | +                    . '/' . $fileName . '.log'; | 
|  | 179 | + | 
|  | 180 | +                return new Logger( | 
|  | 181 | +                    $fileName, | 
|  | 182 | +                    [ | 
|  | 183 | +                        'main' => new Stream($logFile), | 
|  | 184 | +                    ] | 
|  | 185 | +                ); | 
|  | 186 | +            }, | 
|  | 187 | +            true | 
|  | 188 | +        ); | 
|  | 189 | +    } | 
|  | 190 | + | 
|  | 191 | +    /** | 
|  | 192 | +     * @return Service | 
|  | 193 | +     */ | 
|  | 194 | +    private function getServiceRequest(): Service | 
|  | 195 | +    { | 
|  | 196 | +        return new Service( | 
|  | 197 | +            [ | 
|  | 198 | +                'className' => Request::class, | 
|  | 199 | +            ], | 
|  | 200 | +            true | 
|  | 201 | +        ); | 
|  | 202 | +    } | 
|  | 203 | + | 
|  | 204 | +    /** | 
|  | 205 | +     * @return Service | 
|  | 206 | +     */ | 
|  | 207 | +    private function getServiceResponse(): Service | 
|  | 208 | +    { | 
|  | 209 | +        return new Service( | 
|  | 210 | +            [ | 
|  | 211 | +                'className' => Response::class, | 
|  | 212 | +            ], | 
|  | 213 | +            true | 
|  | 214 | +        ); | 
|  | 215 | +    } | 
|  | 216 | + | 
|  | 217 | +    /** | 
|  | 218 | +     * @return Service | 
|  | 219 | +     */ | 
|  | 220 | +    private function getServiceRouter(): Service | 
|  | 221 | +    { | 
|  | 222 | +        return new Service( | 
|  | 223 | +            [ | 
|  | 224 | +                'className' => Router::class, | 
|  | 225 | +                'arguments' => [ | 
|  | 226 | +                    [ | 
|  | 227 | +                        'type'  => 'parameter', | 
|  | 228 | +                        'value' => false, | 
|  | 229 | +                    ] | 
|  | 230 | +                ] | 
|  | 231 | +            ], | 
|  | 232 | +            true | 
|  | 233 | +        ); | 
|  | 234 | +    } | 
|  | 235 | + | 
|  | 236 | +    /** | 
|  | 237 | +     * @return Service | 
|  | 238 | +     */ | 
|  | 239 | +    private function getServiceSecurity(): Service | 
|  | 240 | +    { | 
|  | 241 | +        return new Service( | 
|  | 242 | +            [ | 
|  | 243 | +                'className' => Security::class, | 
|  | 244 | +            ], | 
|  | 245 | +            true | 
|  | 246 | +        ); | 
|  | 247 | +    } | 
|  | 248 | +} | 
0 commit comments