Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DatabaseCheck
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 call
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 check
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * USAGE
4 *
5 * $app = new \Slim\Slim();
6 * $app->add(new \Slim\Extras\Middleware\DatabaseCheck());
7 *
8 */
9namespace Slim\Extras\Middleware;
10
11class DatabaseCheck extends \Slim\Middleware
12{
13
14    /**
15     * Call middleware.
16     *
17     * @return void
18     */
19    public function call() 
20    {
21        // Attach as hook.
22        $this->app->hook('slim.before', array($this, 'check'));
23            
24        // Call next middleware.
25        $this->next->call();
26    }
27
28    public function check() {
29        error_log($this->app->request->getPath());
30        if ($this->app->request->getPath() != $this->app->urlFor('uri_install')){
31            // Test database connection
32            try {
33                \UserFrosting\Database::connection();
34            } catch (\PDOException $e){
35                $this->app->redirect($this->app->urlFor('uri_install'));
36            }
37        }
38    }
39}