Hi,
I’m trying to create some kinds of import to move database info and transform data. In a feture this import needs to be executed by cron every day. I want to use part of my wrote code and reuse some models and controllers. To do this i’m trying to call Slim 3 through command line, but I have some problems.
First of one is commands:
php cli.php import
I don’t know how to process argv correctly.
Also, i have NotFound and Error Handlers with response object, but i don’t need response, i need plain text.
To do this i have cli.php:
require __DIR__ . '/vendor/autoload.php';
if (PHP_SAPI == 'cli') {
$argv = $GLOBALS['argv'];
array_shift($argv);
$pathInfo = implode('/', $argv);
$env = \Slim\Http\Environment::mock(['PATH_INFO' => $pathInfo]);
$settings = require __DIR__ . '/app/config/settings.php'; // here are return ['settings'=>'']
//i try adding here path_info but this is wrong, i'm sure
$settings['environment'] = $env;
$app = new \Slim\App($settings);
$container = $app->getContainer();
$container['errorHandler'] = function ($c) {
return function ($request, $response, $exception) use ($c) {
//this is wrong, i'm not with http
return $c['response']->withStatus(500)
->withHeader('Content-Type', 'text/text')
->write('Something went wrong!');
};
};
$c['notFoundHandler'] = function ($c) {
//this is wrong, i'm not with http
return function ($request, $response) use ($c) {
return $c['response']
->withStatus(404)
->withHeader('Content-Type', 'text/text')
->write('lalalalal');
};
};
$app->map(['GET'], 'import', function() {
// do import
});
}
If i execute this i see a 404 Page not found html.
Any directions?
Thanks
EDIT:
I tryed to changes error handlers after reading some PSR-7 docs without luck:
$container['errorHandler'] = function ($c) {
return function ($request, $response, $exception) use ($c) {
$x = $response->getBody();
$x->write("error");
return $response->withBody($x);
};
};
$c['notFoundHandler'] = function ($c) {
return function ($request, $response) use ($c) {
$x = $response->getBody();
$x->write("command not found");
return $response->withBody($x);
};
};