<?php

/*
--------------------------------------------------------
EasyConf Enterprise Knowledge Builder
Pack 2599-D
--------------------------------------------------------
*/

define(
    'BASE_PATH',
    dirname(__DIR__,3)
);

$folders = [

    'controllers' => BASE_PATH.'/app/controllers',

    'services'    => BASE_PATH.'/app/services',

    'core'        => BASE_PATH.'/app/core'

];

$out =
BASE_PATH .
'/bak/pack2599/knowledge/knowledge.json';

$result=[];

foreach($folders as $type=>$folder){

    $it=new RecursiveIteratorIterator(

        new RecursiveDirectoryIterator(

            $folder,

            FilesystemIterator::SKIP_DOTS

        )

    );

    foreach($it as $file){

        if($file->getExtension()!='php'){

            continue;

        }

        $code=file_get_contents(
            $file->getPathname()
        );

        $namespace='';

        if(
            preg_match(
                '/namespace\s+([^;]+);/',
                $code,
                $m
            )
        ){

            $namespace=trim($m[1]);

        }

        $class='';

        if(
            preg_match(
                '/class\s+([A-Za-z0-9_]+)/',
                $code,
                $m
            )
        ){

            $class=$m[1];

        }

        if($class==''){

            continue;

        }

        $extends='';

        if(
            preg_match(
                '/extends\s+([A-Za-z0-9_\\\\]+)/',
                $code,
                $m
            )
        ){

            $extends=$m[1];

        }

        preg_match_all(

            '/public\s+function\s+([A-Za-z0-9_]+)/',

            $code,

            $methods

        );

        $result[]=[

            'type'=>$type,

            'namespace'=>$namespace,

            'class'=>$class,

            'extends'=>$extends,

            'methods'=>$methods[1],

            'file'=>str_replace(
                BASE_PATH.'/',
                '',
                $file->getPathname()
            )

        ];

    }

}

file_put_contents(

    $out,

    json_encode(

        $result,

        JSON_PRETTY_PRINT|
        JSON_UNESCAPED_SLASHES

    )

);

echo PHP_EOL;
echo "Knowledge Repository created";
echo PHP_EOL;
echo $out;
echo PHP_EOL;
