<commit_before><?php

namespace Studio\Composer;

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Studio\Config\Config;
use Studio\Config\FileStorage;
use Symfony\Component\Finder\Finder;

class StudioPlugin implements PluginInterface, EventSubscriberInterface
{
    public function activate(Composer $composer, IOInterface $io)
    {
        // ...
    }

    public static function getSubscribedEvents()
    {
        return [
            ScriptEvents::POST_AUTOLOAD_DUMP => 'dumpAutoload',
        ];
    }

    public function dumpAutoload(Event $event)
    {
        $path = $event->getComposer()->getPackage()->getTargetDir();
        $studioFile = "{$path}studio.json";

        $config = $this->getConfig($studioFile);
        if ($config->hasPackages()) {
            $this->autoloadFrom($config->getPackages());
        }
    }

    /**
     * Instantiate and return the config object.
     *
     * @param string $file
     * @return Config
     */
    protected function getConfig($file)
    {
        return new Config(new FileStorage($file));
    }

    protected function autoloadFrom(array $directories)
    {
        $finder = new Finder();

        // Find all Composer autoloader files in the supervised packages' directories
        // so that we can include and setup all of their dependencies.
        $autoloaders = $finder->in($directories)
                              ->files()
                              ->name('autoload.php')
                              ->depth('<= 3')
                              ->followLinks();

        $includes = [];
        foreach ($autoloaders as $file) {
            $includes[] = "require_once __DIR__ . '/../$file';";
        }

        $this->appendIncludes($includes);
    }

    protected function appendIncludes(array $includes)
    {
        $contents = "\n" . '// @generated by Composer Studio (https://github.com/franzliedke/studio)' . "\n";
        $contents .= "\n" . implode("\n", $includes) . "\n";

        $autoloadFile = 'vendor/autoload.php';

        file_put_contents($autoloadFile, $contents, FILE_APPEND);
    }
}<commit_msg>Fix autoloads not being included.<commit_after><?php

namespace Studio\Composer;

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Studio\Config\Config;
use Studio\Config\FileStorage;
use Symfony\Component\Finder\Finder;

class StudioPlugin implements PluginInterface, EventSubscriberInterface
{
    public function activate(Composer $composer, IOInterface $io)
    {
        // ...
    }

    public static function getSubscribedEvents()
    {
        return [
            ScriptEvents::POST_AUTOLOAD_DUMP => 'dumpAutoload',
        ];
    }

    public function dumpAutoload(Event $event)
    {
        $path = $event->getComposer()->getPackage()->getTargetDir();
        $studioFile = "{$path}studio.json";

        $config = $this->getConfig($studioFile);
        if ($config->hasPackages()) {
            $this->autoloadFrom($config->getPackages());
        }
    }

    /**
     * Instantiate and return the config object.
     *
     * @param string $file
     * @return Config
     */
    protected function getConfig($file)
    {
        return new Config(new FileStorage($file));
    }

    protected function autoloadFrom(array $directories)
    {
        $finder = new Finder();

        // Find all Composer autoloader files in the supervised packages' directories
        // so that we can include and setup all of their dependencies.
        $autoloaders = $finder->in($directories)
                              ->files()
                              ->name('autoload.php')
                              ->depth('<= 3')
                              ->followLinks();

        $includes = [];
        foreach ($autoloaders as $file) {
            $includes[] = "require_once __DIR__ . '/../$file';";
        }

        $this->appendIncludes($includes);
    }

    protected function appendIncludes(array $includes)
    {
        $code = '// @generated by Composer Studio (https://github.com/franzliedke/studio)' . "\n";
        $code .= "\n" . implode("\n", $includes) . "\n";

        $autoloadFile = 'vendor/autoload.php';
        $contents = file_get_contents($autoloadFile);

        $contents = str_replace(
            'return ComposerAutoloader',
            "$code\nreturn ComposerAutoloader",
            $contents
        );

        file_put_contents($autoloadFile, $contents);
    }
}
{'fraction_non_alphanumeric': 0.12568536482496837, 'fraction_numerical': 0.00042176296921130323, 'mean_word_length': 2.2419685577580313, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 2, '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': '3803910', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1325, 'n_words': 285}