save modified options at terminable middleware instead of destruct function

This commit is contained in:
printempw 2016-12-17 17:07:41 +08:00
parent c9a3c0549b
commit 65acb7426b
7 changed files with 102 additions and 47 deletions

View File

@ -30,6 +30,7 @@ class Kernel extends HttpKernel
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\Internationalization::class,
\App\Http\Middleware\SaveOptionRepository::class,
//\App\Http\Middleware\VerifyCsrfToken::class,
],

View File

@ -0,0 +1,32 @@
<?php
namespace App\Http\Middleware;
use Closure;
class SaveOptionRepository
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request);
}
/**
* Do the really operations of saving modified options.
*
* @param \Illuminate\Http\Request $request
* @param \Symfony\Component\HttpFoundation\Response $response
* @return void
*/
public function terminate($request, $response)
{
app('options')->save();
}
}

View File

@ -223,6 +223,9 @@ class PluginManager
$enabled = array_values(array_unique($enabled));
$this->option->set('plugins_enabled', json_encode($enabled));
// ensure to save options
$this->option->save();
}
/**

View File

@ -71,10 +71,10 @@ class OptionRepository extends Repository
*/
public function save()
{
$this->items_modified = array_unique($this->items_modified);
$this->itemsModified = array_unique($this->itemsModified);
try {
foreach ($this->items_modified as $key) {
foreach ($this->itemsModified as $key) {
if (!DB::table('options')->where('option_name', $key)->first()) {
DB::table('options')
->insert(['option_name' => $key, 'option_value' => $this[$key]]);
@ -84,6 +84,9 @@ class OptionRepository extends Repository
->update(['option_value' => $this[$key]]);
}
}
// clear the list
$this->itemsModified = [];
} catch (QueryException $e) {
return;
}

View File

@ -19,7 +19,7 @@ class Repository implements ArrayAccess // Illuminate\Contracts\Cache\Repository
*
* @var array
*/
protected $items_modified = [];
protected $itemsModified = [];
/**
* Determine if an item exists in the repository.
@ -57,11 +57,11 @@ class Repository implements ArrayAccess // Illuminate\Contracts\Cache\Repository
// If given key is an array
foreach ($key as $innerKey => $innerValue) {
Arr::set($this->items, $innerKey, $innerValue);
$this->items_modified[] = $innerKey;
$this->itemsModified[] = $innerKey;
}
} else {
Arr::set($this->items, $key, $value);
$this->items_modified[] = $key;
$this->itemsModified[] = $key;
}
}

55
bootstrap/app.php Normal file
View File

@ -0,0 +1,55 @@
<?php
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/
return $app;

View File

@ -6,53 +6,14 @@
|--------------------------------------------------------------------------
|
| Blessing Skin Server separated these codes here to ensure that
| runtime check will be executed correctly, since namespaced class names
| will be regarded as parse error under PHP 5.3.
| runtime check at index.php will be executed correctly, since
| namespaced class names will cause parse error under PHP 5.3.
|
*/
require __DIR__.'/autoload.php';
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
$app = require_once __DIR__.'/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);