openrat-webdav

git clone http://git.code.weiherhei.de/openrat-webdav.git
Log | Files | Refs

URIParser.class.php (2640B)


      1 <?php
      2 
      3 
      4 use dav\exception\NotFoundException;
      5 
      6 class URIParser
      7 {
      8     const ROOT = 'root';
      9 
     10     public $type;
     11     public $projectid;
     12     public $objectid;
     13 
     14     private $uri;
     15     /**
     16      * @var CMS
     17      */
     18     private $client;
     19 
     20     public function __construct( $client, $uri )
     21     {
     22         $this->uri = $uri;
     23         $this->client = $client;
     24         $this->parseURI();
     25     }
     26 
     27     /**
     28      * URI parsen.
     29      */
     30     private function parseURI()
     31     {
     32         $uri = $this->uri;
     33 
     34         Logger::trace('WEBDAV: Parsen der URI ' . $uri);
     35 
     36         $uriParts = explode('/', $uri);
     37 
     38         $first = array_shift($uriParts);
     39         if ($first) {
     40             throw new RuntimeException( 'URI does not begin with \'/\'.' );
     41         }
     42 
     43         $projectName = array_shift($uriParts);
     44 
     45         if ( ! $projectName ) {
     46             $this->type = self::ROOT;  // Root-Verzeichnis
     47             return;
     48         }
     49 
     50         try {
     51 
     52             $result = $this->client->projectlist();
     53         }
     54         catch( Exception $e) {
     55             Logger::error("Failed to read projects: \n".$this->client->__toString()."\n".$e->getMessage() );
     56             throw $e;
     57         }
     58         //Logger::trace( print_r( $result,true) );
     59 
     60         $projects = $result['projects'];
     61 
     62         foreach( $projects as $id=>$projectinfo)
     63             if   ( $projectinfo['name'] == $projectName)
     64                 $this->projectid = $id;
     65 
     66         if ( ! $this->projectid ) {
     67             throw new RuntimeException( 'Project \''.$projectName.'\' not found.' );
     68         }
     69 
     70         $project = $this->client->project($this->projectid);
     71         Logger::trace( print_r( $project,true) );
     72 
     73 
     74         $objectid = $project['rootobjectid'];
     75         $type = 'folder';
     76 
     77         while (sizeof($uriParts) > 0) {
     78             $name = array_shift($uriParts);
     79 
     80             if   ( !$name )
     81                 continue; // empty path segments
     82 
     83             $folder = $this->client->folder($objectid);
     84 
     85             $found = false;
     86             foreach ($folder['content']['object'] as $oid => $object) {
     87                 if ($object['filename'] == $name) {
     88                     $found = true;
     89 
     90                     $type     = $object['type'];
     91                     $objectid = $object['id'  ];
     92 
     93                     break;
     94                 }
     95 
     96             }
     97 
     98             if (!$found) {
     99                 throw new NotFoundException('Not found path segment: '.$name );
    100             }
    101         }
    102 
    103         $this->type = $type;
    104         $this->objectid   = $objectid;
    105     }
    106 
    107 
    108     public function __toString()
    109     {
    110         return "DAV-Object: $this->uri ==> [$this->type] projectid: $this->projectid / objectid: $this->objectid";
    111     }
    112 }