public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null)
    {
        if ('\\' !== substr($namespace, -1)) {
            throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": %s.', $namespace));
        }
        if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
            throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: %s.', $namespace));
        }

        $classes = $this->findClasses($namespace, $resource, (array) $exclude);
        // prepare for deep cloning
        $serializedPrototype = serialize($prototype);
        $interfaces = [];
        $singlyImplemented = [];

        foreach ($classes as $class => $errorMessage) {
            if (interface_exists($class, false)) {
                $interfaces[] = $class;
            } else {
                $this->setDefinition($class, $definition = unserialize($serializedPrototype));
                if (null !== $errorMessage) {
                    $definition->addError($errorMessage);

                    continue;
                }
                foreach (class_implements($class, false) as $interface) {
                    $singlyImplemented[$interface] = isset($singlyImplemented[$interface]) ? false : $class;
                }
            }
        }
        foreach ($interfaces as $interface) {
            if (!empty($singlyImplemented[$interface])) {
                $this->container->setAlias($interface, $singlyImplemented[$interface])
                    ->setPublic(false);
            }
        }
    }