<?php

class NodeTypeHelper
{

    /** @var INodeType[] */
    private static $_nodeTypes = null;

    /**
     * @return INodeType[]
     */
    public static function getNodeTypes()
    {
        if (self::$_nodeTypes === null) {
            $typesPath = Yii::getPathOfAlias('admin.types');
            $typeDirs = scandir($typesPath);
            /** @var $types INodeType[] */
            $types = array();
            foreach ($typeDirs as $dir) {
                if ($dir != '.' && $dir != '..' && is_dir($typesPath . '/' . $dir)) {
                    $typeName = $dir;
                    $className = ucfirst($typeName) . 'Type';
                    Yii::import('admin.types.' . $typeName . '.' . $className);
                    $types[$typeName] = new $className;
                }
            }
            self::$_nodeTypes = $types;
        }
        return self::$_nodeTypes;
    }

    public static function getNodeType($name){
        $types =  self::getNodeTypes();
        return $types[$name];
    }
}
