afshinpersian /
afshlim_framework
| 1 | <?php |
||||
| 2 | |||||
| 3 | |||||
| 4 | use Illuminate\Database\Capsule\Manager as Capsule; |
||||
| 5 | $container = $app->getContainer(); |
||||
| 6 | |||||
| 7 | /* database connection */ |
||||
| 8 | $container['db'] = function ($container) { |
||||
| 9 | $db = $container['settings']['databases']['db']; |
||||
| 10 | $pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['database'], |
||||
| 11 | $db['username'], $db['password']); |
||||
| 12 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||||
| 13 | $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); |
||||
| 14 | return $pdo; |
||||
| 15 | }; |
||||
| 16 | |||||
| 17 | $moduleInitializer = new \Core\ModuleInitializer($app, [ |
||||
| 18 | 'App\\Module\\Hello' |
||||
| 19 | ]); |
||||
| 20 | |||||
| 21 | $moduleInitializer->initModules(); |
||||
| 22 | |||||
| 23 | |||||
| 24 | |||||
| 25 | // $container is application's DIC container. |
||||
| 26 | // Setup Paginator resolvers |
||||
| 27 | Illuminate\Pagination\Paginator::currentPageResolver(function ($pageName = 'page') use ($container) { |
||||
| 28 | |||||
| 29 | $page = $container->request->getParam($pageName); |
||||
| 30 | if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) { |
||||
| 31 | return $page; |
||||
| 32 | } |
||||
| 33 | return 1; |
||||
| 34 | }); |
||||
| 35 | |||||
| 36 | |||||
| 37 | // $container['mailer'] = function ($container) { |
||||
| 38 | // $config = $container->settings['swiftmailer']; |
||||
| 39 | // $allowedTransportTypes = ['smtp', 'sendmail']; |
||||
| 40 | // if (false === $config['enable'] |
||||
| 41 | // || !in_array($config['transport_type'], $allowedTransportTypes) |
||||
| 42 | // ) { |
||||
| 43 | // return false; |
||||
| 44 | // } |
||||
| 45 | // if ('smtp' === $config['transport_type']) { |
||||
| 46 | // $transport = new \Swift_SmtpTransport(); |
||||
| 47 | // } elseif ('sendmail' === $config['transport_type']) { |
||||
| 48 | // $transport = new \Swift_SendmailTransport(); |
||||
| 49 | // } |
||||
| 50 | // if (isset($config[$config['transport_type']]) |
||||
| 51 | // && is_array($config[$config['transport_type']]) |
||||
| 52 | // && count($config[$config['transport_type']]) |
||||
| 53 | // ) { |
||||
| 54 | // foreach ($config[$config['transport_type']] as $optionKey => $optionValue) { |
||||
| 55 | // $methodName = 'set' . str_replace('_', '', ucwords($optionKey, '_')); |
||||
| 56 | // $transport->{$methodName}($optionValue); |
||||
| 57 | // } |
||||
| 58 | // } |
||||
| 59 | // return new \Swift_Mailer($transport); |
||||
| 60 | // }; |
||||
| 61 | |||||
| 62 | |||||
| 63 | |||||
| 64 | |||||
| 65 | |||||
| 66 | // |
||||
| 67 | |||||
| 68 | |||||
| 69 | $container['generalErrorHandler'] = function ($container) { |
||||
| 70 | return new \Core\Handlers\GeneralErrorHandler($container); |
||||
|
0 ignored issues
–
show
|
|||||
| 71 | }; |
||||
| 72 | |||||
| 73 | // $container['errorHandler'] = function ($container) { |
||||
| 74 | // return function ($request, $response, $exception) use ($container) { |
||||
| 75 | // return $container['response']->withStatus(500) |
||||
| 76 | // ->withHeader('Content-Type', 'text/html') |
||||
| 77 | // ->write('Something went wrong!'); |
||||
| 78 | // }; |
||||
| 79 | // }; |
||||
| 80 | |||||
| 81 | // Service factory for the ORM |
||||
| 82 | $container['validator'] = function () { |
||||
| 83 | return new App\Validation\Validator(); |
||||
| 84 | }; |
||||
| 85 | |||||
| 86 | |||||
| 87 | $container['eloquent'] = function ($container) { |
||||
| 88 | $db = $container['settings']['databases']['db']; |
||||
| 89 | |||||
| 90 | $capsule = new \Illuminate\Database\Capsule\Manager; |
||||
| 91 | $capsule->addConnection($db); |
||||
| 92 | $capsule->setAsGlobal(); |
||||
| 93 | $capsule->bootEloquent(); |
||||
| 94 | $capsule::connection()->enableQueryLog(); |
||||
| 95 | |||||
| 96 | return $capsule; |
||||
| 97 | }; |
||||
| 98 | |||||
| 99 | // database |
||||
| 100 | $capsule = new Capsule; |
||||
| 101 | $capsule->addConnection($config['settings']['databases']['db']); |
||||
| 102 | $capsule->setAsGlobal(); |
||||
| 103 | $capsule->bootEloquent(); |
||||
| 104 | |||||
| 105 | // monolog |
||||
| 106 | $container['logger'] = function ($container) { |
||||
| 107 | $settings = $container->get('settings')['app']['logger']; |
||||
| 108 | $logger = new Monolog\Logger($settings['name']); |
||||
| 109 | $logger->pushProcessor(new Monolog\Processor\UidProcessor()); |
||||
| 110 | $logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level'])); |
||||
| 111 | return $logger; |
||||
| 112 | }; |
||||
| 113 | |||||
| 114 | // not found handler |
||||
| 115 | $container['notFoundHandler'] = function ($container) { |
||||
| 116 | return function (\Slim\Http\Request $request, \Slim\Http\Response $response) use ($container) { |
||||
| 117 | return $container['view']->render($response->withStatus(404), '404'); |
||||
| 118 | }; |
||||
| 119 | }; |
||||
| 120 | |||||
| 121 | |||||
| 122 | //$app->getContainer()->get('blade')->get('global-var'); |
||||
| 123 | $translator = new \Core\Translator\Translator($container); |
||||
| 124 | $translator->init(); |
||||
| 125 | |||||
| 126 | $container['translator'] = function () use ($translator) { |
||||
| 127 | return $translator; |
||||
| 128 | }; |
||||
| 129 | |||||
| 130 | |||||
| 131 | // Register provider |
||||
| 132 | $container['flash'] = function () { |
||||
| 133 | return new \Slim\Flash\Messages(); |
||||
| 134 | }; |
||||
| 135 | |||||
| 136 | |||||
| 137 | // |
||||
| 138 | $container['session'] = function ($container) { |
||||
| 139 | $setting_session_driver = $container['settings']['session']['driver'] ?? 'session'; |
||||
| 140 | $sessionOBJ = new \Core\Services\Session(); |
||||
| 141 | $session = $sessionOBJ->init($setting_session_driver) ; |
||||
| 142 | return $session; |
||||
| 143 | }; |
||||
| 144 | |||||
| 145 | //$setting_session_driver = $container[' ings']['session']['driver'] ?? 'session'; |
||||
| 146 | // |
||||
| 147 | //$sessionOBJ = new \Core\Services\Session($setting_session_driver); |
||||
| 148 | //$session = $sessionOBJ->init('session') ; |
||||
| 149 | |||||
| 150 | |||||
| 151 | // Register Blade View helper |
||||
| 152 | $container['view'] = function ($container) { |
||||
| 153 | $messages = $container->flash->getMessages(); |
||||
| 154 | |||||
| 155 | if(!is_dir('../app/View/cache')){ |
||||
| 156 | @mkdir('../app/View/cache'); |
||||
|
0 ignored issues
–
show
It seems like you do not handle an error condition for
mkdir(). This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||||
| 157 | } |
||||
| 158 | |||||
| 159 | |||||
| 160 | $viewSettings = $container['settings']['view']; |
||||
| 161 | return new \Slim\Views\Blade( |
||||
| 162 | [$viewSettings['blade_template_path'].$viewSettings['template']], |
||||
| 163 | $viewSettings['blade_cache_path'], |
||||
| 164 | null, |
||||
| 165 | [ |
||||
| 166 | 'translator'=> $container['translator'], |
||||
| 167 | 'messages'=> $messages, |
||||
| 168 | 'settings' => $container->settings |
||||
| 169 | ] |
||||
| 170 | ); |
||||
| 171 | }; |
||||
| 172 | |||||
| 173 | // Register Blade View helper |
||||
| 174 | $container['json'] = function ($container) { |
||||
|
0 ignored issues
–
show
The parameter
$container is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 175 | return new \Core\Handlers\JsonHandler(); |
||||
| 176 | }; |
||||
| 177 | |||||
| 178 | $container['jsonApi'] = function ($container) { |
||||
|
0 ignored issues
–
show
The parameter
$container is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 179 | return new \Core\Renderers\JsonApiRenderer(); |
||||
| 180 | }; |
||||
| 181 | |||||
| 182 | |||||
| 183 | |||||
| 184 | $app->getContainer()['view']->getRenderer()->getCompiler()->directive('helloWorld', function(){ |
||||
| 185 | |||||
| 186 | return "<?php echo 'Hello Directive'; ?>"; |
||||
| 187 | }); |
||||
| 188 | |||||
| 189 | |||||
| 190 | |||||
| 191 | /*Dynamic containers in services*/ |
||||
| 192 | $dir = scandir(__APP_ROOT__.'/core/Services/'); |
||||
| 193 | $ex_folders = array('..', '.'); |
||||
| 194 | $filesInServices = array_diff($dir,$ex_folders); |
||||
| 195 | |||||
| 196 | foreach($filesInServices as $service){ |
||||
| 197 | $content = preg_replace('/.php/','',$service); |
||||
| 198 | $container[$content] = function () use ($content){ |
||||
| 199 | $class = '\\Core\\Services\\'.$content ; |
||||
| 200 | return new $class(); |
||||
| 201 | }; |
||||
| 202 | } |
||||
| 203 | |||||
| 204 | |||||
| 205 | // data access container |
||||
| 206 | $array = getDirFiles(__APP_ROOT__.'/app/DataAccess/'); |
||||
| 207 | foreach($array as $key=>$item){ |
||||
| 208 | $classDataAccessFolder[$item] = getDirFiles(__APP_ROOT__.'/app/DataAccess/'.$item); |
||||
| 209 | |||||
| 210 | } |
||||
| 211 | $result = array(); |
||||
| 212 | foreach($classDataAccessFolder as $DaFolder=>$DAFile) |
||||
| 213 | { |
||||
| 214 | foreach($DAFile as $r){ |
||||
| 215 | $dataAccessFiles[$r] = $DaFolder.'\\'.$r; |
||||
| 216 | } |
||||
| 217 | } |
||||
| 218 | |||||
| 219 | foreach($dataAccessFiles as $key=>$dataAccessFile){ |
||||
| 220 | $contentDataAccess = preg_replace('/.php/','',$dataAccessFile); |
||||
| 221 | $containerDataAccess = preg_replace('/.php/','',$key); |
||||
| 222 | $container[$containerDataAccess] = function ($container) use ($contentDataAccess){ |
||||
| 223 | $classDataAccess = '\\App\\DataAccess\\'.$contentDataAccess ; |
||||
| 224 | return new $classDataAccess($container); |
||||
| 225 | }; |
||||
| 226 | } |
||||
| 227 | |||||
| 228 | |||||
| 229 | |||||
| 230 | |||||
| 231 | |||||
| 232 | $GLOBALS['container'] = $container; |
||||
| 233 | $GLOBALS['app'] = $app; |
||||
| 234 | $GLOBALS['settings'] = $container['settings']; |
||||
| 235 | |||||
| 236 | |||||
| 237 | |||||
| 238 | return $container; |
||||
| 239 | |||||
| 240 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.