From 6d4b86a7303f8d4fa715d972bf6578bf40151d10 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Thu, 27 Jul 2017 19:11:07 +0200 Subject: [PATCH] Added demo recreation handling --- GrocyDemoDataGenerator.php | 8 +++++++ composer.json | 11 +++++---- index.php | 48 +++++++++++++++++++++++++++++++------- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/GrocyDemoDataGenerator.php b/GrocyDemoDataGenerator.php index 0c8b7823..b015bfc2 100644 --- a/GrocyDemoDataGenerator.php +++ b/GrocyDemoDataGenerator.php @@ -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); + } } diff --git a/composer.json b/composer.json index c92fd905..771c5dce 100644 --- a/composer.json +++ b/composer.json @@ -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" + } } diff --git a/index.php b/index.php index fc3f8e15..c389167f 100644 --- a/index.php +++ b/index.php @@ -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();