Outils pour utilisateurs

Outils du site


findout:api-dev

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

findout:api-dev [2023/08/09 19:35] – created 85.169.126.26findout:api-dev [2023/10/13 09:58] (Version actuelle) – supprimée admin
Ligne 1: Ligne 1:
-**hierarchie** 
- 
-<code> 
-classes/<classname>/<classname>.php 
-classes/<classname>/traits/<traitname>.php (<traitname> = <classname><traitpart> pour éviter les doublons (à challenger) 
-</code> 
- 
-chaque classe inclut  
-<code> 
-set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__)); 
-</code> 
- 
-**apiPath.php** 
- 
-<file> 
-<?php 
- 
-$sApiPaths = '[ 
-  { "method": "GET", "path": "obj1", "go": "getAll" }, 
-  { "method": "GET", "path": "obj1/$id", "go": "get;1" }, 
-  { "method": "PATCH", "path": "obj2/$id", "go": "patch;1" }, 
-  { "method": "PATCH", "path": "obj1/$id", "go": "patch;1" }, 
-  { "method": "GET", "path": "obj2", "go": "geAll" }, 
-  { "method": "GET", "path": "obj2/$id", "go": "get;1" }, 
-  { "method": "GET", "path": "obj2/$id/details", "go": "getDetails;1" }, 
-  { "method": "GET", "path": "obj2/$id/details/$detid", "go": "getDetail;1,3" } 
-]' ; 
- 
-spl_autoload_register(function($className) { 
-    $fileName = stream_resolve_include_path('classes/' . $className . '/' . $className . '.php'); 
-    if ($fileName !== false) { 
-        include $fileName; 
-    } 
-}); 
-spl_autoload_register(function($traitName) { 
-    $fileName = stream_resolve_include_path('traits/' . $traitName . '.php'); 
-    if ($fileName !== false) { 
-        include $fileName; 
-    } 
-}); 
- 
-function apiPathMatch ( $method, $path ) { 
-        $aPath = explode ( '/', $path ) ; 
-        $iParts = count ( $aPath ) ; 
-        $sFirst = $aPath [ 0 ] ; 
-        $sMethod = $method ; 
- 
-        $aCompare = sortPaths () ; 
-        foreach ( $aCompare as $iKey => $aValue ) { 
-                if ( $method != $aValue [ 'method' ] ) continue ; 
-                if ( $sFirst != $aValue [ 'first' ] ) continue ; 
-                if ( $iParts < $aValue [ 'count' ] ) continue ; 
-                if ( $iParts > $aValue [ 'count' ] ) continue ; 
- 
-                if ( $iParts == 1 ) { 
-                        doThings ( $aValue [ 'first' ], $aValue [ 'go' ], $path ) ; 
-                        return ; 
-                } 
- 
-                $iCount = 0 ; 
-                foreach ( explode ( '/', $aValue [ 'path' ] ) as $iKey2 => $sPath ) { 
-                        $iCount++ ; 
-                        if ( preg_match ( '/\$.*/', $sPath ) ) continue ; 
-                        if ( $sPath != $aPath [ $iKey2 ] ) break ; 
-                } 
-                if ( $iCount == $iParts ) { 
-                        doThings ( $aValue [ 'first' ], $aValue [ 'go' ], $path ) ; 
-                        return ; 
-                } 
-        } 
-        echo "No match" ; 
-} 
- 
-function sortPaths () { 
-        global $sApiPaths ; 
- 
-        $aPaths = json_decode ( $sApiPaths, true ) ; 
- 
-        foreach ( $aPaths as $iKey => $aPath ) { 
-                $sLocalPath = $aPath ['path'] ; 
-                $sLocalGo = $aPath ['go'] ; 
-                $aLocalPath = explode ('/', $sLocalPath) ; 
-                $iLocalPart = count ( $aLocalPath ) ; 
-                $sLocalFirst = $aLocalPath [0] ; 
-                $sLocalMethod = $aPath [ 'method' ] ; 
- 
-                $aLocalResult [ $iKey ] [ 'count' ] = $iLocalPart ; 
-                $aLocalResult [ $iKey ] [ 'first' ] = $sLocalFirst ; 
-                $aLocalResult [ $iKey ] [ 'method' ] = $sLocalMethod ; 
-                $aLocalResult [ $iKey ] [ 'path' ] = $sLocalPath ; 
-                $aLocalResult [ $iKey ] [ 'go' ] = $sLocalGo ; 
- 
-                $aSort1 [ $iKey ] = $sLocalFirst ; 
-                $aSort2 [ $iKey ] = $iLocalPart ; 
-        } 
- 
-        array_multisort($aSort1, SORT_ASC, $aSort2, SORT_ASC, $aLocalResult ); 
- 
-        return $aLocalResult ; 
-} 
- 
-function doThings( $class, $method, $path) 
-{ 
-        $o = new $class () ; 
-        $a = explode ( ';', $method ) ; 
-        $m = $a [ 0 ] ; 
-        $p = explode ( ',', $a [ 1 ] ) ; 
-        $pa = explode ( '/', $path ) ; 
-        $pl = [] ; 
-        foreach ( $p as $k => $pn ) { 
-                echo "avec le param : " . $pa [ $pn ] . "\n" ; 
-                $pl [] = $pa [ $pn ] ; 
-        } 
- 
-        $o -> {$m} ( $pl ) ; 
-} 
- 
-apiPathMatch ( 'GET', 'obj2/123/details/ABC' ) ; 
- 
-?> 
-</file> 
- 
-**Classe : obj2** 
-* classes/obj2.php * 
- 
-<file> 
-<?php 
- 
-set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__)); 
- 
-class obj1 { 
-        use obj1Get ; 
-        use obj1GetAll ; 
-        use obj1Patch ; 
- 
-        private $text; 
- 
-        function __construct () { 
-                $this -> text = 'Je suis OBJ1' ; 
-        } 
- 
-        public function getText () { 
-                return ( $this -> text ) ; 
-        } 
-} 
- 
-</file> 
- 
-**Trait : obj2GetDetail** 
-* classes/obj2/traits/obj2GetDetail.php * 
-* paramètre : tableau de valeurs * 
- 
-<file> 
-<?php 
- 
-trait obj2GetDetail { 
-        function getDetail ( $p ) { 
-                echo $this -> text () . "\n" ; 
-                echo "- dans getDetails" . "\n" ; 
-                echo "- params : " . $p [0] . ", " . $p [1] . "\n" ; 
-        } 
-} 
- 
-</file> 
- 
  
findout/api-dev.1691602522.txt.gz · Dernière modification : 2023/08/09 19:35 de 85.169.126.26

DokuWiki Appliance - Powered by TurnKey Linux