<commit_before><?php

namespace Bolt\Composer;

use Bolt\Composer\Action\DumpAutoload;
use Bolt\Composer\Action\InitJson;
use Bolt\Composer\Action\InstallPackage;
use Bolt\Composer\Action\RemovePackage;
use Bolt\Composer\Action\RequirePackage;
use Bolt\Composer\Action\SearchPackage;
use Bolt\Composer\Action\ShowPackage;
use Bolt\Composer\Action\UpdatePackage;
use Bolt\Library as Lib;
use Bolt\Translation\Translator as Trans;
use Composer\Factory;
use Composer\IO\BufferIO;
use Composer\Json\JsonFile;
use Guzzle\Http\Client as GuzzleClient;
use Guzzle\Http\Exception\RequestException;
use Guzzle\Http\Exception\CurlException;
use Silex\Application;

class PackageManager
{
    /**
     * @var array
     */
    private $options;

    /**
     * @var Composer\IO\BufferIO
     */
    private $io;

    /**
     * @var Composer\Composer
     */
    private $composer;

    /**
     * @var Bolt\Composer\Action\DumpAutoload
     */
    private $dumpautoload;

    /**
     * @var Bolt\Composer\Action\InitJson
     */
    private $initJson;

    /**
     * @var Bolt\Composer\Action\RemovePackage
     */
    private $remove;

    /**
     * @var Bolt\Composer\Action\RequirePackage
     */
    private $require;

    /**
     * @var Bolt\Composer\Action\SearchPackage
     */
    private $search;

    /**
     * @var Bolt\Composer\Action\ShowPackage
     */
    private $show;

    /**
     * @var Bolt\Composer\Action\UpdatePackage
     */
    private $update;

    /**
     * @var Silex\Application
     */
    private $app;

    /**
     * @var boolean
     */
    private $downgradeSsl = false;

    /**
     * @var array
     */
    public $messages = array();

    /**
     *
     * @param Application $app
     * @param boolean     $readWriteMode
     */
    public function __construct(Application $app)
    {
        $this->app = $app;

        // Get default options
        $this->getOptions();

        // Set composer environment variables
        putenv('COMPOSER_HOME=' . $this->app['resources']->getPath('cache') . '/composer');

        /*
         * If the extension project area is writable, ensure the JSON is up-to-date
         * and test connection to the extension server.
         *
         * If all is OK, set $app['extend.online'] to TRUE
         */
        if ($app['extend.writeable']) {
            // Copy/update installer helper
            $this->copyInstaller();

            // Do required JSON set up
            $this->setupJson();

            // Ping the extensions server to confirm connection
            $response = $this->ping($this->app['extend.site'], 'ping', true);
            $httpOk = array(200, 301, 302);
            if (in_array($response, $httpOk)) {
                $app['extend.online'] = true;
            } else {
                $this->messages[] = $this->app['extend.site'] . ' is unreachable.';
            }
        }

        if ($app['extend.online']) {
            // Create the IO
            $this->io = new BufferIO();

            // Create the Composer object
            $this->composer = $this->getComposer();
        }
    }

    /**
     * Get a new Composer object
     *
     * @return Composer\Composer
     */
    public function getComposer()
    {
        // Set working directory
        chdir($this->options['basedir']);

        // Use the factory to get a new Composer object
        $this->composer = Factory::create($this->io, $this->options['composerjson'], true);

        if ($this->downgradeSsl) {
            $this->allowSslDowngrade(true);
        }

        return $this->composer;
    }

    /**
     * Return the output from the last IO
     *
     * @return array
     */
    public function getOutput()
    {
        return $this->io->getOutput();
    }

    /**
     * Dump fresh autoloader
     */
    public function dumpautoload()
    {
        if (!$this->dumpautoload) {
            $this->dumpautoload = new DumpAutoload($this->io, $this->composer, $this->options);
        }

        $this->dumpautoload->execute();
    }

    /**
     * Install configured packages
     *
     * @return integer 0 on success or a positive error code on failure
     */
    public function installPackages()
    {
        if (!$this->install) {
            $this->install = new InstallPackage($this->io, $this->composer, $this->options);
        }

        // 0 on success or a positive error code on failure
        return $this->install->execute();
    }

    /**
     * Remove packages from the root install
     *
     * @param $packages array Indexed array of package names to remove
     * @return integer 0 on success or a positive error code on failure
     */
    public function removePackage(array $packages)
    {
        if (!$this->remove) {
            $this->remove = new RemovePackage($this->app, $this->io, $this->composer, $this->options);
        }

        // 0 on success or a positive error code on failure
        return $this->remove->execute($packages);
    }

    /**
     * Require (install) packages
     *
     * @param $packages array Associative array of package names/versions to remove
     *                        Format: array('name' => '', 'version' => '')
     * @return integer 0 on success or a positive error code on failure
     */
    public function requirePackage(array $packages)
    {
        if (!$this->require) {
            $this->require = new RequirePackage($this->app, $this->io, $this->composer, $this->options);
        }

        // 0 on success or a positive error code on failure
        return $this->require->execute($packages);
    }

    /**
     * Search for packages
     *
     * @param $packages array Indexed array of package names to search
     * @return array List of matching packages
     */
    public function searchPackage(array $packages)
    {
        if (!$this->search) {
            $this->search = new SearchPackage($this->io, $this->composer, $this->options);
        }

        return $this->search->execute($packages);
    }

    /**
     * Show packages
     *
     * @param $packages
     * @return
     */
    public function showPackage($target, $package = '', $version = '')
    {
        if (!$this->show) {
            $this->show = new ShowPackage($this->io, $this->composer, $this->options);
        }

        return $this->show->execute($target, $package, $version);
    }

    /**
     * Remove packages from the root install
     *
     * @param $packages array Indexed array of package names to remove
     * @return integer 0 on success or a positive error code on failure
     */<commit_msg>Move more PackageManger set up out of the constructor<commit_after><?php

namespace Bolt\Composer;

use Bolt\Composer\Action\DumpAutoload;
use Bolt\Composer\Action\InitJson;
use Bolt\Composer\Action\InstallPackage;
use Bolt\Composer\Action\RemovePackage;
use Bolt\Composer\Action\RequirePackage;
use Bolt\Composer\Action\SearchPackage;
use Bolt\Composer\Action\ShowPackage;
use Bolt\Composer\Action\UpdatePackage;
use Bolt\Library as Lib;
use Bolt\Translation\Translator as Trans;
use Composer\Factory;
use Composer\IO\BufferIO;
use Composer\Json\JsonFile;
use Guzzle\Http\Client as GuzzleClient;
use Guzzle\Http\Exception\RequestException;
use Guzzle\Http\Exception\CurlException;
use Silex\Application;

class PackageManager
{
    /**
     * @var array
     */
    private $options;

    /**
     * @var Composer\IO\BufferIO
     */
    private $io;

    /**
     * @var Composer\Composer
     */
    private $composer;

    /**
     * @var Bolt\Composer\Action\DumpAutoload
     */
    private $dumpautoload;

    /**
     * @var Bolt\Composer\Action\InitJson
     */
    private $initJson;

    /**
     * @var Bolt\Composer\Action\RemovePackage
     */
    private $remove;

    /**
     * @var Bolt\Composer\Action\RequirePackage
     */
    private $require;

    /**
     * @var Bolt\Composer\Action\SearchPackage
     */
    private $search;

    /**
     * @var Bolt\Composer\Action\ShowPackage
     */
    private $show;

    /**
     * @var Bolt\Composer\Action\UpdatePackage
     */
    private $update;

    /**
     * @var Silex\Application
     */
    private $app;

    /**
     * @var boolean
     */
    private $downgradeSsl = false;

    /**
     * @var array
     */
    public $messages = array();

    /**
     *
     * @param Application $app
     * @param boolean     $readWriteMode
     */
    public function __construct(Application $app)
    {
        $this->app = $app;

        // Set composer environment variables
        putenv('COMPOSER_HOME=' . $this->app['resources']->getPath('cache') . '/composer');

        // Get default options
        $this->getOptions();

        // Set up
        $this->setup();
    }

    /**
     * If the extension project area is writable, ensure the JSON is up-to-date
     * and test connection to the extension server.
     *
     * $app['extend.writeable'] is originally set in Extend::register()
     *
     * If all is OK, set $app['extend.online'] to TRUE
     */
    private function setup()
    {
        if ($this->app['extend.writeable']) {
            // Copy/update installer helper
            $this->copyInstaller();

            // Do required JSON set up
            $this->setupJson();

            // Ping the extensions server to confirm connection
            $response = $this->ping($this->app['extend.site'], 'ping', true);
            $httpOk = array(200, 301, 302);
            if (in_array($response, $httpOk)) {
                $this->app['extend.online'] = true;
            } else {
                $this->messages[] = $this->app['extend.site'] . ' is unreachable.';
            }
        }

        if ($this->app['extend.online']) {
            // Create the IO
            $this->io = new BufferIO();

            // Create the Composer object
            $this->composer = $this->getComposer();
        }
    }

    /**
     * Get a new Composer object
     *
     * @return Composer\Composer
     */
    public function getComposer()
    {
        // Set working directory
        chdir($this->options['basedir']);

        // Use the factory to get a new Composer object
        $this->composer = Factory::create($this->io, $this->options['composerjson'], true);

        if ($this->downgradeSsl) {
            $this->allowSslDowngrade(true);
        }

        return $this->composer;
    }

    /**
     * Return the output from the last IO
     *
     * @return array
     */
    public function getOutput()
    {
        return $this->io->getOutput();
    }

    /**
     * Dump fresh autoloader
     */
    public function dumpautoload()
    {
        if (!$this->dumpautoload) {
            $this->dumpautoload = new DumpAutoload($this->io, $this->composer, $this->options);
        }

        $this->dumpautoload->execute();
    }

    /**
     * Install configured packages
     *
     * @return integer 0 on success or a positive error code on failure
     */
    public function installPackages()
    {
        if (!$this->install) {
            $this->install = new InstallPackage($this->io, $this->composer, $this->options);
        }

        // 0 on success or a positive error code on failure
        return $this->install->execute();
    }

    /**
     * Remove packages from the root install
     *
     * @param $packages array Indexed array of package names to remove
     * @return integer 0 on success or a positive error code on failure
     */
    public function removePackage(array $packages)
    {
        if (!$this->remove) {
            $this->remove = new RemovePackage($this->app, $this->io, $this->composer, $this->options);
        }

        // 0 on success or a positive error code on failure
        return $this->remove->execute($packages);
    }

    /**
     * Require (install) packages
     *
     * @param $packages array Associative array of package names/versions to remove
     *                        Format: array('name' => '', 'version' => '')
     * @return integer 0 on success or a positive error code on failure
     */
    public function requirePackage(array $packages)
    {
        if (!$this->require) {
            $this->require = new RequirePackage($this->app, $this->io, $this->composer, $this->options);
        }

        // 0 on success or a positive error code on failure
        return $this->require->execute($packages);
    }

    /**
     * Search for packages
     *
     * @param $packages array Indexed array of package names to search
     * @return array List of matching packages
     */
    public function searchPackage(array $packages)
    {
        if (!$this->search) {
            $this->search = new SearchPackage($this->io, $this->composer, $this->options);
        }

        return $this->search->execute($packages);
    }

    /**
     * Show packages
     *
     * @param $packages
     * @return
     */
    public function showPackage($target, $package = '', $version = '')
    {
        if (!$this->show) {
            $this->show = new ShowPackage($this->io, $this->composer, $this->options);
        }

        return $this->show->execute($target, $package, $version);
    }

    /**
     * Update packages in the root install
     *
     * @param  $packages array Indexed array of package names to update
     * @return integer 0 on success or a positive error code on failure
     */
{'fraction_non_alphanumeric': 0.13084461734121344, 'fraction_numerical': 0.0023939552629610233, 'mean_word_length': 2.0128465179175117, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 143, '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': '5877128', 'n_tokens_mistral': 3916, 'n_tokens_neox': 3802, 'n_words': 984}