Issues (9)

config/container.php (1 issue)

Labels
Severity
1
<?php
2
3
use App\Table\UserModel;
0 ignored issues
show
The type App\Table\UserModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
use Aura\Session\Session;
5
use Aura\Session\SessionFactory;
6
use Cake\Database\Connection;
7
use Cake\Database\Driver\Mysql;
8
use Interop\Container\Exception\ContainerException;
9
use Monolog\Logger;
10
use Odan\Twig\TwigAssetsExtension;
11
use Odan\Twig\TwigTranslationExtension;
12
use Slim\Container;
13
use Slim\Http\Environment;
14
use Slim\Http\Request;
15
use Slim\Http\Response;
16
use Slim\Views\Twig;
17
use Symfony\Component\Translation\Loader\MoFileLoader;
18
use Symfony\Component\Translation\MessageSelector;
19
use Symfony\Component\Translation\Translator;
20
21
$container = $app->getContainer();
22
23
/**
24
 * Environment container (for routes).
25
 *
26
 * @return Environment
27
 */
28
$container['environment'] = function (): Environment {
29
    $scriptName = $_SERVER['SCRIPT_NAME'];
30
    $_SERVER['SCRIPT_NAME'] = dirname(dirname($scriptName)) . '/' . basename($scriptName);
31
32
    return new Slim\Http\Environment($_SERVER);
33
};
34
35
/**
36
 * Twig container.
37
 *
38
 * @param Container $container
39
 * @return Twig
40
 * @throws ContainerException
41
 */
42
$container[Twig::class] = function (Container $container): Twig {
43
    $twigSettings = $container->get('settings')->get('twig');
44
45
    $basePath = rtrim(str_ireplace('index.php', '', $container->get('request')->getUri()->getBasePath()), '/');
46
47
    $twig = new Twig($twigSettings['viewPath'],
48
        ['cache' => $twigSettings['cachePath'], 'auto_reload' => $twigSettings['autoReload']]);
49
    $twig->addExtension(new TwigTranslationExtension());
50
    $twig->addExtension(new \Slim\Views\TwigExtension($container->get('router'), $basePath));
51
    $twig->addExtension(new TwigAssetsExtension($twig->getEnvironment(), $twigSettings['assetCache']));
52
53
    return $twig;
54
};
55
56
/**
57
 * Translator container.
58
 *
59
 * @param Container $container
60
 * @return Translator $translator
61
 * @throws ContainerException
62
 */
63
$container[Translator::class] = function (Container $container): Translator {
64
    $settings = $container->get('settings')->get(Translator::class);
65
    $translator = new Translator($settings['locale'], /** @scrutinizer ignore-type */
66
        new MessageSelector());
67
    $translator->addLoader('mo', new MoFileLoader());
68
69
    return $translator;
70
};
71
72
/**
73
 * Database connection container.
74
 *
75
 * @param Container $container
76
 * @return Connection
77
 * @throws ContainerException
78
 */
79
$container[Connection::class] = function (Container $container): Connection {
80
    $config = $container->get('settings')->get('db');
81
    $driver = new Mysql([
82
        'host' => $config['host'],
83
        'port' => $config['port'],
84
        'database' => $config['database'],
85
        'username' => $config['username'],
86
        'password' => $config['password'],
87
        'encoding' => $config['encoding'],
88
        'charset' => $config['charset'],
89
        'collation' => $config['collation'],
90
        'prefix' => '',
91
        'flags' => [
92
            // Enable exceptions
93
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
94
            // Set default fetch mode
95
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
96
            PDO::ATTR_PERSISTENT => false,
97
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8 COLLATE utf8_unicode_ci",
98
        ],
99
    ]);
100
    $db = new Connection([
101
        'driver' => $driver,
102
    ]);
103
104
    $db->connect();
105
106
    return $db;
107
};
108
109
/**
110
 * Session container.
111
 *
112
 * @param Container $container
113
 * @return Session
114
 * @throws ContainerException
115
 */
116
$container[Session::class] = function (Container $container): Session {
117
    $factory = new SessionFactory();
118
    $cookies = $container->get('request')->getCookieParams();
119
    $session = $factory->newInstance($cookies);
120
    $settings = $container->get('settings')->get(Session::class);
121
    $session->setName($settings['name']);
122
    $session->setCacheExpire($settings['cache_expire']);
123
124
    return $session;
125
};
126
127
/**
128
 * Logger container.
129
 *
130
 * @param Container $container
131
 * @return Logger
132
 * @throws ContainerException
133
 */
134
$container[Monolog\Logger::class] = function (Container $container) {
135
    return new Logger($container->get('settings')->get('logger')['main']);
136
};
137
138
/**
139
 * Not found handler.
140
 *
141
 * @param Container $container
142
 * @return Closure
143
 */
144
$container['notFoundHandler'] = function (Container $container) {
145
    return function (Request $request, Response $response) use ($container) {
146
        return $response->withRedirect($container->get('router')->pathFor('notFound', ['language' => 'en']));
147
    };
148
};
149