Commit b4138560 authored by Rodrigo Aguilera's avatar Rodrigo Aguilera
Browse files

Initial module.

parent 53fb289e
Pipeline #183579840 passed with stages
in 4 minutes and 29 seconds
/vendor/
vendor
/web/
web
/node_modules/
.env
composer.lock
yarn.lock
/.editorconfig
/.gitattributes
#PHPUnit output
junit.xml
.phpunit.result.cache
# Ignore local overrides.
docker-compose.override.yml
include:
- project: 'drupalspoons/webmasters'
# Best practice is to pin to a tag or a SHA1. https://docs.gitlab.com/ee/ci/yaml/#includefile
ref: 1.7.0-rc1
# The template below may be inspected at https://gitlab.com/drupalspoons/webmasters/-/blob/master/templates/.gitlab-ci.yml
file: 'templates/.gitlab-ci.yml'
#
# Projects may override anything in the template above.
# The code below is specific to devel project. You dont need to copy it to your new project.
#
# Run tests on Drupal 9.0 by default. Avoids compat breaks in 9.1+
composer_node:
variables:
# https://getcomposer.org/doc/articles/versions.md#next-significant-release-operators
DRUPAL_CORE_CONSTRAINT: ~9.0.0
<?php
namespace Spoons;
use Composer\Script\Event;
use Symfony\Component\Process\Process;
/**
* A Composer script handler.
*/
class ScriptHandler {
/**
* Create a web/modules/[SLUG] dir and symlink all project files into it.
*
* @param \Composer\Script\Event $event
* A Composer package event.
*/
public static function createSymlinks(Event $event) {
$full_name = $event->getComposer()->getPackage()->getName();
[, $project_name] = explode('/', $full_name);
$cmd = "rm -rf web/modules/custom/$project_name && mkdir -p web/modules/custom/$project_name";
$process = new Process($cmd);
$process->mustRun();
$cmd = 'find ../../../.. -maxdepth 1 ! -name .git ! -name web ! -name vendor ! -name .idea -print | while read file; do ln -s "$file" .; done';
$process = new Process($cmd, "web/modules/custom/$project_name");
$process->mustRun();
}
}
# Autoservices and autowiring
Brings features from symfony 3.3 and above into the Drupal
service container.
## Features
* Register services without a MODULENAME.services.yml just the class.
* [Autowire](https://symfony.com/doc/current/service_container/autowiring.html) those services.
* Register aliases for many Drupal services to ease autowiring.
## Usage
This module provides new ways for other modules to register services so
it does nothing by itself and it should be installed as a dependency.
Once enabled any class placed in `modulename/src/Autoservice/` will be
registered into the container with autowiring enabled. This means that
if the constructor has all the classes that it needs correctly type-hinted
with the corresponding interfaces the service will be available
with its full class name as the identifier.
Since the autowiring needs to know what service corresponds with an
interface and Drupal doesn't provide that mapping this module also
adds many aliases so the autowiring knows better what service to inject.
It won't work if you interface doesn't follow the pattern of being in
the same namespace as the service and the same name with the `Interface`
suffix.
Other services that can be registered:
* `modulename/src/AutoEventListener/`: The event_listener tag is automatically added. Autowired.
* `modulename/src/AutoPluginManager/`: Inherits from the `default_plugin_manager`. Not autowired.
## Contributions
The project is open to improvements about how to bring more
symfony container features into Drupal but also feel free to open any discussion about
how to promote and stabilize it further so more modules
can rely on it.
Patches on drupal.org are accepted but merge requests on
[gitlab](https://gitlab.com/upstreamable/drupal-autoservices) are preferred.
## Real time communication
Join the [#autoservices](https://drupalchat.me/channel/autoservices)
channel on [drupalchat.me](https://drupalchat.me).
## Notes
This project started as a way to ease development of
[Libraries provider](https://gitlab.com/upstreamable/drupal-libraries-provider)
so at the moment it works for the services defined there (no services.yml file!)
If you want to contribute to bring this features into core
[there is a meta issue.](https://www.drupal.org/project/drupal/issues/3021900)
The inspiration about how to achieve the auto-registrations came from
[this talk](https://www.youtube.com/watch?v=q06b2uVM8MQ) (mute the left channel of your audio).
## Similar modules
[Extended container](https://www.drupal.org/project/extended_container): it needs
patches to Drupal and that makes it more difficult to install and maintain.
name: Autoservices
type: module
description: Register services into the container without configuration.
package: Container
core_version_requirement: ^8.8 || ^9
{
"name": "drupal/autoservices",
"description": "Register services into the container without configuration",
"type": "drupal-module",
"homepage": "https://gitlab.com/upstreamable/drupal-autoservices",
"support": {
"source": "https://gitlab.com/upstreamable/drupal-autoservices",
"issues": "https://gitlab.com/upstreamable/drupal-autoservices/issues"
},
"license": "GPL-2.0-or-later",
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8"
}
],
"require": {
},
"require-dev": {
"composer/installers": "^1",
"cweagans/composer-patches": "~1.0",
"drupal/core-composer-scaffold": "^9.0",
"drupal/core-dev": "^9.0",
"drupal/core-recommended": "^9.0",
"drush/drush": "^10",
"mglaman/phpstan-drupal": "^0.12",
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpstan/phpstan-deprecation-rules": "^0.12",
"zaporylie/composer-drupal-optimizations": "^1.0"
},
"autoload": {
"classmap": [".spoons/ScriptHandler.php"]
},
"config": {
"process-timeout": 36000
},
"scripts": {
"si": "drush si -v --db-url=${SIMPLETEST_DB:-mysql://root:password@mariadb/db}",
"phpcs": "phpcs --runtime-set ignore_warnings_on_exit 1 --runtime-set ignore_errors_on_exit 1 web/modules/custom",
"lint": "parallel-lint --exclude web --exclude vendor .",
"webserver": "cd web && php -S 0.0.0.0:8888 .ht.router.php",
"chromedriver": "chromedriver --port=9515 --verbose --whitelisted-ips --log-path=/tmp/chromedriver.log --no-sandbox",
"unit": "phpunit --verbose web/modules/custom",
"phpstan": "phpstan analyse web/modules/custom",
"stylelint": "yarn --silent --cwd web/core stylelint --formatter verbose --config ./.stylelintrc.json ../modules/custom/**/*.css",
"eslint": "yarn --silent --cwd web/core eslint -c ./.eslintrc.json ../modules/custom",
"post-update-cmd": ["Spoons\\ScriptHandler::createSymlinks"]
},
"extra": {
"installer-paths": {
"web/core": ["type:drupal-core"],
"web/libraries/{$name}": ["type:drupal-library"],
"web/modules/contrib/{$name}": ["type:drupal-module"],
"web/profiles/{$name}": ["type:drupal-profile"],
"web/themes/{$name}": ["type:drupal-theme"],
"drush/{$name}": ["type:drupal-drush"]
},
"drupal-scaffold": {
"locations": {
"web-root": "web/"
}
}
}
}
version: "3.1"
services:
# More info at https://github.com/wodby/php
drupal:
image: wodby/php:${PHP_TAG-7.4-dev}
command: composer webserver
environment:
PHP_SENDMAIL_PATH: /dev/null
COLUMNS: ${COLUMNS-80} # Set 80 columns for docker exec -it.
## Read instructions at https://wodby.com/docs/stacks/drupal/local/#debugging-cli-requests
# The line below is commented out because the mere presence of that env variable loads XDebug regardless of the value.
# Enable XDebug when you `up` your container: PHP_XDEBUG=1 docker-compose up -d
# PHP_XDEBUG:
PHP_XDEBUG_DEFAULT_ENABLE:
PHP_IDE_CONFIG:
PHP_XDEBUG_REMOTE_HOST:
PHP_XDEBUG_REMOTE_CONNECT_BACK:
# Specify 'drupal' instead of 127.0.0.1 so that chrome service can reach it.
SIMPLETEST_BASE_URL: http://drupal:8888
SIMPLETEST_DB:
volumes:
- ./:/var/www/html:cached
- ./.docker/zz-php.ini:/usr/local/etc/php/conf.d/zz-php.ini
# ports:
# - '${WEB_PORT-8889}:8888'
# More info at https://github.com/wodby/mariadb
mariadb:
image: wodby/mariadb:${MARIADB_TAG-10.3}
stop_grace_period: 30s
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mariadb-datavolume:/var/lib/mysql
# https://gorannikolovski.com/blog/docker4drupal-and-functional-javascript-tests
chrome:
image: drupalci/webdriver-chromedriver:production
ulimits:
core:
soft: -1
hard: -1
cap_add:
- SYS_ADMIN
volumes:
- /dev/shm:/dev/shm
entrypoint:
- chromedriver
- "--no-sandbox"
- "--log-path=/tmp/chromedriver.log"
- "--verbose"
- "--whitelisted-ips="
#data volumes https://docs.docker.com/storage/volumes/
volumes:
mariadb-datavolume:
postgres-datavolume:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Module">
<file>.</file>
<arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,md,yml"/>
<config name="drupal_core_version" value="8"/>
<!-- Initially include all Drupal and DrupalPractice sniffs. -->
<rule ref="vendor/drupal/coder/coder_sniffer/Drupal"/>
<rule ref="vendor/drupal/coder/coder_sniffer/DrupalPractice"/>
<!-- Use 's' to print the full sniff name in the report. -->
<!-- A '-' is prefixed to each of these, so s becomes -s, etc. -->
<arg value="s"/>
<arg value="-colors"/>
<arg name='report-width' value='120'/>
<!-- Ignore all files that match these patterns. They are matched against -->
<!-- the full file path and there is an implied wildcard at each end. -->
<!-- Periods must be escaped using \. -->
<exclude-pattern>_ignore</exclude-pattern>
<exclude-pattern>\.patch</exclude-pattern>
<exclude-pattern>interdif</exclude-pattern>
<!-- Examples for how you disable rules you do not like. -->
<!-- Exclude a sniff from running on specific files. -->
<rule ref="Drupal.Files.TxtFileLineLength.TooLong">
<!-- Exclude .md files from the line limit rule. -->
<exclude-pattern>\.md</exclude-pattern>
</rule>
<rule ref="Drupal.Commenting.DocComment.ParamNotFirst">
<!-- Drush commands are most readable with @command at top. -->
<exclude-pattern>Commands\.php</exclude-pattern>
</rule>
<!-- Use a rule, but exclude one of its sniffs from all files. -->
<rule ref="Drupal.Arrays.Array.LongLineDeclaration">
<!-- Method declarations should be exempt. -->
<severity>0</severity>
</rule>
</ruleset>
parameters:
customRulesetUsed: true
reportUnmatchedIgnoredErrors: false
# Ignore phpstan-drupal extension's rules.
ignoreErrors:
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
- '#Plugin definitions cannot be altered.#'
- '#Missing cache backend declaration for performance.#'
- '#Plugin manager has cache backend specified but does not declare cache tags.#'
# Migrate test fixtures kill phpstan, too much PHP.
excludes_analyse:
- */tests/fixtures/*.php
includes:
- vendor/mglaman/phpstan-drupal/extension.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
<?xml version="1.0" encoding="UTF-8"?>
<!-- TODO set checkForUnintentionallyCoveredCode="true" once https://www.drupal.org/node/2626832 is resolved. -->
<!-- PHPUnit expects functional tests to be run with either a privileged user
or your current system user. See core/tests/README.md and
https://www.drupal.org/node/2116263 for details.
-->
<phpunit bootstrap="web/core/tests/bootstrap.php" colors="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
printerClass="\Drupal\Tests\Listeners\HtmlOutputPrinter">
<php>
<!-- Set error reporting to E_ALL. -->
<ini name="error_reporting" value="32767"/>
<!-- Do not limit the amount of memory tests take to run. -->
<ini name="memory_limit" value="-1"/>
<!-- Example SIMPLETEST_BASE_URL value: http://localhost -->
<env name="SIMPLETEST_BASE_URL" value="http://127.0.0.1:8888"/>
<!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
<env name="SIMPLETEST_DB" value="mysql://root:password@mariadb/db"/>
<!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->
<env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/tmp"/>
<!-- To have browsertest output use an alternative base URL. For example if
SIMPLETEST_BASE_URL is an internal DDEV URL, you can set this to the
external DDev URL so you can follow the links directly.
-->
<env name="BROWSERTEST_OUTPUT_BASE_URL" value=""/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/>
<!-- Example for changing the driver class for mink tests MINK_DRIVER_CLASS value: 'Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver' -->
<env name="MINK_DRIVER_CLASS" value=''/>
<!-- Example for changing the driver args to mink tests MINK_DRIVER_ARGS value: '["http://127.0.0.1:8510"]' -->
<env name="MINK_DRIVER_ARGS" value=''/>
<!-- Example for changing the driver args to phantomjs tests MINK_DRIVER_ARGS_PHANTOMJS value: '["http://127.0.0.1:8510"]' -->
<env name="MINK_DRIVER_ARGS_PHANTOMJS" value=''/>
<!-- Example for changing the driver args to webdriver tests MINK_DRIVER_ARGS_WEBDRIVER value: '["chrome", { "chromeOptions": { "w3c": false } }, "http://localhost:4444/wd/hub"]' For using the Firefox browser, replace "chrome" with "firefox" -->
<!-- Example from drupal.org https://dispatcher.drupalci.org/job/drupal_contrib/177251/console-->
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value='["chrome", {"browserName":"chrome","chromeOptions":{"args":["--disable-gpu","--headless"]}}, "http://chrome:9515"]'/>
</php>
<testsuites>
<testsuite name="unit">
<file>./tests/TestSuites/UnitTestSuite.php</file>
</testsuite>
<testsuite name="kernel">
<file>./tests/TestSuites/KernelTestSuite.php</file>
</testsuite>
<testsuite name="functional">
<file>./tests/TestSuites/FunctionalTestSuite.php</file>
</testsuite>
<testsuite name="functional-javascript">
<file>./tests/TestSuites/FunctionalJavascriptTestSuite.php</file>
</testsuite>
<testsuite name="build">
<file>./tests/TestSuites/BuildTestSuite.php</file>
</testsuite>
</testsuites>
<listeners>
<listener class="\Drupal\Tests\Listeners\DrupalListener">
</listener>
<!-- The Symfony deprecation listener has to come after the Drupal listener -->
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
</listener>
</listeners>
</phpunit>
<?php
namespace Drupal\autoservices;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\Finder\Finder;
/**
* Defines a service provider for the Autoservices module.
*/
class AutoservicesServiceProvider extends ServiceProviderBase {
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
$this->aliasInterfaces($container);
foreach ($container->getParameter('container.modules') as $moduleName => $moduleInfo) {
$autoservices = [
'Autoservice' => [
'autowire' => TRUE,
],
'AutoPluginManager' => [
'autowire' => FALSE,
'parent' => 'default_plugin_manager',
],
'AutoEventSubscriber' => [
'autowire' => TRUE,
'tags' => [
['name' => 'event_subscriber'],
],
],
];
foreach ($autoservices as $namespace => $configuration) {
$path = dirname($moduleInfo['pathname']) . '/src/' . $namespace;
if (file_exists($path)) {
$finder = new Finder();
$finder->in($path)->files()->name('*.php');
foreach ($finder as $fileInfo) {
$class = 'Drupal\\' . $moduleName . '\\' . $namespace . '\\' . substr($fileInfo->getFilename(), 0, -4);
if ($container->hasDefinition($class)) {
continue;
}
$definition = isset($configuration['parent']) ? new ChildDefinition($configuration['parent']) : new Definition($class);
$definition->setAutowired($configuration['autowire']);
if (isset($configuration['tags'])) {
foreach ($configuration['tags'] as $tag) {
$definition->addTag($tag['name']);
}
}
$container->setDefinition($class, $definition);
}
}
}
}
}
/**
* Add aliases for all services that implement and obvious interface.
*/
protected function aliasInterfaces(ContainerBuilder $container) {
foreach ($container->getDefinitions() as $definitionName => $definition) {
$definitionInitial = substr($definitionName, 0, 1);
$interfaceCandidate = $definition->getClass() . 'Interface';
if (
$definitionInitial === strtolower($definitionInitial) &&
interface_exists($interfaceCandidate) &&
!$container->hasAlias($interfaceCandidate)
) {
$container->setAlias($interfaceCandidate, $definitionName);
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment