Skip to content
Snippets Groups Projects
Select Git revision
  • 9babcfb9e22cb09f0d0af759b6491c45ff2f2278
  • master default protected
2 results

base.php

Blame
  • router.php 4.78 KiB
    <?php
    /**
     * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
     * This file is licensed under the Affero General Public License version 3 or
     * later.
     * See the COPYING-README file.
     */
    
    use Symfony\Component\Routing\Matcher\UrlMatcher;
    use Symfony\Component\Routing\Generator\UrlGenerator;
    use Symfony\Component\Routing\RequestContext;
    use Symfony\Component\Routing\RouteCollection;
    //use Symfony\Component\Routing\Route;
    
    class OC_Router {
    	protected $collections = array();
    	protected $collection = null;
    	protected $root = null;
    
    	protected $generator = null;
    	protected $routing_files;
    	protected $cache_key;
    
    	public function __construct() {
    		$baseUrl = OC_Helper::linkTo('', 'index.php');
    		if ( !OC::$CLI) {
    			$method = $_SERVER['REQUEST_METHOD'];
    		}else{
    			$method = 'GET';
    		}
    		$host = OC_Request::serverHost();
    		$schema = OC_Request::serverProtocol();
    		$this->context = new RequestContext($baseUrl, $method, $host, $schema);
    		// TODO cache
    		$this->root = $this->getCollection('root');
    	}
    
    	public function getRoutingFiles() {
    		if (!isset($this->routing_files)) {
    			$this->routing_files = array();
    			foreach(OC_APP::getEnabledApps() as $app) {
    				$file = OC_App::getAppPath($app).'/appinfo/routes.php';
    				if(file_exists($file)) {
    					$this->routing_files[$app] = $file;
    				}
    			}
    		}
    		return $this->routing_files;
    	}
    
    	public function getCacheKey() {
    		if (!isset($this->cache_key)) {
    			$files = $this->getRoutingFiles();
    			$files[] = 'settings/routes.php';
    			$files[] = 'core/routes.php';
    			$files[] = 'ocs/routes.php';
    			$this->cache_key = OC_Cache::generateCacheKeyFromFiles($files);
    		}
    		return $this->cache_key;
    	}
    
    	/**
    	 * loads the api routes
    	 */
    	public function loadRoutes() {
    		foreach($this->getRoutingFiles() as $app => $file) {
    			$this->useCollection($app);
    			require_once $file;
    			$collection = $this->getCollection($app);
    			$this->root->addCollection($collection, '/apps/'.$app);