Added demo recreation handling

This commit is contained in:
Bernd Bestel 2017-07-27 19:11:07 +02:00
parent 9c2ee58433
commit 6d4b86a730
3 changed files with 54 additions and 13 deletions

View File

@ -65,4 +65,12 @@ class GrocyDemoDataGenerator
GrocyLogicHabits::TrackHabit(2, date('Y-m-d H:i:s', strtotime('-20 days')));
}
}
public static function RecreateDemo()
{
unlink(__DIR__ . '/data/grocy.db');
$db = Grocy::GetDbConnectionRaw(true);
self::PopulateDemoData($db);
}
}

View File

@ -1,7 +1,8 @@
{
"require": {
"slim/slim": "^3.8",
"slim/php-view": "^2.2",
"morris/lessql": "^0.3.4"
}
"require": {
"slim/slim": "^3.8",
"slim/php-view": "^2.2",
"morris/lessql": "^0.3.4",
"pavlakis/slim-cli": "^1.0"
}
}

View File

@ -13,14 +13,24 @@ require_once __DIR__ . '/GrocyLogicStock.php';
require_once __DIR__ . '/GrocyLogicHabits.php';
require_once __DIR__ . '/GrocyPhpHelper.php';
$app = new \Slim\App(new \Slim\Container([
'settings' => [
'displayErrorDetails' => true,
'determineRouteBeforeAppMiddleware' => true
],
]));
$container = $app->getContainer();
$container['renderer'] = new PhpRenderer('./views');
$app = new \Slim\App;
if (PHP_SAPI !== 'cli')
{
$app = new \Slim\App(new \Slim\Container([
'settings' => [
'displayErrorDetails' => true,
'determineRouteBeforeAppMiddleware' => true
],
]));
$container = $app->getContainer();
$container['renderer'] = new PhpRenderer('./views');
}
if (PHP_SAPI === 'cli')
{
$app->add(new \pavlakis\cli\CliRequest());
}
if (!Grocy::IsDemoInstallation())
{
@ -430,4 +440,26 @@ $app->group('/api', function() use($db)
return $response->withHeader('Content-Type', 'application/json');
});
$app->group('/cli', function()
{
$this->get('/recreatedemo', function(Request $request, Response $response)
{
if (Grocy::IsDemoInstallation())
{
GrocyDemoDataGenerator::RecreateDemo();
}
});
})->add(function($request, $response, $next)
{
$response = $next($request, $response);
if (PHP_SAPI !== 'cli')
{
echo 'Please call this only from CLI';
return $response->withHeader('Content-Type', 'text/plain')->withStatus(400);
}
return $response->withHeader('Content-Type', 'text/plain');
});
$app->run();