<commit_before><?php
/**
 * This file is part of the teleport package.
 *
 * Copyright (c) MODX, LLC
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Teleport;

use Composer\Script\CommandEvent;
use Symfony\Component\Finder\Finder;

/**
 * Provides Composer scripting callbacks.
 *
 * @package Teleport
 */
class Composer
{
    /**
     * Composer post-install-cmd callback.
     *
     * @param CommandEvent $event The composer Event object.
     */
    public static function postInstall(CommandEvent $event)
    {
        self::symLinkTpl($event);
    }

    /**
     * Composer post-update-cmd callback.
     *
     * @param CommandEvent $event The composer Event object.
     */
    public static function postUpdate(CommandEvent $event)
    {
        self::symLinkTpl($event);
    }

    /**
     * Create symlinks to teleport tpl files in local tpl directory.
     *
     * @param CommandEvent $event The composer Event object.
     */
    public static function symLinkTpl(CommandEvent $event)
    {
        $config = self::getOptions($event);

        /* symlink the tpl files from the teleport library */
        $sources = new Finder();
        $sources->files()->ignoreVCS(true)->ignoreDotFiles(true)->name('*.tpl.json')->name('*.php')->in(dirname(dirname(__DIR__)) . '/tpl');

        foreach ($sources as $source) {
            self::createSymLink($event, dirname(dirname(__DIR__)) . '/tpl', $source, $config);
        }
    }

    /**
     * Create a symlink to a source in a specified target directory.
     *
     * @param CommandEvent $event The composer Event object.
     * @param string $base The base path of the installation.
     * @param \SplFileInfo $source The source file info.
     * @param array $config Project config options.
     */
    protected static function createSymLink($event, $base, $source, $config)
    {
        $filename = $source->getPathname();
        $relative = substr($filename, strlen($base) + 1);
        $relativeFilename = '../vendor/modxcms/teleport/tpl/' . $relative;
        $target = $config['teleport-tpl-dir'] . '/' . $relative;
        if (!is_dir(dirname($target))) {
            @mkdir(dirname($target), 0777, true);
        }
        if ($config['teleport-tpl-update'] && file_exists($target)) {
            unlink($target);
        }
        if (!file_exists($target)) {
            symlink($relativeFilename, $target);
        }
    }

    /**
     * Get the config data from the extra definition in composer.json.<commit_msg>And now to make it work on symlinks from various depths<commit_after><?php
/**
 * This file is part of the teleport package.
 *
 * Copyright (c) MODX, LLC
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Teleport;

use Composer\Script\CommandEvent;
use Symfony\Component\Finder\Finder;

/**
 * Provides Composer scripting callbacks.
 *
 * @package Teleport
 */
class Composer
{
    /**
     * Composer post-install-cmd callback.
     *
     * @param CommandEvent $event The composer Event object.
     */
    public static function postInstall(CommandEvent $event)
    {
        self::symLinkTpl($event);
    }

    /**
     * Composer post-update-cmd callback.
     *
     * @param CommandEvent $event The composer Event object.
     */
    public static function postUpdate(CommandEvent $event)
    {
        self::symLinkTpl($event);
    }

    /**
     * Create symlinks to teleport tpl files in local tpl directory.
     *
     * @param CommandEvent $event The composer Event object.
     */
    public static function symLinkTpl(CommandEvent $event)
    {
        $config = self::getOptions($event);

        /* symlink the tpl files from the teleport library */
        $sources = new Finder();
        $sources->files()->ignoreVCS(true)->ignoreDotFiles(true)->name('*.tpl.json')->name('*.php')->in(dirname(dirname(__DIR__)) . '/tpl');

        foreach ($sources as $source) {
            self::createSymLink($event, dirname(dirname(__DIR__)) . '/tpl', $source, $config);
        }
    }

    /**
     * Create a symlink to a source in a specified target directory.
     *
     * @param CommandEvent $event The composer Event object.
     * @param string $base The base path of the installation.
     * @param \SplFileInfo $source The source file info.
     * @param array $config Project config options.
     */
    protected static function createSymLink($event, $base, $source, $config)
    {
        $filename = $source->getPathname();
        $relative = substr($filename, strlen($base) + 1);
        $depth = substr_count($relative, '/');
        $relativeFilename = str_repeat('../', $depth + 1) . 'vendor/modxcms/teleport/tpl/' . $relative;
        $target = $config['teleport-tpl-dir'] . '/' . $relative;
        if (!is_dir(dirname($target))) {
            @mkdir(dirname($target), 0777, true);
        }
        if ($config['teleport-tpl-update'] && file_exists($target)) {
            unlink($target);
        }
        if (!file_exists($target)) {
            symlink($relativeFilename, $target);
        }
    }

    /**
     * Get the config data from the extra definition in composer.json.
{'fraction_non_alphanumeric': 0.13061532653831634, 'fraction_numerical': 0.0020762551906379767, 'mean_word_length': 2.5755735492577596, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '2852167', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1502, 'n_words': 426}