DrupalKernel example



use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$autoloader = require_once 'autoload.php';

$kernel = new DrupalKernel('prod', $autoloader);

$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();

$kernel->terminate($request$response);
parent::__construct($environment$class_loader$allow_dumping);
  }

  /** * Sets a container with a kernel service on the Drupal class. * * @return \Drupal\Component\DependencyInjection\ContainerInterface * A container with the kernel service set. */
  public static function setContainerWithKernel() {
    $container = new ContainerBuilder();
    $kernel = new DrupalKernel('test', NULL);
    // Objects of the same type will have access to each others private and     // protected members even though they are not the same instances. This is     // because the implementation specific details are already known when     // inside those objects.     $kernel->container = $container;
    $container->set('kernel', $kernel);
    $container->set(ReverseContainer::classnew ReverseContainer($container));
    \Drupal::setContainer($container);
    return $container;
  }

}
/** * Boots up a Drupal environment. * * @return \Drupal\Core\DrupalKernelInterface * The Drupal kernel. * * @throws \Exception * Exception thrown if kernel does not boot. */
  protected function boot() {
    $kernel = new DrupalKernel('prod', $this->classLoader, FALSE);
    $kernel::bootEnvironment();
    $kernel->setSitePath($this->getSitePath());
    Settings::initialize($kernel->getAppRoot()$kernel->getSitePath()$this->classLoader);
    $kernel->boot();
    // Some services require a request to work. For example, CommentManager.     // This is needed as generating the URL fires up entity load hooks.     $kernel->getContainer()
      ->get('request_stack')
      ->push(Request::createFromGlobals());

    return $kernel;
  }
/** * {@inheritdoc} * * @throws \Symfony\Component\Console\Exception\InvalidArgumentException */
  protected function execute(InputInterface $input, OutputInterface $output): int {
    $root = dirname(__DIR__, 5);
    chdir($root);

    $this->classLoader = require 'autoload.php';
    $kernel = new DrupalKernel('prod', $this->classLoader, FALSE);
    $kernel::bootEnvironment();
    $kernel->setSitePath($input->getOption('site-path'));
    Settings::initialize($kernel->getAppRoot()$kernel->getSitePath()$this->classLoader);

    $request = Request::createFromGlobals();

    $kernel->boot();
    $kernel->preHandle($request);

    $container = $kernel->getContainer();
    $uid = $input->getArgument('uid');
    


use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$autoloader = require_once 'autoload.php';

$kernel = new DrupalKernel('prod', $autoloader);

$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();

$kernel->terminate($request$response);
return $this->install($this->classLoader, $io$install_profile$input->getOption('langcode')$this->getSitePath()$input->getOption('site-name'));
  }

  /** * Returns whether there is already an existing Drupal installation. * * @return bool */
  protected function isDrupalInstalled() {
    try {
      $kernel = new DrupalKernel('prod', $this->classLoader, FALSE);
      $kernel::bootEnvironment();
      $kernel->setSitePath($this->getSitePath());
      Settings::initialize($kernel->getAppRoot()$kernel->getSitePath()$this->classLoader);
      $kernel->boot();
    }
    catch (ConnectionNotDefinedException $e) {
      return FALSE;
    }
    return !empty(Database::getConnectionInfo());
  }

  
// Assert that we call the setApcuPrefix on the classloader if     // class_loader_auto_detect is set to TRUE;     if ($value) {
      $classloader->setApcuPrefix(Argument::type('string'))->shouldBeCalled();
    }
    else {
      $classloader->setApcuPrefix(Argument::type('string'))->shouldNotBeCalled();
    }

    // Create a kernel suitable for testing.     $kernel = new DrupalKernel('test', $classloader->reveal(), FALSE, vfsStream::url('root'));
    $kernel->setSitePath(vfsStream::url('root/sites/default'));
    $kernel->boot();
  }

}
$autoload = $connection_info['default']['autoload'] ?? '';
    if (str_contains($autoload, 'src/Driver/Database/')) {
      [$first$second] = explode('\\', $namespace, 3);
      if ($first === 'Drupal' && strtolower($second) === $second) {
        // Add the module that provides the database driver to the list of         // modules as the first to be enabled.         array_unshift($modules$second);
      }
    }

    // Bootstrap the kernel. Do not use createFromRequest() to retain Settings.     $kernel = new DrupalKernel('testing', $this->classLoader, FALSE);
    $kernel->setSitePath($this->siteDirectory);
    // Boot a new one-time container from scratch. Set the module list upfront     // to avoid a subsequent rebuild or setting the kernel into the     // pre-installer mode.     $extensions = $modules ? $this->getExtensionsForModules($modules) : [];
    $kernel->updateModules($extensions$extensions);

    // DrupalKernel::boot() is not sufficient as it does not invoke preHandle(),     // which is required to initialize legacy global variables.     $request = Request::create('/');
    $kernel->boot();
    

  public function testDiscoverServiceCustom() {
    new Settings([
      'container_yamls' => [
        __DIR__ . '/fixtures/custom.yml',
      ],
    ]);

    $kernel = new DrupalKernel('prod', new ClassLoader());
    $kernel->discoverServiceProviders();

    $reflected_yamls = (new \ReflectionObject($kernel))->getProperty('serviceYamls');

    $expect = [
      'app' => [
        'core' => 'core/core.services.yml',
      ],
      'site' => [
        __DIR__ . '/fixtures/custom.yml',
      ],
    ];
public function testSystemInitIgnoresSecondaries() {
    // Clone the master credentials to a replica connection.     // Note this will result in two independent connection objects that happen     // to point to the same place.     $connection_info = Database::getConnectionInfo('default');
    Database::addConnectionInfo('default', 'replica', $connection_info['default']);

    /** @var \Drupal\Core\Database\ReplicaKillSwitch $service */
    $service = \Drupal::service('database.replica_kill_switch');
    $service->trigger();
    $class_loader = require $this->root . '/autoload.php';
    $kernel = new DrupalKernel('testing', $class_loader, FALSE);
    $event = new RequestEvent($kernel, Request::create('http://example.com'), HttpKernelInterface::MAIN_REQUEST);
    $service->checkReplicaServer($event);

    $db1 = Database::getConnection('default', 'default');
    $db2 = Database::getConnection('replica', 'default');

    $this->assertSame($db1$db2, 'System Init ignores secondaries when requested.');

    // Makes sure that session value set right.     $session = \Drupal::service('session');
    $this->assertTrue($session->has('ignore_replica_server'));
    
 $result);
  }

  /** * @covers ::getTestClasses */
  public function testGetTestsInProfiles() {
    $this->setupVfsWithTestClasses();
    $class_loader = $this->prophesize(ClassLoader::class);

    $container = new Container();
    $container->set('kernel', new DrupalKernel('prod', new ClassLoader()));
    $container->setParameter('site.path', 'sites/default');
    \Drupal::setContainer($container);

    $test_discovery = new TestDiscovery('vfs://drupal', $class_loader->reveal());

    $result = $test_discovery->getTestClasses('test_profile_module', ['PHPUnit-Kernel']);
    $expected = [
      'example3' => [
        'Drupal\Tests\test_profile_module\Kernel\KernelExampleTest4' => [
          'name' => 'Drupal\Tests\test_profile_module\Kernel\KernelExampleTest4',
          'description' => 'Test description',
          
Home | Imprint | This part of the site doesn't use cookies.