<commit_before><?php

namespace Bolt\Provider;

use Bolt\AccessControl\Token\Token;
use Bolt\Collection\Bag;
use Bolt\Menu\Builder;
use Bolt\Menu\MenuBuilder;
use Bolt\Menu\MenuEntry;
use Bolt\Menu\Resolver;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\Stopwatch\Stopwatch;

class MenuServiceProvider implements ServiceProviderInterface
{
    /**
     * {@inheritdoc}
     */
    public function register(Application $app)
    {
        $app['menu'] = $app->share(
            function ($app) {
                $builder = new MenuBuilder($app);

                return $builder;
            }
        );

        /**
         * @internal Backwards compatibility not guaranteed on this provider presently.
         */
        $app['menu.admin'] = $app->share(
            function ($app) {
                // This service should not be invoked until request cycle since it depends
                // on url generation and request base path. Probably should be refactored somehow.
                $baseUrl = '';
                if (($request = $app['request_stack']->getCurrentRequest()) !== null) {
                    $baseUrl = $request->getBasePath();
                }
                $baseUrl .= '/' . trim($app['controller.backend.mount_prefix'], '/');

                $rootEntry = MenuEntry::createRoot($app['url_generator'], $baseUrl);
                $token = $app['session']->get('authentication');
                if (!$token instanceof Token) {
                    return $rootEntry;
                }
                $user = $token->getUser();

                /** @var Stopwatch $watch */
                $watch = $app['stopwatch'];

                // ~ 1 ms
                $watch->start('menu.build.admin');
                $builder = new Builder\AdminMenu();
                $builder->build($rootEntry);
                $watch->stop('menu.build.admin');

                // ~ 2 ms
                $watch->start('menu.build.admin_content');
                $contentTypes = Bag::fromRecursive($app['config']->get('contenttypes'));
                $builder = new Builder\AdminContent($contentTypes);
                $builder->build($rootEntry);
                $watch->stop('menu.build.admin_content');

                // ~ 100 ms
                $watch->start('menu.resolve.recent');
                $resolver = new Resolver\RecentlyEdited($app['storage'], $app['markdown']);
                $resolver->resolve($rootEntry, $contentTypes);
                $watch->stop('menu.resolve.recent');

                // ~ 20 ms
                $watch->start('menu.resolve.access');
                $resolver = new Resolver\Access($app['permissions']);
                $resolver->resolve($rootEntry, $user);
                $watch->stop('menu.resolve.access');

                return $rootEntry;
            }
        );
    }

    /**
     * {@inheritdoc}
     */
    public function boot(Application $app)
    {
    }
}
<commit_msg>Split builders out of base service<commit_after><?php

namespace Bolt\Provider;

use Bolt\AccessControl\Token\Token;
use Bolt\Collection\Bag;
use Bolt\Menu\Builder;
use Bolt\Menu\MenuBuilder;
use Bolt\Menu\MenuEntry;
use Bolt\Menu\Resolver;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\Stopwatch\Stopwatch;

class MenuServiceProvider implements ServiceProviderInterface
{
    /**
     * {@inheritdoc}
     */
    public function register(Application $app)
    {
        $app['menu'] = $app->share(
            function ($app) {
                $builder = new MenuBuilder($app);

                return $builder;
            }
        );

        /**
         * @internal Backwards compatibility not guaranteed on this provider presently.
         */
        $app['menu.admin_builder'] = function ($app) {
            $baseUrl = '';
            if (($request = $app['request_stack']->getCurrentRequest()) !== null) {
                $baseUrl = $request->getBasePath();
            }
            $baseUrl .= '/' . trim($app['controller.backend.mount_prefix'], '/');

            $rootEntry = MenuEntry::createRoot($app['url_generator'], $baseUrl);

            $builder = new Builder\AdminMenu();
            $builder->build($rootEntry);

            $contentTypes = Bag::fromRecursive($app['config']->get('contenttypes'));
            $builder = new Builder\AdminContent($contentTypes);
            $builder->build($rootEntry);

            return $rootEntry;
        };

        /**
         * @internal Backwards compatibility not guaranteed on this provider presently.
         */
        $app['menu.admin'] = $app->share(
            function ($app) {
                $token = $app['session']->get('authentication');
                if (!$token instanceof Token) {
                    return MenuEntry::create('root');
                }
                $user = $token->getUser();

                /** @var Stopwatch $watch */
                $watch = $app['stopwatch'];
                $rootEntry = $app['menu.admin_builder'];
                $contentTypes = Bag::from($app['config']->get('contenttypes'));

                // ~ 100 ms
                $watch->start('menu.resolve.recent');
                $resolver = new Resolver\RecentlyEdited($app['storage'], $app['markdown']);
                $resolver->resolve($rootEntry, $contentTypes);
                $watch->stop('menu.resolve.recent');

                // ~ 20 ms
                $watch->start('menu.resolve.access');
                $resolver = new Resolver\Access($app['permissions']);
                $resolver->resolve($rootEntry, $user);
                $watch->stop('menu.resolve.access');

                return $rootEntry;
            }
        );
    }

    /**
     * {@inheritdoc}
     */
    public function boot(Application $app)
    {
    }
}

{'fraction_non_alphanumeric': 0.14405618362452896, 'fraction_numerical': 0.0020554984583761563, 'mean_word_length': 1.799137104506232, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, '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': '913662', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1475, 'n_words': 285}