Compare commits
No commits in common. "main" and "1-no-autowiring" have entirely different histories.
main
...
1-no-autow
9 changed files with 43 additions and 165 deletions
|
@ -1,4 +0,0 @@
|
||||||
# Autowire Example
|
|
||||||
|
|
||||||
An example module to demonstrate autowiring service classes in Drupal 8, as
|
|
||||||
well as how to define Controllers as Services.
|
|
|
@ -1,5 +1,3 @@
|
||||||
name: 'Autowire Example'
|
name: Foo
|
||||||
type: module
|
type: module
|
||||||
description: 'An example module to demonstrate autowiring in Drupal 8.'
|
|
||||||
core: 8.x
|
core: 8.x
|
||||||
package: 'Example modules'
|
|
||||||
|
|
9
autowire_example.module
Normal file
9
autowire_example.module
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements hook_preprocess_HOOK().
|
||||||
|
*/
|
||||||
|
function autowire_example_preprocess_html() {
|
||||||
|
$service = \Drupal::service('autowire_example.test');
|
||||||
|
$service->__invoke();
|
||||||
|
}
|
|
@ -1,6 +0,0 @@
|
||||||
autowire_example.user_count:
|
|
||||||
path: '/user-count'
|
|
||||||
defaults:
|
|
||||||
_controller: 'Drupal\autowire_example\Controller\ExampleController::__invoke'
|
|
||||||
requirements:
|
|
||||||
_access: 'TRUE'
|
|
|
@ -1,9 +1,5 @@
|
||||||
services:
|
services:
|
||||||
Drupal\Core\Entity\EntityTypeManagerInterface:
|
autowire_example.test:
|
||||||
alias: entity_type.manager
|
class: Drupal\autowire_example\Service\ExampleService
|
||||||
|
arguments:
|
||||||
Drupal\autowire_example\Controller\ExampleController:
|
- '@current_user'
|
||||||
autowire: true
|
|
||||||
|
|
||||||
Drupal\autowire_example\Service\UserCounter:
|
|
||||||
autowire: true
|
|
||||||
|
|
|
@ -1,48 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Drupal\autowire_example\Controller;
|
|
||||||
|
|
||||||
use Drupal\autowire_example\Service\UserCounter;
|
|
||||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An example controller.
|
|
||||||
*/
|
|
||||||
class ExampleController {
|
|
||||||
|
|
||||||
use StringTranslationTrait;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The user counter service.
|
|
||||||
*
|
|
||||||
* @var \Drupal\autowire_example\Service\UserCounter
|
|
||||||
*/
|
|
||||||
private $userCounter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ExampleController constructor.
|
|
||||||
*
|
|
||||||
* @param \Drupal\autowire_example\Service\UserCounter $user_counter
|
|
||||||
* The user counter service.
|
|
||||||
*/
|
|
||||||
public function __construct(UserCounter $user_counter) {
|
|
||||||
$this->userCounter = $user_counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Display the username of the current user.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
* A render array.
|
|
||||||
*/
|
|
||||||
public function __invoke() {
|
|
||||||
return [
|
|
||||||
'#markup' => $this->formatPlural(
|
|
||||||
number_format($this->userCounter->getActiveUserCount()),
|
|
||||||
'This site has 1 active user.',
|
|
||||||
'This site has @count active users.'
|
|
||||||
)
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
29
src/Service/ExampleService.php
Normal file
29
src/Service/ExampleService.php
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\autowire_example\Service;
|
||||||
|
|
||||||
|
use Drupal\Core\Session\AccountProxyInterface;
|
||||||
|
|
||||||
|
class ExampleService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Drupal\Core\Session\AccountProxyInterface
|
||||||
|
*/
|
||||||
|
private $currentUser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bar constructor.
|
||||||
|
*
|
||||||
|
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
|
||||||
|
* The current user.
|
||||||
|
*/
|
||||||
|
public function __construct(AccountProxyInterface $current_user) {
|
||||||
|
$this->currentUser = $current_user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke() {
|
||||||
|
dump($this->currentUser->id());
|
||||||
|
die;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,46 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Drupal\autowire_example\Service;
|
|
||||||
|
|
||||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A service to count users.
|
|
||||||
*/
|
|
||||||
class UserCounter {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The user storage instance.
|
|
||||||
*
|
|
||||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
|
||||||
*/
|
|
||||||
private $userStorage;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* UserCounter constructor.
|
|
||||||
*
|
|
||||||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
|
||||||
* The entity type manager.
|
|
||||||
*
|
|
||||||
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
|
|
||||||
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
|
|
||||||
*/
|
|
||||||
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
|
|
||||||
$this->userStorage = $entity_type_manager->getStorage('user');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Count the active users.
|
|
||||||
*
|
|
||||||
* @return integer
|
|
||||||
* The number of active users.
|
|
||||||
*/
|
|
||||||
public function getActiveUserCount() {
|
|
||||||
return (int) $this->userStorage
|
|
||||||
->getQuery()
|
|
||||||
->condition('status', 1)
|
|
||||||
->count()
|
|
||||||
->execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,50 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Drupal\Tests\autowire_example\Kernel\Service;
|
|
||||||
|
|
||||||
use Drupal\autowire_example\Service\UserCounter;
|
|
||||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the user counter service.
|
|
||||||
*/
|
|
||||||
class UserCounterTest extends EntityKernelTestBase {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The user counter service.
|
|
||||||
*
|
|
||||||
* @var \Drupal\autowire_example\Service\UserCounter
|
|
||||||
*/
|
|
||||||
private $userCounter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public static $modules = ['autowire_example'];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
protected function setUp() {
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
$this->userCounter = $this->container->get(UserCounter::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function active_users_are_counted() {
|
|
||||||
$this->createUser(['status' => 1]);
|
|
||||||
$this->createUser(['status' => 1]);
|
|
||||||
|
|
||||||
$this->assertEquals(2, $this->userCounter->getActiveUserCount());
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function blocked_users_are_not_counted() {
|
|
||||||
$this->createUser(['status' => 1]);
|
|
||||||
$this->createUser(['status' => 0]);
|
|
||||||
|
|
||||||
$this->assertEquals(1, $this->userCounter->getActiveUserCount());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue