<?php

/*
------------------------------------------------------------
EasyConf Enterprise Architecture Scanner
Pack 2599-C
------------------------------------------------------------
*/

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

$folders = [

    BASE_PATH.'/app/controllers',

    BASE_PATH.'/app/services',

    BASE_PATH.'/app/core',

    BASE_PATH.'/app/helpers'

];

$out =
BASE_PATH.
'/bak/pack2599/architecture_inventory.csv';

$fp=fopen($out,'w');

fputcsv($fp,[

'TYPE',
'CATEGORY',
'CLASS',
'NAMESPACE',
'EXTENDS',
'PUBLIC_METHODS',
'FILE'

]);

function category(string $class,string $file):string
{
    $text=strtolower($class.' '.$file);

    $map=[

        'Workflow'=>[
            'workflow',
            'stage',
            'transition'
        ],

        'Reviewer'=>[
            'review',
            'reviewer'
        ],

        'Author'=>[
            'author',
            'submission'
        ],

        'Production'=>[
            'production',
            'galley',
            'camera'
        ],

        'Issue'=>[
            'issue',
            'proceeding',
            'publication'
        ],

        'Governance'=>[
            'governance',
            'integrity',
            'health'
        ],

        'Enterprise'=>[
            'enterprise',
            'registry',
            'synchronizer'
        ],

        'RBAC'=>[
            'rbac',
            'permission',
            'role'
        ],

        'Dashboard'=>[
            'dashboard'
        ],

        'Notification'=>[
            'notification',
            'mail',
            'email'
        ]

    ];

    foreach($map as $cat=>$words){

        foreach($words as $w){

            if(str_contains($text,$w)){

                return $cat;

            }

        }

    }

    return 'General';
}

foreach($folders as $folder){

    $it=new RecursiveIteratorIterator(

        new RecursiveDirectoryIterator(

            $folder,

            FilesystemIterator::SKIP_DOTS

        )

    );

    foreach($it as $file){

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

            continue;

        }

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

        $tokens=token_get_all($src);

        $namespace='';

        $class='';

        $extends='';

        $methods=[];

        for($i=0;$i<count($tokens);$i++){

            if(
                is_array($tokens[$i]) &&
                $tokens[$i][0]==T_NAMESPACE
            ){

                $namespace='';

                for($j=$i+1;$j<count($tokens);$j++){

                    if($tokens[$j]==';'){

                        break;

                    }

                    if(is_array($tokens[$j])){

                        $namespace.=$tokens[$j][1];

                    }

                }

            }

            if(
                is_array($tokens[$i]) &&
                $tokens[$i][0]==T_CLASS
            ){

                for($j=$i+1;$j<count($tokens);$j++){

                    if(
                        is_array($tokens[$j]) &&
                        $tokens[$j][0]==T_STRING
                    ){

                        $class=$tokens[$j][1];

                        break;

                    }

                }

            }

            if(
                is_array($tokens[$i]) &&
                $tokens[$i][0]==T_EXTENDS
            ){

                for($j=$i+1;$j<count($tokens);$j++){

                    if(
                        is_array($tokens[$j]) &&
                        $tokens[$j][0]==T_STRING
                    ){

                        $extends=$tokens[$j][1];

                        break;

                    }

                }

            }

            if(
                is_array($tokens[$i]) &&
                $tokens[$i][0]==T_PUBLIC
            ){

                if(
                    isset($tokens[$i+2]) &&
                    is_array($tokens[$i+2]) &&
                    $tokens[$i+2][0]==T_STRING
                ){

                    $methods[]=$tokens[$i+2][1];

                }

            }

        }

        if($class==''){

            continue;

        }

        $type='Other';

        $path=$file->getPathname();

        if(str_contains($path,'/controllers/')){

            $type='Controller';

        }
        elseif(str_contains($path,'/services/')){

            $type='Service';

        }
        elseif(str_contains($path,'/core/')){

            $type='Core';

        }
        elseif(str_contains($path,'/helpers/')){

            $type='Helper';

        }

        fputcsv($fp,[

            $type,

            category($class,$path),

            $class,

            trim($namespace),

            $extends,

            implode('|',$methods),

            str_replace(
                BASE_PATH.'/',
                '',
                $path
            )

        ]);

    }

}

fclose($fp);

echo PHP_EOL;
echo "Architecture inventory generated.";
echo PHP_EOL;
echo $out;
echo PHP_EOL;

