Skip to content

Commit

Permalink
Breaking up the handle function
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterPhilip committed Apr 2, 2018
1 parent 8a11035 commit 19c22e2
Showing 1 changed file with 92 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@
*/
class CheckForMaintenanceMode extends LaravelMaintenanceMode
{

/**
* The language path
*
* @var string
*/
protected $language;

/**
* The prefix for the view variables
*
* @var string
*/
protected $prefix;

/**
* If the information should be injected into all the views
*
* @var bool
*/
protected $inject = false;

/**
* Handle the request
*
Expand All @@ -33,17 +55,17 @@ class CheckForMaintenanceMode extends LaravelMaintenanceMode
public function handle($request, Closure $next)
{
// Grab our configs
$injectGlobally = $this->app['config']->get('maintenancemode.inject.globally', true);
$prefix = $this->app['config']->get('maintenancemode.inject.prefix', 'MaintenanceMode');
$lang = $this->app['config']->get('maintenancemode.language-path', 'maintenancemode::defaults');
$this->inject = $this->app['config']->get('maintenancemode.inject.globally', true);
$this->prefix = $this->app['config']->get('maintenancemode.inject.prefix', 'MaintenanceMode');
$this->language = $this->app['config']->get('maintenancemode.language-path', 'maintenancemode::defaults');

// Setup value array
$info = [
$prefix . 'Enabled' => false,
$prefix . 'Timestamp' => time(),
$prefix . 'Message' => $this->app['translator']->get($lang . '.message'),
$prefix . 'View' => null,
$prefix . 'Retry' => 60,
'Enabled' => false,
'Timestamp' => time(),
'Message' => $this->app['translator']->get($this->language . '.message'),
'View' => null,
'Retry' => 60,
];

// Are we down?
Expand All @@ -52,84 +74,98 @@ public function handle($request, Closure $next)
// Yes. :(
Carbon::setLocale(App::getLocale());

$info[$prefix.'Enabled'] = true;
$info['Enabled'] = true;

$data = json_decode(file_get_contents($this->app->storagePath().'/framework/down'), true);

// Update the array with data from down file
$info[$prefix . 'Timestamp'] = Carbon::createFromTimestamp($data['time']);
$info['Timestamp'] = Carbon::createFromTimestamp($data['time']);

if(isset($data['message']) && $data['message'])
{
$info[$prefix . 'Message'] = $data['message'];
$info['Message'] = $data['message'];
}
if(isset($data['view']) && $data['view'])
{
$info[$prefix . 'View'] = $data['view'];
$info['View'] = $data['view'];
}
if(isset($data['retry']) && intval($data['retry'], 10) !== 0)
{
$info[$prefix . 'Retry'] = intval($data['retry'], 10);
$info['Retry'] = intval($data['retry'], 10);
}

if($injectGlobally)
// Inject the information into the views before the exception
$this->injectIntoViews($info);

if(!$this->isExempt())
{
// Inject the information globally
foreach($info as $key => $value)
{
$this->app['view']->share($key, $value);
}
// The user isn't exempt, so show them the error page
throw new MaintenanceModeException($data['time'], $data['retry'], $data['message'], $data['view']);
}
}
else
{
// Inject the default information into the views
$this->injectIntoViews($info);
}

// Check to see if the user is exempt or not
$isExempt = false;
return $next($request);
}

/**
* Inject the prefixed data into the views
*
* @param $info
* @return null
*/
protected function injectIntoViews($info)
{
if($this->inject)
{
// Inject the information globally (to prevent the need of isset)
foreach($info as $key => $value)
{
$this->app['view']->share($this->prefix . $key, $value);
}
}
}

// Grab all of the exemption classes to create/execute against
$exemptions = $this->app['config']->get('maintenancemode.exemptions', []);
foreach($exemptions as $className)
/**
* Check if a user is exempt from the maintenance mode page
*
* @return bool
* @throws ExemptionDoesNotExist
* @throws InvalidExemption
*/
protected function isExempt()
{
// Grab all of the exemption classes to create/execute against
$exemptions = $this->app['config']->get('maintenancemode.exemptions', []);
foreach($exemptions as $className)
{
if(class_exists($className))
{
if(class_exists($className))
$exemption = new $className($this->app);
if($exemption instanceof MaintenanceModeExemption)
{
$exemption = new $className($this->app);
if($exemption instanceof MaintenanceModeExemption)
// Run the exemption check
if($exemption->isExempt())
{
// Run the exemption check
if($exemption->isExempt())
{
$isExempt = true;
break;
}
}
else
{
// Class doesn't match what we're looking for
throw new InvalidExemption($this->app['translator']->get($lang . '.exceptions.invalid', ['class' => $className]));
return true;
}
}
else
{
// Where's Waldo?
throw new ExemptionDoesNotExist($this->app['translator']->get($lang . '.exceptions.missing', ['class' => $className]));
// Class doesn't match what we're looking for
throw new InvalidExemption($this->app['translator']->get($this->language . '.exceptions.invalid', ['class' => $className]));
}
}

if(!$isExempt)
{
// Since the session isn't started... it'll throw an error
throw new MaintenanceModeException($data['time'], $data['retry'], $data['message'], $data['view']);
}
}
else
{
if($injectGlobally)
else
{
// Inject the information globally (to prevent the need of isset)
foreach($info as $key => $value)
{
$this->app['view']->share($key, $value);
}
// Where's Waldo?
throw new ExemptionDoesNotExist($this->app['translator']->get($this->language . '.exceptions.missing', ['class' => $className]));
}
}
return $next($request);
return false;
}
}

0 comments on commit 19c22e2

Please sign in to comment.