<commit_before><?php

namespace Oro\Bundle\DistributionBundle\Script;

use Composer\Installer\InstallationManager;
use Composer\Package\PackageInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class Runner
{
    /**
     * @var \Composer\Installer\InstallationManager
     */
    protected $installationManager;

    /**
     * @var string|null
     */
    protected $applicationRootDir;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @var string
     */
    protected $environment;

    /**
     * @param InstallationManager $installationManager
     * @param LoggerInterface $logger
     * @param string $applicationRootDir
     * @param string $environment
     */
    public function __construct(
        InstallationManager $installationManager,
        LoggerInterface $logger,
        $applicationRootDir,
        $environment
    ) {
        $this->installationManager = $installationManager;
        $this->logger = $logger;
        $this->applicationRootDir = realpath($applicationRootDir);
        $this->environment = $environment;
    }

    /**
     * @param PackageInterface $package
     * @return string
     */
    public function install(PackageInterface $package)
    {
        return $this->run($this->getPackageScriptPath($package, 'install.php'));
    }

    /**
     * @param PackageInterface $package
     * @return string
     */
    public function uninstall(PackageInterface $package)
    {
        return $this->run($this->getPackageScriptPath($package, 'uninstall.php'));
    }

    /**
     * @param PackageInterface $updatedPackage
     * @param string $previousPackageVersion
     * @return string
     */
    public function update(PackageInterface $updatedPackage, $previousPackageVersion)
    {
        $migrationScripts = $this->findMigrationScripts($updatedPackage, $previousPackageVersion);
        if (!$migrationScripts) {
            $migrationScripts = [$this->getPackageScriptPath($updatedPackage, 'update.php')];
        }

        $output = [];
        foreach ($migrationScripts as $script) {
            $scriptOutput = $this->run($script);
            if ($scriptOutput) {
                $output[] = $scriptOutput;
            }
        }
        if (!$output) {

            return null;
        }

        return implode(PHP_EOL, $output);
    }

    /**
     * @return string
     * @throws ProcessFailedException
     */
    public function runPlatformUpdate()
    {
        return $this->runCommand('oro:platform:update');
    }

    /**
     * @return string
     * @throws ProcessFailedException
     */
    public function clearApplicationCache()
    {
        return $this->runCommand('cache:clear');
    }

    /**
     * @return string
     * @throws ProcessFailedException
     */
    public function updateDBSchema()
    {
        return $this->runCommand('oro:migration:load');
    }

    /**
     * Needed to be executed after package has been uninstalled so that main application (app/console) could be built
     */
    public function removeCachedFiles()
    {
        if (!$this->applicationRootDir) {
            return;
        }
        $finder = new Finder();
        $finder->files()
            ->in($this->applicationRootDir)
            ->name('bundles.php')
            ->name('*ProjectContainer.php');

        foreach ($finder as $file) {
            /** @var SplFileInfo $file */
            if (is_file($file->getPathname())) {
                $this->logger->info(sprintf('Removing %s', $file->getPathname()));
                unlink($file->getPathname());
            }
        }
    }

    /**
     * @param PackageInterface[] $packages
     *
     * @return string
     */
    public function loadFixtures(array $packages)<commit_msg>BAP-3164: Fix package installer<commit_after><?php

namespace Oro\Bundle\DistributionBundle\Script;

use Composer\Installer\InstallationManager;
use Composer\Package\PackageInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class Runner
{
    /**
     * @var \Composer\Installer\InstallationManager
     */
    protected $installationManager;

    /**
     * @var string|null
     */
    protected $applicationRootDir;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @var string
     */
    protected $environment;

    /**
     * @param InstallationManager $installationManager
     * @param LoggerInterface $logger
     * @param string $applicationRootDir
     * @param string $environment
     */
    public function __construct(
        InstallationManager $installationManager,
        LoggerInterface $logger,
        $applicationRootDir,
        $environment
    ) {
        $this->installationManager = $installationManager;
        $this->logger = $logger;
        $this->applicationRootDir = realpath($applicationRootDir);
        $this->environment = $environment;
    }

    /**
     * @param PackageInterface $package
     * @return string
     */
    public function install(PackageInterface $package)
    {
        return $this->run($this->getPackageScriptPath($package, 'install.php'));
    }

    /**
     * @param PackageInterface $package
     * @return string
     */
    public function uninstall(PackageInterface $package)
    {
        return $this->run($this->getPackageScriptPath($package, 'uninstall.php'));
    }

    /**
     * @param PackageInterface $updatedPackage
     * @param string $previousPackageVersion
     * @return string
     */
    public function update(PackageInterface $updatedPackage, $previousPackageVersion)
    {
        $migrationScripts = $this->findMigrationScripts($updatedPackage, $previousPackageVersion);
        if (!$migrationScripts) {
            $migrationScripts = [$this->getPackageScriptPath($updatedPackage, 'update.php')];
        }

        $output = [];
        foreach ($migrationScripts as $script) {
            $scriptOutput = $this->run($script);
            if ($scriptOutput) {
                $output[] = $scriptOutput;
            }
        }
        if (!$output) {

            return null;
        }

        return implode(PHP_EOL, $output);
    }

    /**
     * @return string
     * @throws ProcessFailedException
     */
    public function runPlatformUpdate()
    {
        return $this->runCommand('oro:platform:update');
    }

    /**
     * @return string
     * @throws ProcessFailedException
     */
    public function clearApplicationCache()
    {
        return $this->runCommand('cache:clear');
    }

    /**
     * @return string
     * @throws ProcessFailedException
     */
    public function updateDBSchema()
    {
        return $this->runCommand('doctrine:schema:update --force');
    }

    /**
     * Needed to be executed after package has been uninstalled so that main application (app/console) could be built
     */
    public function removeCachedFiles()
    {
        if (!$this->applicationRootDir) {
            return;
        }
        $finder = new Finder();
        $finder->files()
            ->in($this->applicationRootDir)
            ->name('bundles.php')
            ->name('*ProjectContainer.php');

        foreach ($finder as $file) {
            /** @var SplFileInfo $file */
            if (is_file($file->getPathname())) {
                $this->logger->info(sprintf('Removing %s', $file->getPathname()));
                unlink($file->getPathname());
            }
        }
    }

    /**
     * @param PackageInterface[] $packages
     *
     * @return string
     */
    public function loadFixtures(array $packages)
{'fraction_non_alphanumeric': 0.12090112640801001, 'fraction_numerical': 0.0005006257822277847, 'mean_word_length': 2.3660488626790226, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '2269409', 'n_tokens_mistral': 2158, 'n_tokens_neox': 2129, 'n_words': 419}