protected function hydrateRegistry()
    {
        if ($this->ready) {
            return;
        }

        $modelClasses = $this->getModelClasses();

        $callback = function (\ReflectionClass $reflect) use (
            &$callback,
            &$classList
        ) {
            $parentClass = $reflect->getParentClass()->getName();
            //            $classList[] = $reflect->getName();
            if (Model::class === $parentClass) {
                $extends = [];
            } else {
                if ($reflect->getParentClass()->isAbstract()) {
                    $extends = $callback($reflect->getParentClass());
                } else {
                    $extends = [$parentClass];
                }
            }
            $classList = array_merge($classList, $extends);

            return $extends;
        };

        /* @var $obj Model */

        if (!empty($modelClasses)) {
            $classPopulationOrder = [];
            $classToAppMap = [];
            $classList = [];

            foreach ($modelClasses as $appName => $classes) {
                foreach ($classes as $class) {
                    $classToAppMap[$class] = $appName;
                    $reflect = new \ReflectionClass($class);

                    // if we cannot create an instance of a class just skip,
                    // e.g traits abstract etc

                    if (!$reflect->isInstantiable()) {
                        continue;
                    }

                    if ($this->hasModel($class) ||
                        !$reflect->isSubclassOf(Model::class)) {
                        continue;
                    }

                    // callback to get non-abstract parent, since this needs to
                    // created before we can create this child class instance
                    // if none is found return empty array
                    $classList[] = $reflect->getName();
                    $classPopulationOrder[$class] = $callback($reflect);
                }
            }
            $classList = array_unique($classList);

            foreach ($classList as $class) {
                if (!ArrayHelper::hasKey($classToAppMap, $class)) {
                    throw new OrmException(
                        "Make '$class' abstract or register it as ".
                        'an application model'
                    );
                }
            }

            try {
                $classPopulationOrder = Tools::topologicalSort($classPopulationOrder);
            } catch (CircularDependencyError $e) {
                throw new OrmException($e->getMessage());
            }

            foreach ($classPopulationOrder as $class) {
                $obj = new $class(['registry' => $this]);

                $obj->setupClassInfo(
                    null,
                    ['meta' => ['appName' => $classToAppMap[$class]], 'registry' => $this]
                );
            }
        }
    }