Outils pour utilisateurs

Outils du site


findout:api

Ceci est une ancienne révision du document !


hierarchie

classes/<classname>/<classname>.php
classes/<classname>/traits/<traitname>.php (<traitname> = <classname><traitpart> pour éviter les doublons (à challenger)

chaque classe inclut

set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__));

apiPath.php

<?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' ) ;

?>

Classe : obj2 * classes/obj2.php *

<?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 ) ;
        }
}

Trait : obj2GetDetail * classes/obj2/traits/obj2GetDetail.php * * paramètre : tableau de valeurs *

<?php

trait obj2GetDetail {
        function getDetail ( $p ) {
                echo $this -> text () . "\n" ;
                echo "- dans getDetails" . "\n" ;
                echo "- params : " . $p [0] . ", " . $p [1] . "\n" ;
        }
}
findout/api.1691600597.txt.gz · Dernière modification : 2023/08/09 19:03 de 85.169.126.26 · Actuellement bloqué par : 192.168.0.100,216.73.216.57

DokuWiki Appliance - Powered by TurnKey Linux