Skip to content

Commit

Permalink
Merge 55eeb5d into 5f8f219
Browse files Browse the repository at this point in the history
  • Loading branch information
Dropelikeit committed Feb 3, 2023
2 parents 5f8f219 + 55eeb5d commit fa0bef1
Show file tree
Hide file tree
Showing 76 changed files with 3,324 additions and 0 deletions.
138 changes: 138 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,138 @@
name: "CI Tests"

on:
pull_request:
push:

jobs:
php8:
name: PHP 8
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: "actions/checkout@v3"
with:
fetch-depth: 2

- name: "Install PHP 8"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8"

- name: "Cache composer packages"
uses: "actions/cache@v3"
with:
path: "~/.composer/cache"
key: "php-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-composer-locked-"

- name: "Install dependencies with composer"
run: "composer install --no-interaction --prefer-dist"

- name: "Run unit tests"
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer test-unit
composer global require php-coveralls/php-coveralls
php-coveralls --coverage_clover=build/output/tests/coverage.xml --json_path=build/output/tests/coveralls-upload.json -v
- name: "Run acceptance tests"
run: composer test-acceptance

- name: "Run PHP CS Check"
run: "composer cs-check"

- name: "Run Psalm"
run: "composer psalm"

- name: "PHP Lint"
run: "composer lint"

php81:
name: PHP 8.1
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: "actions/checkout@v3"
with:
fetch-depth: 2

- name: "Install PHP 8.1"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"

- name: "Cache composer packages"
uses: "actions/cache@v3"
with:
path: "~/.composer/cache"
key: "php-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-composer-locked-"

- name: "Install dependencies with composer"
run: "composer install --no-interaction --prefer-dist"

- name: "Run unit tests"
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer test-unit
composer global require php-coveralls/php-coveralls
php-coveralls --coverage_clover=build/output/tests/coverage.xml --json_path=build/output/tests/coveralls-upload.json -v
- name: "Run acceptance tests"
run: composer test-acceptance

- name: "Run PHP CS Check"
run: "composer cs-check"

- name: "Run Psalm"
run: "composer psalm"

- name: "PHP Lint"
run: "composer lint"

php82:
name: PHP 8.2
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: "actions/checkout@v3"
with:
fetch-depth: 2

- name: "Install PHP 8.2"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.2"

- name: "Cache composer packages"
uses: "actions/cache@v3"
with:
path: "~/.composer/cache"
key: "php-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-composer-locked-"

- name: "Install dependencies with composer"
run: "composer install --no-interaction --prefer-dist"

- name: "Run unit tests"
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer test-unit
composer global require php-coveralls/php-coveralls
php-coveralls --coverage_clover=build/output/tests/coverage.xml --json_path=build/output/tests/coveralls-upload.json -v
- name: "Run acceptance tests"
run: composer test-acceptance

- name: "Run PHP CS Check"
run: "PHP_CS_FIXER_IGNORE_ENV=1 composer cs-check"

- name: "Run Psalm"
run: "composer psalm"

- name: "PHP Lint"
run: "composer lint"
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
.idea
.phpunit.result.cache
/vendor/
composer.lock
/bin/
/tmp/
/html/
79 changes: 79 additions & 0 deletions .php-cs-fixer.php
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
;

$config = new PhpCsFixer\Config();
return $config
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'braces' => false,
'array_indentation' => true,
'array_syntax' => [
'syntax' => 'short',
],
'blank_line_after_opening_tag' => false,
'blank_line_before_statement' => true,
'cast_spaces' => true,
'concat_space' => [
'spacing' => 'one',
],
'declare_equal_normalize' => true,
'include' => true,
'function_typehint_space' => true,
'linebreak_after_opening_tag' => true,
'lowercase_cast' => true,
'lowercase_static_reference' => true,
'magic_constant_casing' => true,
'modernize_types_casting' => true,
'native_function_casing' => true,
'new_with_braces' => true,
'no_alternative_syntax' => true,
'no_empty_comment' => true,
'no_empty_statement' => true,
'no_closing_tag' => true,
'no_extra_blank_lines' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_multiline_whitespace_around_double_arrow' => true,
'no_null_property_initialization' => true,
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_around_offset' => true,
'no_superfluous_elseif' => true,
'no_trailing_comma_in_singleline' => true,
'no_unneeded_control_parentheses' => true,
'no_unneeded_curly_braces' => true,
'no_unneeded_final_method' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'normalize_index_brace' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => true,
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
'phpdoc_no_access' => true,
'return_type_declaration' => true,
'short_scalar_cast' => true,
'simplified_null_return' => true,
'single_blank_line_before_namespace' => true,
'single_quote' => true,
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
'ternary_to_null_coalescing' => true,
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => true,
'trailing_comma_in_multiline' => true,
'yoda_style' => false,

/** @risky */
'strict_comparison' => true,
'dir_constant' => true,
])->setFinder($finder);
62 changes: 62 additions & 0 deletions README.md
@@ -0,0 +1,62 @@
PHP DI Container
----------------

A lightweight dependency injection container that is decoupled from any framework.
This library can be used for production, but I recommend that it is better to use a di-container from a larger community like Symfony, Laravel or Laminas.

This library was developed for fun, but is supported if someone uses it.

This project can also be used as an example to show how a modern DI container works in a simplified way.

---------------

How it works

```bash
composer require marcel-strahl/container
```

Written for PHP >= 8.0

This library use the PSR container.

--------------

Usage:

```php
<?php

use MarcelStrahl\Container\FileLoader\AdapterBuilder;
use MarcelStrahl\Container\FileLoader\PHPArrayAdapter;
use MarcelStrahl\Container\ObjectBuilder\ObjectBuilderFactory;
use MarcelStrahl\Container\Delegator\ObjectDelegator;
use MarcelStrahl\Container\ClassContainer;
use MarcelStrahl\Container\ObjectContainer;
use MarcelStrahl\Container\AppContainer;
use MarcelStrahl\Container\Dto\ObjectStore;
use MarcelStrahl\Tests\Unit\FileLoader\data\PhpArrayLoaderClassDummy;

$adapter = (new AdapterBuilder())->build(PHPArrayAdapter::class);

$path = sprintf('%s/../Unit/FileLoader/php_array_config.php', __DIR__);
Assert::stringNotEmpty($path);

$builderFactory = new ObjectBuilderFactory();
$delegator = new ObjectDelegator($builderFactory);

$classStore = $adapter->loadFileFromPath($path, ClassStore::create());

$classContainer = ClassContainer::create($classStore);
$classContainer->compile();

$objectContainer = new ObjectContainer(ObjectStore::create(), $delegator);

$app = AppContainer::initialize($classContainer, $objectContainer);
$builderFactory->setContainer($app);

// Insert `$app` into your application context
```



49 changes: 49 additions & 0 deletions composer.json
@@ -0,0 +1,49 @@
{
"name": "marcel-strahl/container",
"description": "A simple and clean PHP container based on PSR-11",
"keywords": ["psr-11", "11", "psr", "container", "PHP", "php", "DI", "di", "dependency injection", "dependency", "injection"],
"minimum-stability": "stable",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Marcel Strahl",
"email": "info@marcel-strahl.de"
}
],
"scripts": {
"psalm": "psalm --no-cache",
"lint": "parallel-lint --exclude .git --exclude vendor bin .",
"cs-check": "php-cs-fixer -v --dry-run --using-cache=no fix",
"cs-fix": "php-cs-fixer --using-cache=no fix",
"test-unit": "export XDEBUG_MODE=coverage && phpunit --configuration phpunit.xml.dist --testsuite Unit",
"test-acceptance": "export XDEBUG_MODE=coverage && phpunit --configuration phpunit.xml.dist --testsuite Acceptance"
},
"config":{
"bin-dir": "bin"
},
"prefer-stable": true,
"autoload": {
"psr-4": {
"MarcelStrahl\\Container\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"MarcelStrahl\\Tests\\": "tests/"
}
},
"require": {
"php": "^8.0",
"psr/container": "^2.0",
"webmozart/assert": "^1.11"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"roave/security-advisories": "dev-latest",
"friendsofphp/php-cs-fixer": "^3.13",
"php-parallel-lint/php-parallel-lint": "^1.3",
"vimeo/psalm": "^5.6",
"psalm/plugin-phpunit": "^0.18.4"
}
}
25 changes: 25 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" beStrictAboutCoversAnnotation="true" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutTodoAnnotatedTests="true" verbose="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory>./bin</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
<report>
<clover outputFile="build/output/tests/coverage.xml"/>
<text outputFile="php://stdout" showUncoveredFiles="false"/>
</report>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
<testsuite name="Acceptance">
<directory suffix="Test.php">tests/Acceptance</directory>
</testsuite>
</testsuites>
</phpunit>
19 changes: 19 additions & 0 deletions psalm.xml
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<psalm
errorLevel="6"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
>
<projectFiles>
<directory name="src"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
</psalm>

0 comments on commit fa0bef1

Please sign in to comment.