<commit_before><?php namespace CupOfTea\WordPress\Composer;

use Composer\Composer;
use Composer\IO\IOInterface;
use InvalidArgumentException;
use Composer\Util\Filesystem;
use Composer\Plugin\PluginInterface;
use Composer\Package\PackageInterface;
use Composer\Installer\BinaryInstaller;
use Composer\Installer\LibraryInstaller;
use Composer\Downloader\FilesystemException;
use Composer\Repository\InstalledRepositoryInterface;

class Installer extends LibraryInstaller
{
    /**
     * Package Type handled by this Installer.
     *
     * @const string
     */
    const PACKAGE_TYPE = 'cupoftea-wordpress';
    
    protected $plugin;
    
    protected $templates = [];
    
    public function __construct(IOInterface $io, Composer $composer, PluginInterface $plugin, $type = 'library', Filesystem $filesystem = null, BinaryInstaller $binaryInstaller = null)
    {
        parent::__construct($io, $composer, $type, $filesystem, $binaryInstaller);
        
        $this->plugin = $plugin;
    }
    
    /**
     * {@inheritDoc}
     */
    public function supports($packageType)
    {
        return $packageType === self::PACKAGE_TYPE;
    }
    
    /**
     * {@inheritDoc}
     */
    public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
    {
        if ($repo->hasPackage($package)) {
            $files = $this->getInstallFiles($package);
            
            return count($files) && is_readable($this->getInstallPath($package) . '/' . $files[0]);
        }
        
        return false;
    }
    
    /**
     * {@inheritDoc}
     */
    public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
    {
        $this->installCode($package);
        
        if (! $repo->hasPackage($package)) {
            $repo->addPackage(clone $package);
        }
    }
    
    /**
     * {@inheritDoc}
     */
    public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
    {
        if (! $repo->hasPackage($initial)) {
            throw new InvalidArgumentException('Package is not installed: ' . $initial);
        }
        
        $this->updateCode($initial, $target);
        $repo->removePackage($initial);
        
        if (! $repo->hasPackage($target)) {
            $repo->addPackage(clone $target);
        }
    }
    
    /**
     * {@inheritDoc}
     */
    public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
    {
        if (! $repo->hasPackage($package)) {
            throw new InvalidArgumentException('Package is not installed: '.$package);
        }
        
        $this->removeCode($package);
        $repo->removePackage($package);
    }
    
    /**
     * {@inheritDoc}
     */
    public function getInstallPath(PackageInterface $package)
    {
        if ($package->getPrettyName() !== 'cupoftea/wordpress') {
            throw new InvalidArgumentException('This installer can only be used to install the cupoftea/wordpress package.');
        }
        
        return '.';
    }
    
    /**
     * Get the temp path.
     * 
     * @param  \Composer\Package\PackageInterface  $package
     * @return string
     */
    protected function getTempPath(PackageInterface $package)
    {
        return str_replace('/', '-', $package->getPrettyName()) . '_' . $package->getPrettyVersion();
    }
    
    /**
     * Get the list of files to install.
     * 
     * @param  \Composer\Package\PackageInterface  $package
     * @return array
     */
    protected function getInstallFiles(PackageInterface $package)
    {
        return $package->getExtra()['files'];
    }
    
    /**
     * Set up the .gitignore file.
     * 
     * @param  \Composer\Package\PackageInterface  $package
     * @return void
     */
    protected function installGitignore(PackageInterface $package)
    {
        $installPath = $this->getInstallPath($package) . '/.gitignore';
        $templatePath = $this->getTempPath($package) . '/.gitignore.template';
        
        $data = ['APP_PUBLIC' => $this->plugin->getPublicDirectory()];
        
        if (! file_exists($installPath)) {
            return $this->compileTemplate($templatePath, $installPath, $data);
        }
        
        $downloadGitignore = $this->compileTemplate($templatePath, $data);
        $installGitignore = file_get_contents($installPath);
        
        $gitignore = preg_split('/\r?\n/', $downloadGitignore . PHP_EOL . '# User rules' . PHP_EOL . $installGitignore);
        $group = 'user rules';
        $groups = [];
        
        $groups['user rules'] = [];
        
        foreach ($gitignore as $rule) {
            if (preg_match('/^#\s*(.*)/', $rule, $matches)) {
                $group = strtolower($matches[1]);
                
                if (! isset($groups[$group])) {
                    $groups[$group] = [];
                }
            } elseif (! preg_match('/^\s*$/', $rule) && ! in_array($rule, $groups[$group])) {
                $groups[$group][] = $rule;
            }
        }
        
        $gitignore = [];
        
        foreach ($groups as $group => $rules) {
            if (! count($rules) || $group == 'user rules') {
                continue;
            }
            
            sort($rules);
            
            $gitignore[] = '# ' . ucfirst($group);
            $gitignore = array_merge($gitignore, $rules);
            $gitignore[] = '';
        }
        
        $gitignore[] = '# User rules';
        $gitignore = array_merge($gitignore, $groups['user rules']);
        $gitignore[] = '';
        
        file_put_contents($installPath, implode(PHP_EOL, $gitignore));
    }
    
    /**
     * Set up the .env file.
     * 
     * @param  \Composer\Package\PackageInterface  $package
     * @return void
     */
    protected function installDotEnv(PackageInterface $package)
    {
        $templatePath = $this->getTempPath($package) . '/.env.template';
        $dotEnvPath = $this->getInstallPath($package) . '/.env';
        $dotEnvExamplePath = $this->getInstallPath($package) . '/.env.example';
        
        if (! file_exists($templatePath)) {
            return;
        }
        
        $saltKeys = [
            'AUTH_KEY',
            'SECURE_AUTH_KEY',
            'LOGGED_IN_KEY',
            'NONCE_KEY',
            'AUTH_SALT',
            'SECURE_AUTH_SALT',
            'LOGGED_IN_SALT',
            'NONCE_SALT'
        ];<commit_msg>StyleCI<commit_after><?php namespace CupOfTea\WordPress\Composer;

use Composer\Composer;
use Composer\IO\IOInterface;
use InvalidArgumentException;
use Composer\Util\Filesystem;
use Composer\Plugin\PluginInterface;
use Composer\Package\PackageInterface;
use Composer\Installer\BinaryInstaller;
use Composer\Installer\LibraryInstaller;
use Composer\Downloader\FilesystemException;
use Composer\Repository\InstalledRepositoryInterface;

class Installer extends LibraryInstaller
{
    /**
     * Package Type handled by this Installer.
     *
     * @const string
     */
    const PACKAGE_TYPE = 'cupoftea-wordpress';
    
    protected $plugin;
    
    protected $templates = [];
    
    public function __construct(IOInterface $io, Composer $composer, PluginInterface $plugin, $type = 'library', Filesystem $filesystem = null, BinaryInstaller $binaryInstaller = null)
    {
        parent::__construct($io, $composer, $type, $filesystem, $binaryInstaller);
        
        $this->plugin = $plugin;
    }
    
    /**
     * {@inheritDoc}
     */
    public function supports($packageType)
    {
        return $packageType === self::PACKAGE_TYPE;
    }
    
    /**
     * {@inheritDoc}
     */
    public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
    {
        if ($repo->hasPackage($package)) {
            $files = $this->getInstallFiles($package);
            
            return count($files) && is_readable($this->getInstallPath($package) . '/' . $files[0]);
        }
        
        return false;
    }
    
    /**
     * {@inheritDoc}
     */
    public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
    {
        $this->installCode($package);
        
        if (! $repo->hasPackage($package)) {
            $repo->addPackage(clone $package);
        }
    }
    
    /**
     * {@inheritDoc}
     */
    public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
    {
        if (! $repo->hasPackage($initial)) {
            throw new InvalidArgumentException('Package is not installed: ' . $initial);
        }
        
        $this->updateCode($initial, $target);
        $repo->removePackage($initial);
        
        if (! $repo->hasPackage($target)) {
            $repo->addPackage(clone $target);
        }
    }
    
    /**
     * {@inheritDoc}
     */
    public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
    {
        if (! $repo->hasPackage($package)) {
            throw new InvalidArgumentException('Package is not installed: '.$package);
        }
        
        $this->removeCode($package);
        $repo->removePackage($package);
    }
    
    /**
     * {@inheritDoc}
     */
    public function getInstallPath(PackageInterface $package)
    {
        if ($package->getPrettyName() !== 'cupoftea/wordpress') {
            throw new InvalidArgumentException('This installer can only be used to install the cupoftea/wordpress package.');
        }
        
        return '.';
    }
    
    /**
     * Get the temp path.
     *
     * @param  \Composer\Package\PackageInterface  $package
     * @return string
     */
    protected function getTempPath(PackageInterface $package)
    {
        return str_replace('/', '-', $package->getPrettyName()) . '_' . $package->getPrettyVersion();
    }
    
    /**
     * Get the list of files to install.
     *
     * @param  \Composer\Package\PackageInterface  $package
     * @return array
     */
    protected function getInstallFiles(PackageInterface $package)
    {
        return $package->getExtra()['files'];
    }
    
    /**
     * Set up the .gitignore file.
     *
     * @param  \Composer\Package\PackageInterface  $package
     * @return void
     */
    protected function installGitignore(PackageInterface $package)
    {
        $installPath = $this->getInstallPath($package) . '/.gitignore';
        $templatePath = $this->getTempPath($package) . '/.gitignore.template';
        
        $data = ['APP_PUBLIC' => $this->plugin->getPublicDirectory()];
        
        if (! file_exists($installPath)) {
            return $this->compileTemplate($templatePath, $installPath, $data);
        }
        
        $downloadGitignore = $this->compileTemplate($templatePath, $data);
        $installGitignore = file_get_contents($installPath);
        
        $gitignore = preg_split('/\r?\n/', $downloadGitignore . PHP_EOL . '# User rules' . PHP_EOL . $installGitignore);
        $group = 'user rules';
        $groups = [];
        
        $groups['user rules'] = [];
        
        foreach ($gitignore as $rule) {
            if (preg_match('/^#\s*(.*)/', $rule, $matches)) {
                $group = strtolower($matches[1]);
                
                if (! isset($groups[$group])) {
                    $groups[$group] = [];
                }
            } elseif (! preg_match('/^\s*$/', $rule) && ! in_array($rule, $groups[$group])) {
                $groups[$group][] = $rule;
            }
        }
        
        $gitignore = [];
        
        foreach ($groups as $group => $rules) {
            if (! count($rules) || $group == 'user rules') {
                continue;
            }
            
            sort($rules);
            
            $gitignore[] = '# ' . ucfirst($group);
            $gitignore = array_merge($gitignore, $rules);
            $gitignore[] = '';
        }
        
        $gitignore[] = '# User rules';
        $gitignore = array_merge($gitignore, $groups['user rules']);
        $gitignore[] = '';
        
        file_put_contents($installPath, implode(PHP_EOL, $gitignore));
    }
    
    /**
     * Set up the .env file.
     *
     * @param  \Composer\Package\PackageInterface  $package
     * @return void
     */
    protected function installDotEnv(PackageInterface $package)
    {
        $templatePath = $this->getTempPath($package) . '/.env.template';
        $dotEnvPath = $this->getInstallPath($package) . '/.env';
        $dotEnvExamplePath = $this->getInstallPath($package) . '/.env.example';
        
        if (! file_exists($templatePath)) {
            return;
        }
        
        $saltKeys = [
            'AUTH_KEY',
            'SECURE_AUTH_KEY',
            'LOGGED_IN_KEY',
            'NONCE_KEY',
            'AUTH_SALT',
            'SECURE_AUTH_SALT',
            'LOGGED_IN_SALT',
            'NONCE_SALT'
        ];
{'fraction_non_alphanumeric': 0.13723978411719354, 'fraction_numerical': 0.00030840400925212025, 'mean_word_length': 2.036993678295481, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2594489', 'n_tokens_mistral': 3668, 'n_tokens_neox': 3605, 'n_words': 668}