Request.class.php 3.15 KB
Newer Older
Zohten's avatar
Zohten committed
1
<?php
Zohten's avatar
Zohten committed
2 3
class Request
{
Zohten's avatar
Zohten committed
4 5 6 7 8
    protected $controllerName;
    protected $uriParameters;
    protected $data;
    protected static $_instance;

Zohten's avatar
Zohten committed
9 10 11 12 13
    public static function getCurrentRequest()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new Request();
        }
Zohten's avatar
Zohten committed
14
      
Zohten's avatar
Zohten committed
15 16 17 18 19 20 21 22
        return self::$_instance;
    }

    public function __construct()
    {
        $this->initBaseURI();
        $this->initControllerAndParametersFromURI();
        $this->initData();
Zohten's avatar
Zohten committed
23 24
    }

Zohten's avatar
Zohten committed
25 26 27 28 29 30 31
    // intialise baseURI
    // e.g. http://eden.imt-lille-douai.fr/~luc.fabresse/api.php => __BASE_URI = /~luc.fabresse
    // e.g. http://localhost/CDAW/api.php => __BASE_URI = /CDAW
    protected function initBaseURI()
    {
        $this->baseURI = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
    }
Zohten's avatar
Zohten committed
32

Zohten's avatar
Zohten committed
33 34 35 36 37 38 39 40 41 42 43
    // intialise controllerName et uriParameters
    // controllerName contient chaîne 'default' ou le nom du controleur s'il est présent dans l'URI (la requête)
    // uriParameters contient un tableau vide ou un tableau contenant les paramètres passés dans l'URI (la requête)
    // e.g. http://eden.imt-lille-douai.fr/~luc.fabresse/api.php
    //    => controllerName == 'default'
    //       uriParameters == []
    // e.g. http://eden.imt-lille-douai.fr/~luc.fabresse/api.php/user/1
    //    => controllerName == 'user'
    //       uriParameters == [ 1 ]
    protected function initControllerAndParametersFromURI()
    {
Zohten's avatar
Zohten committed
44 45 46 47
        $prefix = $_SERVER['SCRIPT_NAME'];
        $uriParameters = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

        $i=0;
Zohten's avatar
Zohten committed
48 49 50 51 52
        while ($i<strlen($prefix) && $i<strlen($uriParameters)) {
            if ($prefix[$i]===$uriParameters[$i]) {
                $i++;
            }
        }
Zohten's avatar
Zohten committed
53 54 55 56 57 58 59 60 61 62

        $uriParameters = substr($uriParameters, $i);

        $uriParameters = trim($uriParameters, '/');
        $uriSegments = explode('/', $uriParameters);

        $this->controllerName = array_shift($uriSegments) ?: "default";
        $this->uriParameters = $uriSegments;
    }

Zohten's avatar
Zohten committed
63 64 65
    // ==============
    // Public API
    // ==============
Zohten's avatar
Zohten committed
66 67

    // retourne le name du controleur qui doit traiter la requête courante
Zohten's avatar
Zohten committed
68 69 70 71
    public function getControllerName()
    {
        return $this->controllerName;
    }
Zohten's avatar
Zohten committed
72

Zohten's avatar
Zohten committed
73 74 75 76
    public function getUriParams()
    {
        return $this->uriParameters;
    }
Zohten's avatar
Zohten committed
77

Zohten's avatar
Zohten committed
78 79 80 81 82 83 84 85
    public function initData()
    {
        if ($this->getHttpMethod() === 'PUT' || $this->getHttpMethod() === 'POST') {
            $jsondata=file_get_contents("php://input");
            $this->data = json_decode($jsondata, true);
        }
    }
   
Zohten's avatar
Zohten committed
86

Zohten's avatar
Zohten committed
87 88 89 90 91
    // retourne la méthode HTTP utilisée dans la requête courante
    public function getHttpMethod()
    {
        return $_SERVER["REQUEST_METHOD"];
    }
Zohten's avatar
Zohten committed
92

Zohten's avatar
Zohten committed
93 94 95 96 97 98 99 100 101 102
    public function getData()
    {
        return $this->data;
    }
    // returns JWT token in Authorization header or throw an exception
    public function getJwtToken()
    {
        $headers = getallheaders();
        $autorization = $headers['Authorization'];
        $arr = explode(" ", $autorization);
Zohten's avatar
Zohten committed
103

Zohten's avatar
Zohten committed
104 105 106
        if (count($arr)<2) {
            throw new Exception("Missing JWT token");
        }
Zohten's avatar
Zohten committed
107

Zohten's avatar
Zohten committed
108
        $jwt_token = $arr[1];
Zohten's avatar
Zohten committed
109

Zohten's avatar
Zohten committed
110 111 112
        return $jwt_token;
    }
}