<commit_before><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Autoload;

use Composer\Installer\InstallationManager;
use Composer\Json\JsonFile;
use Composer\Package\Loader\JsonLoader;
use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryInterface;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class AutoloadGenerator
{
    private $localRepo;
    private $package;
    private $installationManager;

    public function __construct(RepositoryInterface $localRepo, PackageInterface $package, InstallationManager $installationManager)
    {
        $this->localRepo = $localRepo;
        $this->package = $package;
        $this->installationManager = $installationManager;
    }

    public function dump($targetDir)
    {
        $autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php');

        $autoloadFile .= <<<'EOF'

// autoload.php generated by Composer

function init() {
    $loader = new ClassLoader();

    $map = require __DIR__.'/autoload_namespaces.php';

    foreach ($map as $namespace => $path) {
        $loader->add($namespace, $path);
    }

    $loader->register();

    return $loader;
}

return init();
EOF;

        $namespacesFile = <<<'EOF'
<?php

// autoload_namespace.php generated by Composer

return array(

EOF;

        $autoloads = $this->parseAutoloads();

        if (isset($autoloads['psr-0'])) {
            foreach ($autoloads['psr-0'] as $def) {
                foreach ($def['mapping'] as $prefix => $path) {
                    $exportedPrefix = var_export($prefix, true);
                    $exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true);
                    $namespacesFile .= "    $exportedPrefix => dirname(dirname(__DIR__)).$exportedPath,\n";
                }
            }
        }

        $namespacesFile .= ");\n";

        file_put_contents($targetDir.'/autoload.php', $autoloadFile);
        file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
    }

    private function parseAutoloads()
    {
        $installPaths = array();
        foreach ($this->localRepo->getPackages() as $package) {
            $installPaths[] = array(
                $package,
                $this->installationManager->getInstallPath($package)
            );
        }
        $installPaths[] = array($this->package, '');

        $autoloads = array();
        foreach ($installPaths as $item) {
            list($package, $installPath) = $item;

            if (null !== $package->getTargetDir()) {
                $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
            }

            foreach ($package->getAutoload() as $type => $mapping) {
                $autoloads[$type][] = array(
                    'mapping'   => $mapping,
                    'path'      => $installPath,
                );
            }
        }

        return $autoloads;
    }
}<commit_msg>Reverse-orders namespaces by specificity<commit_after><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Autoload;

use Composer\Installer\InstallationManager;
use Composer\Json\JsonFile;
use Composer\Package\Loader\JsonLoader;
use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryInterface;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class AutoloadGenerator
{
    private $localRepo;
    private $package;
    private $installationManager;

    public function __construct(RepositoryInterface $localRepo, PackageInterface $package, InstallationManager $installationManager)
    {
        $this->localRepo = $localRepo;
        $this->package = $package;
        $this->installationManager = $installationManager;
    }

    public function dump($targetDir)
    {
        $autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php');

        $autoloadFile .= <<<'EOF'

// autoload.php generated by Composer

function init() {
    $loader = new ClassLoader();

    $map = require __DIR__.'/autoload_namespaces.php';

    foreach ($map as $namespace => $path) {
        $loader->add($namespace, $path);
    }

    $loader->register();

    return $loader;
}

return init();
EOF;

        $namespacesFile = <<<'EOF'
<?php

// autoload_namespace.php generated by Composer

return array(

EOF;

        $autoloads = $this->parseAutoloads();

        if (isset($autoloads['psr-0'])) {
            foreach ($autoloads['psr-0'] as $def) {
                $exportedPrefix = var_export($def['namespace'], true);
                $exportedPath = var_export($def['path'], true);
                $namespacesFile .= "    $exportedPrefix => dirname(dirname(__DIR__)).$exportedPath,\n";
            }
        }

        $namespacesFile .= ");\n";

        file_put_contents($targetDir.'/autoload.php', $autoloadFile);
        file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
    }

    private function parseAutoloads()
    {
        $installPaths = array();
        foreach ($this->localRepo->getPackages() as $package) {
            $installPaths[] = array(
                $package,
                $this->installationManager->getInstallPath($package)
            );
        }
        $installPaths[] = array($this->package, '');

        $autoloads = array();
        foreach ($installPaths as $item) {
            list($package, $installPath) = $item;

            if (null !== $package->getTargetDir()) {
                $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
            }

            foreach ($package->getAutoload() as $type => $mapping) {
                foreach ($mapping as $namespace => $path) {
                    $autoloads[$type][] = array(
                        'namespace'   => $namespace,
                        'path'      => ($installPath ? '/'.$installPath : '').'/'.$path,
                    );
                }
            }
        }

        foreach ($autoloads as $type => $maps) {
            usort($autoloads[$type], function ($a, $b) {
                return strcmp($b['namespace'], $a['namespace']);
            });
        }

        return $autoloads;
    }
}
{'fraction_non_alphanumeric': 0.1472251949243235, 'fraction_numerical': 0.0009172909341079345, 'mean_word_length': 2.3394589076059216, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 68, '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': '5001577', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1892, 'n_words': 389}