<commit_before><?php namespace Orchestra\Foundation\Processor;

use Closure;
use Illuminate\Support\Fluent;
use Illuminate\Support\Facades\Event;
use Orchestra\Support\Facades\Publisher;
use Orchestra\Support\Facades\Foundation;
use Orchestra\Support\Facades\Extension as E;
use Orchestra\Contracts\Publisher\FilePermissionException;
use Orchestra\Foundation\Presenter\Extension as ExtensionPresenter;
use Orchestra\Foundation\Validation\Extension as ExtensionValidator;

class Extension extends Processor
{
    /**
     * Create a new processor instance.
     *
     * @param  \Orchestra\Foundation\Presenter\Extension  $presenter
     * @param  \Orchestra\Foundation\Validation\Extension  $validator
     */
    public function __construct(ExtensionPresenter $presenter, ExtensionValidator $validator)
    {
        $this->presenter = $presenter;
        $this->validator = $validator;
    }

    /**
     * View all extension page.
     *
     * @param  object  $listener
     * @return mixed
     */
    public function index($listener)
    {
        $data['extensions'] = E::detect();

        return $listener->indexSucceed($data);
    }

    /**
     * View edit extension configuration page.
     *
     * @param  object  $listener
     * @param  \Illuminate\Support\Fluent  $extension
     * @return mixed
     */
    public function configure($listener, Fluent $extension)
    {
        if (! E::started($extension->get('name'))) {
            return $listener->suspend(404);
        }

        $memory = Foundation::memory();

        // Load configuration from memory.
        $activeConfig = (array) $memory->get("extensions.active.{$extension->get('name')}.config", []);
        $baseConfig   = (array) $memory->get("extension_{$extension->get('name')}", []);

        $eloquent = new Fluent(array_merge($activeConfig, $baseConfig));

        // Add basic form, allow extension to add custom configuration field
        // to this form using events.
        $form = $this->presenter->configure($eloquent, $extension->get('name'));

        Event::fire("orchestra.form: extension.{$extension->get('name')}", [$eloquent, $form]);

        return $listener->configureSucceed(compact('eloquent', 'form', 'extension'));
    }

    /**
     * Update an extension configuration.
     *
     * @param  object  $listener
     * @param  \Illuminate\Support\Fluent  $extension
     * @param  array  $input
     * @return mixed
     */
    public function update($listener, Fluent $extension, array $input)
    {
        if (! E::started($extension->get('name'))) {
            return $listener->suspend(404);
        }

        $validation = $this->validator->with($input, ["orchestra.validate: extension.{$extension->get('name')}"]);

        if ($validation->fails()) {
            return $listener->updateValidationFailed($validation, $extension->uid);
        }

        $memory = Foundation::memory();
        $config = (array) $memory->get("extension.active.{$extension->get('name')}.config", []);
        $input  = new Fluent(array_merge($config, $input));

        unset($input['_token']);

        Event::fire("orchestra.saving: extension.{$extension->get('name')}", [& $input]);

        $memory->put("extensions.active.{$extension->get('name')}.config", $input->getAttributes());
        $memory->put("extension_{$extension->get('name')}", $input->getAttributes());

        Event::fire("orchestra.saved: extension.{$extension->get('name')}", [$input]);

        return $listener->updateSucceed($extension);
    }

    /**
     * Execute installation or update for an extension.
     *
     * @param  object  $listener
     * @param  string  $type
     * @param  \Illuminate\Support\Fluent  $extension
     * @param  \Closure  $callback
     * @return mixed
     */
    protected function execute($listener, $type, Fluent $extension, Closure $callback)
    {
        try {
            // Check if folder is writable via the web instance, this would
            // avoid issue running Orchestra Platform with debug as true where
            // creating/copying the directory would throw an ErrorException.
            if (! E::permission($extension->get('name'))) {
                throw new FilePermissionException("[{$extension->get('name')}] is not writable.");
            }

            call_user_func($callback, $extension->get('name'));
        } catch (FilePermissionException $e) {
            // In events where extension can't be activated due to extension
            // publish failed to push the asset to proper path. We need to
            // put this under a publisher queue.
            Publisher::queue($extension->get('name'));

            return call_user_func([$listener, "{$type}Failed"], $extension);
        }

        return call_user_func([$listener, "{$type}Succeed"], $extension);
    }
}<commit_msg>Remove dead code.<commit_after><?php namespace Orchestra\Foundation\Processor;

use Closure;
use Illuminate\Support\Fluent;
use Illuminate\Support\Facades\Event;
use Orchestra\Support\Facades\Publisher;
use Orchestra\Support\Facades\Foundation;
use Orchestra\Support\Facades\Extension as E;
use Orchestra\Contracts\Publisher\FilePermissionException;
use Orchestra\Foundation\Presenter\Extension as ExtensionPresenter;
use Orchestra\Foundation\Validation\Extension as ExtensionValidator;

class Extension extends Processor
{
    /**
     * Create a new processor instance.
     *
     * @param  \Orchestra\Foundation\Presenter\Extension  $presenter
     * @param  \Orchestra\Foundation\Validation\Extension  $validator
     */
    public function __construct(ExtensionPresenter $presenter, ExtensionValidator $validator)
    {
        $this->presenter = $presenter;
        $this->validator = $validator;
    }

    /**
     * View all extension page.
     *
     * @param  object  $listener
     * @return mixed
     */
    public function index($listener)
    {
        $data['extensions'] = E::detect();

        return $listener->indexSucceed($data);
    }

    /**
     * View edit extension configuration page.
     *
     * @param  object  $listener
     * @param  \Illuminate\Support\Fluent  $extension
     * @return mixed
     */
    public function configure($listener, Fluent $extension)
    {
        if (! E::started($extension->get('name'))) {
            return $listener->suspend(404);
        }

        $memory = Foundation::memory();

        // Load configuration from memory.
        $activeConfig = (array) $memory->get("extensions.active.{$extension->get('name')}.config", []);
        $baseConfig   = (array) $memory->get("extension_{$extension->get('name')}", []);

        $eloquent = new Fluent(array_merge($activeConfig, $baseConfig));

        // Add basic form, allow extension to add custom configuration field
        // to this form using events.
        $form = $this->presenter->configure($eloquent, $extension->get('name'));

        Event::fire("orchestra.form: extension.{$extension->get('name')}", [$eloquent, $form]);

        return $listener->configureSucceed(compact('eloquent', 'form', 'extension'));
    }

    /**
     * Update an extension configuration.
     *
     * @param  object  $listener
     * @param  \Illuminate\Support\Fluent  $extension
     * @param  array  $input
     * @return mixed
     */
    public function update($listener, Fluent $extension, array $input)
    {
        if (! E::started($extension->get('name'))) {
            return $listener->suspend(404);
        }

        $validation = $this->validator->with($input, ["orchestra.validate: extension.{$extension->get('name')}"]);

        if ($validation->fails()) {
            return $listener->updateValidationFailed($validation, $extension->uid);
        }

        $memory = Foundation::memory();
        $config = (array) $memory->get("extension.active.{$extension->get('name')}.config", []);
        $input  = new Fluent(array_merge($config, $input));

        unset($input['_token']);

        Event::fire("orchestra.saving: extension.{$extension->get('name')}", [& $input]);

        $memory->put("extensions.active.{$extension->get('name')}.config", $input->getAttributes());
        $memory->put("extension_{$extension->get('name')}", $input->getAttributes());

        Event::fire("orchestra.saved: extension.{$extension->get('name')}", [$input]);

        return $listener->updateSucceed($extension);
    }
}
{'fraction_non_alphanumeric': 0.15118635984261358, 'fraction_numerical': 0.0014307857398354596, 'mean_word_length': 2.952874646559849, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, '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': '4877373', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2283, 'n_words': 488}