<commit_before><?php

namespace Becklyn\AssetsBundle\Entry;

use Becklyn\AssetsBundle\Asset\NamespacedAsset;
use Becklyn\AssetsBundle\Exception\AssetsException;


/**
 * Contains all namespaces for the application
 */
class EntryNamespaces implements \IteratorAggregate
{
    /**
     * @var array<string,string>
     */
    private $namespaces = [];


    /**
     * @var string
     */
    private $projectDir;

    /**
     * @param string $projectDir
     * @param array  $entries
     */
    public function __construct (string $projectDir, array $entries)
    {
        $this->projectDir = $projectDir;

        foreach ($entries as $namespace => $directory)
        {
            $this->addNamespace($namespace, $directory);
        }
    }


    /**
     * Adds a namespace
     *
     * @param string $namespace
     * @param string $directory
     * @throws AssetsException
     */
    public function addNamespace (string $namespace, string $directory) : void
    {
        $dir = "{$this->projectDir}/" . trim($directory, "/");

        if (isset($this->namespaces[$namespace]))
        {
            throw new AssetsException(sprintf(
                "Duplicate registration of namespace '%s'.",
                $namespace
            ));
        }

        if (is_dir($dir))
        {
            $this->namespaces[$namespace] = $dir;
        }
    }


    /**
     * @return \ArrayIterator|\Traversable
     */
    public function getIterator ()
    {
        return new \ArrayIterator($this->namespaces);
    }


    /**
     * Returns the path to the namespace
     *
     * @param string $namespace
     * @return string
     * @throws AssetsException
     */
    public function getPath (string $namespace) : string
    {
        if (!isset($this->namespaces[$namespace]))
        {
            throw new AssetsException(sprintf(
                "Unknown entry namespace: '%s'",
                $namespace
            ));
        }

        return $this->namespaces[$namespace];
    }


    /**
     * Returns the full file path for the given namespaced asset
     *
     * @param NamespacedAsset $asset
     * @return string
     * @throws AssetsException
     */
    public function getFilePath (NamespacedAsset $asset) : string
    {
        return "{$this->getPath($asset->getNamespace())}/{$asset->getPath()}";
    }
}<commit_msg>Add system namespaces, that were not added by the user<commit_after><?php

namespace Becklyn\AssetsBundle\Entry;

use Becklyn\AssetsBundle\Asset\NamespacedAsset;
use Becklyn\AssetsBundle\Exception\AssetsException;


/**
 * Contains all namespaces for the application
 */
class EntryNamespaces implements \IteratorAggregate
{
    /**
     * @var array<string,string>
     */
    private $namespaces = [];


    /**
     * @var array<string,string>
     */
    private $systemNamespaces = [];


    /**
     * @var string
     */
    private $projectDir;

    /**
     * @param string $projectDir
     * @param array  $entries
     */
    public function __construct (string $projectDir, array $entries)
    {
        $this->projectDir = $projectDir;

        foreach ($entries as $namespace => $directory)
        {
            $this->addNamespace($namespace, $directory);
        }
    }


    /**
     * Adds a namespace
     *
     * @param string $namespace
     * @param string $directory
     * @throws AssetsException
     */
    public function addNamespace (string $namespace, string $directory) : void
    {
        $dir = $this->prepareDirectory($namespace, $directory);

        if (null !== $dir)
        {
            $this->namespaces[$namespace] = $dir;
        }
    }


    /**
     * Adds a namespace
     *
     * @param string $namespace
     * @param string $directory
     * @throws AssetsException
     */
    public function addSystemNamespace (string $namespace, string $directory) : void
    {
        $dir = $this->prepareDirectory($namespace, $directory);

        if (null !== $dir)
        {
            $this->systemNamespaces[$namespace] = $dir;
        }
    }


    /**
     * Prepares the directory
     *
     * @param string $namespace
     * @param string $directory
     * @return null|string
     * @throws AssetsException
     */
    private function prepareDirectory (string $namespace, string $directory) : ?string
    {
        $dir = "{$this->projectDir}/" . trim($directory, "/");

        if (isset($this->namespaces[$namespace]))
        {
            throw new AssetsException(sprintf(
                "Duplicate registration of namespace '%s'.",
                $namespace
            ));
        }

        return is_dir($dir) ? $dir : null;
    }


    /**
     * @return \ArrayIterator|\Traversable
     */
    public function getIterator ()
    {
        return new \ArrayIterator($this->namespaces);
    }


    /**
     * Returns the path to the namespace
     *
     * @param string $namespace
     * @return string
     * @throws AssetsException
     */
    public function getPath (string $namespace) : string
    {
        $path = $this->namespaces[$namespace] ?? $this->systemNamespaces[$namespace] ?? null;

        if (null === $path)
        {
            throw new AssetsException(sprintf(
                "Unknown entry namespace: '%s'",
                $namespace
            ));
        }

        return $path;
    }


    /**
     * Returns the full file path for the given namespaced asset
     *
     * @param NamespacedAsset $asset
     * @return string
     * @throws AssetsException
     */
    public function getFilePath (NamespacedAsset $asset) : string
    {
        return "{$this->getPath($asset->getNamespace())}/{$asset->getPath()}";
    }
}
{'fraction_non_alphanumeric': 0.12398731947868968, 'fraction_numerical': 0.0, 'mean_word_length': 2.00635256749603, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5597273', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1531, 'n_words': 370}