<commit_before><?php

namespace SimpleFilemanager\Implementation\Silex\Controllers;

use Psr\Log\InvalidArgumentException;
use Silex\Application;
use SimpleFilemanager\Lib\SimpleFilemanager;
use Symfony\Component\Finder\Exception\OperationNotPermitedException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;

class Filemanager
{

    /** @var SimpleFilemanager */
    private $sfm;

    /**
     * @param Application $app
     * @param string|null $directory
     *
     * @return string
     */
    public function listAction(Application $app, $directory = null)
    {
        /** @var SimpleFilemanager $sfm */
        $sfm = $app['simple-filemanager.service'];
        $listing = is_null($directory) ? $sfm->listRoot() : $sfm->listDirectoryEntries($directory);
        return $app['twig']->render('@simple-filemanager/list.twig', ['listing' => $listing, 'directory' => is_null($directory) ? DIRECTORY_SEPARATOR : $directory]);
    }

    /**
     * @param Application $app
     * @param string|null $directory
     *
     * @return RedirectResponse
     */
    public function uploadAction(Application $app, $directory = null)
    {
        /** @var SimpleFilemanager $sfm */
        $sfm = $app['simple-filemanager.service'];

        /** @var Request $request */
        $request = $app['request'];

        /** @var UploadedFile $uploadedFile */<commit_msg>Add search fuction<commit_after><?php

namespace SimpleFilemanager\Implementation\Silex\Controllers;

use Psr\Log\InvalidArgumentException;
use Silex\Application;
use SimpleFilemanager\Lib\SimpleFilemanager;
use Symfony\Component\Finder\Exception\OperationNotPermitedException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;

class Filemanager
{

    /** @var SimpleFilemanager */
    private $sfm;

    /**
     * @param Application $app
     * @param string|null $directory
     *
     * @return string
     */
    public function listAction(Application $app, $directory = null)
    {
        /** @var SimpleFilemanager $sfm */
        $sfm = $app['simple-filemanager.service'];
        $listing = is_null($directory) ? $sfm->listRoot() : $sfm->listDirectoryEntries($directory);
        return $app['twig']->render('@simple-filemanager/list.twig', ['listing' => $listing, 'directory' => is_null($directory) ? DIRECTORY_SEPARATOR : $directory]);
    }

    /**
     * @param string $search
     * @param array $extensions
     *
     * @return mixed
     */
    public function search(string $search, array $extensions = [])
    {
        $search = preg_replace('/[^a-zA-Z0-9\-_\.]/', '', $search);
        $searchRegex = str_replace(['-', '.'], ['\-', '\.'], $search);

        $extensionsRegex = implode('|', $extensions);

        return Finder::create()->files()->in($this->rootDirectory)->name('/('.$searchRegex.').*' . ($extensions ? '\.(' . $extensionsRegex . ')' : '') . '$/i')->sortByName();
    }

    /**
     * @param Application $app
     * @param string|null $directory
     *
     * @return RedirectResponse
     */
    public function uploadAction(Application $app, $directory = null)
    {
        /** @var SimpleFilemanager $sfm */
        $sfm = $app['simple-filemanager.service'];

        /** @var Request $request */
        $request = $app['request'];

        /** @var UploadedFile $uploadedFile */
{'fraction_non_alphanumeric': 0.15080213903743314, 'fraction_numerical': 0.0005347593582887701, 'mean_word_length': 3.4011764705882355, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, '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': '2647065', 'n_tokens_mistral': 1096, 'n_tokens_neox': 1074, 'n_words': 200}