<?php

/*
------------------------------------------------------------
EasyConf Enterprise Scanner
Pack 2599-B
------------------------------------------------------------
*/

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/class_inventory.csv';

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

fputcsv($fp,[

'TYPE',

'CLASS',

'NAMESPACE',

'EXTENDS',

'FILE'

]);

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='';

        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($class!=''){

            $type='Other';

            if(
                str_contains(
                    $file->getPathname(),
                    '/controllers/'
                )
            ){

                $type='Controller';

            }
            elseif(
                str_contains(
                    $file->getPathname(),
                    '/services/'
                )
            ){

                $type='Service';

            }
            elseif(
                str_contains(
                    $file->getPathname(),
                    '/core/'
                )
            ){

                $type='Core';

            }
            elseif(
                str_contains(
                    $file->getPathname(),
                    '/helpers/'
                )
            ){

                $type='Helper';

            }

            fputcsv($fp,[

                $type,

                $class,

                $namespace,

                $extends,

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

            ]);

        }

    }

}

fclose($fp);

echo PHP_EOL;
echo "Inventory written to:\n";
echo $out;
echo PHP_EOL;
