rest-proxy.php

Simple proxy for RESTful APIs
git clone git://git.finwo.net/app/rest-proxy.php
Log | Files | Refs | README

Route.php (6131B)


      1 <?php
      2 
      3 namespace Finwo\Framework;
      4 
      5 use Finwo\Datatools\Mappable;
      6 
      7 class Route extends Mappable
      8 {
      9     // Custom/matching values
     10     protected $type = 'default';
     11     protected $name;
     12     protected $host;
     13     protected $pattern;
     14     protected $method;
     15     protected $controller;
     16     protected $defaults;
     17     protected $prefix;
     18     protected $parsedUri;
     19 
     20     // After-parse values
     21     protected $parameters;
     22 
     23     // Request info
     24     protected $requestUri = '';
     25     protected $requestMethod = '';
     26     protected $httpHost = '';
     27 
     28     public function __construct($name, $config)
     29     {
     30         $this->name = $name;
     31         parent::__construct($config);
     32     }
     33 
     34     public function match()
     35     {
     36         // If this function gets called, process the request.. No sooner
     37 
     38         // Handle pattern prefix
     39         if (strlen($this->prefix)) {
     40             if (substr($this->requestUri, 0, strlen($this->prefix)) == $this->prefix) {
     41                 $this->requestUri = substr($this->requestUri, strlen($this->prefix));
     42             } else {
     43                 return false;
     44             }
     45         }
     46 
     47         // Host match
     48         if (!is_null($this->host) && !preg_match('/' . $this->host . '/i', $this->httpHost)) {
     49             return false;
     50         }
     51 
     52         // Method match
     53         if (!is_null($this->method) && !preg_match('/' . $this->method . '/i', $this->requestMethod)) {
     54             return false;
     55         }
     56 
     57         // Parse the pattern
     58         $parsed = array_merge(array(
     59             'path' => '/',
     60             'query' => ''
     61         ), parse_url($this->requestUri));
     62 
     63         // Store parsed uri
     64         $this->parsedUri = $parsed;
     65         parse_str($this->parsedUri['query'], $query);
     66         $this->parsedUri['query'] = $query;
     67 
     68         if (!is_null($this->pattern) && !preg_match('/' . $this->pattern . '/i', $parsed['path'], $this->parameters)) {
     69             return false;
     70         }
     71 
     72         // Strip parameters of numeric keys
     73         foreach ($this->parameters as $key => $parameter) {
     74             if (is_int($key)) {
     75                 unset($this->parameters[$key]);
     76             }
     77         }
     78 
     79         // Parse the query
     80         parse_str($parsed['query'], $matches);
     81         $this->parameters = array_merge((array)$this->defaults, $this->parameters, $matches);
     82 
     83         return true;
     84     }
     85 
     86     /**
     87      * @return string
     88      */
     89     public function getType()
     90     {
     91         return $this->type;
     92     }
     93 
     94     /**
     95      * @param string $type
     96      * @return Route
     97      */
     98     public function setType($type)
     99     {
    100         $this->type = $type;
    101         return $this;
    102     }
    103 
    104     /**
    105      * @return null
    106      */
    107     public function getName()
    108     {
    109         return $this->name;
    110     }
    111 
    112     /**
    113      * @param null $name
    114      * @return Route
    115      */
    116     public function setName($name)
    117     {
    118         $this->name = $name;
    119         return $this;
    120     }
    121 
    122     /**
    123      * @return mixed
    124      */
    125     public function getHost()
    126     {
    127         return $this->host;
    128     }
    129 
    130     /**
    131      * @param mixed $host
    132      * @return Route
    133      */
    134     public function setHost($host)
    135     {
    136         $this->host = $host;
    137         return $this;
    138     }
    139 
    140     /**
    141      * @return mixed
    142      */
    143     public function getPattern()
    144     {
    145         return $this->pattern;
    146     }
    147 
    148     /**
    149      * @param mixed $pattern
    150      * @return Route
    151      */
    152     public function setPattern($pattern)
    153     {
    154         $this->pattern = $pattern;
    155         return $this;
    156     }
    157 
    158     /**
    159      * @return mixed
    160      */
    161     public function getMethod()
    162     {
    163         return $this->method;
    164     }
    165 
    166     /**
    167      * @param mixed $method
    168      * @return Route
    169      */
    170     public function setMethod($method)
    171     {
    172         $this->method = $method;
    173         return $this;
    174     }
    175 
    176     /**
    177      * @return mixed
    178      */
    179     public function getController()
    180     {
    181         return $this->controller;
    182     }
    183 
    184     /**
    185      * @param mixed $controller
    186      * @return Route
    187      */
    188     public function setController($controller)
    189     {
    190         $this->controller = $controller;
    191         return $this;
    192     }
    193 
    194     /**
    195      * @return mixed
    196      */
    197     public function getDefaults()
    198     {
    199         return $this->defaults;
    200     }
    201 
    202     /**
    203      * @param mixed $defaults
    204      * @return Route
    205      */
    206     public function setDefaults($defaults)
    207     {
    208         $this->defaults = $defaults;
    209         return $this;
    210     }
    211 
    212     /**
    213      * @return mixed
    214      */
    215     public function getPrefix()
    216     {
    217         return $this->prefix;
    218     }
    219 
    220     /**
    221      * @param mixed $prefix
    222      * @return Route
    223      */
    224     public function setPrefix($prefix)
    225     {
    226         $this->prefix = $prefix;
    227         return $this;
    228     }
    229 
    230     /**
    231      * @return mixed
    232      */
    233     public function getParsedUri()
    234     {
    235         return $this->parsedUri;
    236     }
    237 
    238     /**
    239      * @param mixed $parsedUri
    240      * @return Route
    241      */
    242     public function setParsedUri($parsedUri)
    243     {
    244         $this->parsedUri = $parsedUri;
    245         return $this;
    246     }
    247 
    248     /**
    249      * @return mixed
    250      */
    251     public function getParameters()
    252     {
    253         return $this->parameters;
    254     }
    255 
    256     /**
    257      * @param mixed $parameters
    258      * @return Route
    259      */
    260     public function setParameters($parameters)
    261     {
    262         $this->parameters = $parameters;
    263         return $this;
    264     }
    265 
    266     /**
    267      * @return string
    268      */
    269     public function getRequestUri()
    270     {
    271         return $this->requestUri;
    272     }
    273 
    274     /**
    275      * @param string $requestUri
    276      * @return Route
    277      */
    278     public function setRequestUri($requestUri)
    279     {
    280         $this->requestUri = $requestUri;
    281         return $this;
    282     }
    283 
    284     /**
    285      * @return string
    286      */
    287     public function getRequestMethod()
    288     {
    289         return $this->requestMethod;
    290     }
    291 
    292     /**
    293      * @param string $requestMethod
    294      * @return Route
    295      */
    296     public function setRequestMethod($requestMethod)
    297     {
    298         $this->requestMethod = $requestMethod;
    299         return $this;
    300     }
    301 
    302     /**
    303      * @return string
    304      */
    305     public function getHttpHost()
    306     {
    307         return $this->httpHost;
    308     }
    309 
    310     /**
    311      * @param string $httpHost
    312      * @return Route
    313      */
    314     public function setHttpHost($httpHost)
    315     {
    316         $this->httpHost = $httpHost;
    317         return $this;
    318     }
    319 }