├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── composer.lock ├── phpunit.xml ├── src ├── Container.php └── Exception │ ├── AlreadyInContainerException.php │ ├── ContainerException.php │ └── NotFoundException.php └── tests └── ContainerTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.5 5 | - 5.6 6 | - 7.0 7 | - 7.1 8 | 9 | before_script: 10 | - composer self-update 11 | - composer install --prefer-source --no-interaction --dev 12 | 13 | script: phpunit 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Packagist](https://img.shields.io/packagist/v/anned20/Strix.svg)](https://packagist.org/packages/anned20/strix) 2 | [![Latest Stable Version](https://poser.pugx.org/anned20/strix/v/stable)](https://packagist.org/packages/anned20/strix) 3 | [![Build Status](https://travis-ci.org/anned20/Strix.svg?branch=master)](https://travis-ci.org/anned20/Strix) 4 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/anned20/Strix/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/anned20/Strix/?branch=master) 5 | 6 | # Strix 7 | 8 | Minimal container for modern PHP applications following the PSR-11 standard 9 | 10 | ## Installation 11 | 12 | `composer require anned20/strix` 13 | 14 | ## Usage 15 | 16 | ```php 17 | add('config', ['hello' => 'world']); 30 | 31 | // Use the container for closures 32 | $container->add('function', function () { 33 | return rand(); 34 | }); 35 | 36 | // Let's use the config 37 | $hello = $container->get('config')['hello']; 38 | 39 | // And the function 40 | $rand = $container->get('function')(); 41 | 42 | // Factories can be made too! 43 | $container->add('factory', function () { 44 | return new SomeClass(); 45 | }); 46 | 47 | // Just like services 48 | $myService = new SomeClass(); 49 | 50 | $container->add('service', $myService); 51 | 52 | // Whoops! 53 | $container->add('config', ['foo' => 'bar']); // AlreadyInContainerException thrown 54 | 55 | // Let's check before adding 56 | if (!$container->has('config')) { 57 | $container->add('config', ['foo' => 'bar']); 58 | } 59 | 60 | // But I want to overwrite the old one! No problem! 61 | if ($container->has('config')) { 62 | $container->delete('config'); 63 | } 64 | 65 | $container->add('config', ['foo' => 'bar']); 66 | 67 | // Whoops! 68 | $bye = $container->get('bye'); // NotFoundException thrown 69 | 70 | ``` 71 | 72 | ## Contributing 73 | 74 | 1. Fork it! 75 | 2. Create your feature branch: `git checkout -b my-new-feature` 76 | 3. Commit your changes: `git commit -am 'Add some feature'` 77 | 4. Push to the branch: `git push origin my-new-feature` 78 | 5. Submit a pull request :D 79 | 80 | ## License 81 | 82 | Strix is a PSR-11 compliant container 83 | 84 | Copyright © 2017 Anne Douwe Bouma 85 | 86 | Permission is hereby granted, free of charge, to any person obtaining 87 | a copy of this software and associated documentation files (the "Software"), 88 | to deal in the Software without restriction, including without limitation 89 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 90 | and/or sell copies of the Software, and to permit persons to whom the 91 | Software is furnished to do so, subject to the following conditions: 92 | 93 | The above copyright notice and this permission notice shall be included 94 | in all copies or substantial portions of the Software. 95 | 96 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 97 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 98 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 99 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 100 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 101 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 102 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 103 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "anned20/strix", 3 | "description": "Minimal container for modern PHP applications following the PSR11 standard", 4 | "keywords": ["psr-11", "container", "container-interop", "container-interface", "minimal"], 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Anne Douwe Bouma", 10 | "email": "annedouwebouma@gmail.com" 11 | } 12 | ], 13 | "minimum-stability": "dev", 14 | "require": { 15 | "php": ">= 5.5.0", 16 | "psr/container": "^1.0" 17 | }, 18 | "require-dev": { 19 | "phpunit/phpunit": "4.0.*" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "anned20\\Strix\\": "src" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "74283318176dc04b955888371a522d12", 8 | "packages": [ 9 | { 10 | "name": "psr/container", 11 | "version": "dev-master", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/php-fig/container.git", 15 | "reference": "2cc4a01788191489dc7459446ba832fa79a216a7" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/php-fig/container/zipball/2cc4a01788191489dc7459446ba832fa79a216a7", 20 | "reference": "2cc4a01788191489dc7459446ba832fa79a216a7", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "php": ">=5.3.0" 25 | }, 26 | "type": "library", 27 | "extra": { 28 | "branch-alias": { 29 | "dev-master": "1.0.x-dev" 30 | } 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "Psr\\Container\\": "src/" 35 | } 36 | }, 37 | "notification-url": "https://packagist.org/downloads/", 38 | "license": [ 39 | "MIT" 40 | ], 41 | "authors": [ 42 | { 43 | "name": "PHP-FIG", 44 | "homepage": "http://www.php-fig.org/" 45 | } 46 | ], 47 | "description": "Common Container Interface (PHP FIG PSR-11)", 48 | "homepage": "https://github.com/php-fig/container", 49 | "keywords": [ 50 | "PSR-11", 51 | "container", 52 | "container-interface", 53 | "container-interop", 54 | "psr" 55 | ], 56 | "time": "2017-06-28 15:35:32" 57 | } 58 | ], 59 | "packages-dev": [ 60 | { 61 | "name": "phpunit/php-code-coverage", 62 | "version": "2.0.x-dev", 63 | "source": { 64 | "type": "git", 65 | "url": "https://github.com/sebastianbergmann/php-code-coverage.git", 66 | "reference": "eabb6310071950ecca0f23e8fdb301fd08c99911" 67 | }, 68 | "dist": { 69 | "type": "zip", 70 | "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/eabb6310071950ecca0f23e8fdb301fd08c99911", 71 | "reference": "eabb6310071950ecca0f23e8fdb301fd08c99911", 72 | "shasum": "" 73 | }, 74 | "require": { 75 | "php": ">=5.3.3", 76 | "phpunit/php-file-iterator": "~1.3", 77 | "phpunit/php-text-template": "~1.2", 78 | "phpunit/php-token-stream": "~1.3", 79 | "sebastian/environment": "~1.0", 80 | "sebastian/version": "~1.0" 81 | }, 82 | "require-dev": { 83 | "ext-xdebug": ">=2.1.4", 84 | "phpunit/phpunit": "~4" 85 | }, 86 | "suggest": { 87 | "ext-dom": "*", 88 | "ext-xdebug": ">=2.2.1", 89 | "ext-xmlwriter": "*" 90 | }, 91 | "type": "library", 92 | "extra": { 93 | "branch-alias": { 94 | "dev-master": "2.0.x-dev" 95 | } 96 | }, 97 | "autoload": { 98 | "classmap": [ 99 | "src/" 100 | ] 101 | }, 102 | "notification-url": "https://packagist.org/downloads/", 103 | "license": [ 104 | "BSD-3-Clause" 105 | ], 106 | "authors": [ 107 | { 108 | "name": "Sebastian Bergmann", 109 | "email": "sb@sebastian-bergmann.de", 110 | "role": "lead" 111 | } 112 | ], 113 | "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", 114 | "homepage": "https://github.com/sebastianbergmann/php-code-coverage", 115 | "keywords": [ 116 | "coverage", 117 | "testing", 118 | "xunit" 119 | ], 120 | "time": "2015-05-25 12:54:30" 121 | }, 122 | { 123 | "name": "phpunit/php-file-iterator", 124 | "version": "1.3.4", 125 | "source": { 126 | "type": "git", 127 | "url": "https://github.com/sebastianbergmann/php-file-iterator.git", 128 | "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb" 129 | }, 130 | "dist": { 131 | "type": "zip", 132 | "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/acd690379117b042d1c8af1fafd61bde001bf6bb", 133 | "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb", 134 | "shasum": "" 135 | }, 136 | "require": { 137 | "php": ">=5.3.3" 138 | }, 139 | "type": "library", 140 | "autoload": { 141 | "classmap": [ 142 | "File/" 143 | ] 144 | }, 145 | "notification-url": "https://packagist.org/downloads/", 146 | "include-path": [ 147 | "" 148 | ], 149 | "license": [ 150 | "BSD-3-Clause" 151 | ], 152 | "authors": [ 153 | { 154 | "name": "Sebastian Bergmann", 155 | "email": "sb@sebastian-bergmann.de", 156 | "role": "lead" 157 | } 158 | ], 159 | "description": "FilterIterator implementation that filters files based on a list of suffixes.", 160 | "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", 161 | "keywords": [ 162 | "filesystem", 163 | "iterator" 164 | ], 165 | "time": "2013-10-10T15:34:57+00:00" 166 | }, 167 | { 168 | "name": "phpunit/php-text-template", 169 | "version": "1.2.1", 170 | "source": { 171 | "type": "git", 172 | "url": "https://github.com/sebastianbergmann/php-text-template.git", 173 | "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" 174 | }, 175 | "dist": { 176 | "type": "zip", 177 | "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", 178 | "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", 179 | "shasum": "" 180 | }, 181 | "require": { 182 | "php": ">=5.3.3" 183 | }, 184 | "type": "library", 185 | "autoload": { 186 | "classmap": [ 187 | "src/" 188 | ] 189 | }, 190 | "notification-url": "https://packagist.org/downloads/", 191 | "license": [ 192 | "BSD-3-Clause" 193 | ], 194 | "authors": [ 195 | { 196 | "name": "Sebastian Bergmann", 197 | "email": "sebastian@phpunit.de", 198 | "role": "lead" 199 | } 200 | ], 201 | "description": "Simple template engine.", 202 | "homepage": "https://github.com/sebastianbergmann/php-text-template/", 203 | "keywords": [ 204 | "template" 205 | ], 206 | "time": "2015-06-21T13:50:34+00:00" 207 | }, 208 | { 209 | "name": "phpunit/php-timer", 210 | "version": "dev-master", 211 | "source": { 212 | "type": "git", 213 | "url": "https://github.com/sebastianbergmann/php-timer.git", 214 | "reference": "d107f347d368dd8a384601398280c7c608390ab7" 215 | }, 216 | "dist": { 217 | "type": "zip", 218 | "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/d107f347d368dd8a384601398280c7c608390ab7", 219 | "reference": "d107f347d368dd8a384601398280c7c608390ab7", 220 | "shasum": "" 221 | }, 222 | "require": { 223 | "php": "^5.3.3 || ^7.0" 224 | }, 225 | "require-dev": { 226 | "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" 227 | }, 228 | "type": "library", 229 | "extra": { 230 | "branch-alias": { 231 | "dev-master": "1.0-dev" 232 | } 233 | }, 234 | "autoload": { 235 | "classmap": [ 236 | "src/" 237 | ] 238 | }, 239 | "notification-url": "https://packagist.org/downloads/", 240 | "license": [ 241 | "BSD-3-Clause" 242 | ], 243 | "authors": [ 244 | { 245 | "name": "Sebastian Bergmann", 246 | "email": "sb@sebastian-bergmann.de", 247 | "role": "lead" 248 | } 249 | ], 250 | "description": "Utility class for timing", 251 | "homepage": "https://github.com/sebastianbergmann/php-timer/", 252 | "keywords": [ 253 | "timer" 254 | ], 255 | "time": "2017-03-07 15:42:04" 256 | }, 257 | { 258 | "name": "phpunit/php-token-stream", 259 | "version": "1.4.x-dev", 260 | "source": { 261 | "type": "git", 262 | "url": "https://github.com/sebastianbergmann/php-token-stream.git", 263 | "reference": "958103f327daef5dd0bb328dec53e0a9e43cfaf7" 264 | }, 265 | "dist": { 266 | "type": "zip", 267 | "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/958103f327daef5dd0bb328dec53e0a9e43cfaf7", 268 | "reference": "958103f327daef5dd0bb328dec53e0a9e43cfaf7", 269 | "shasum": "" 270 | }, 271 | "require": { 272 | "ext-tokenizer": "*", 273 | "php": ">=5.3.3" 274 | }, 275 | "require-dev": { 276 | "phpunit/phpunit": "~4.2" 277 | }, 278 | "type": "library", 279 | "extra": { 280 | "branch-alias": { 281 | "dev-master": "1.4-dev" 282 | } 283 | }, 284 | "autoload": { 285 | "classmap": [ 286 | "src/" 287 | ] 288 | }, 289 | "notification-url": "https://packagist.org/downloads/", 290 | "license": [ 291 | "BSD-3-Clause" 292 | ], 293 | "authors": [ 294 | { 295 | "name": "Sebastian Bergmann", 296 | "email": "sebastian@phpunit.de" 297 | } 298 | ], 299 | "description": "Wrapper around PHP's tokenizer extension.", 300 | "homepage": "https://github.com/sebastianbergmann/php-token-stream/", 301 | "keywords": [ 302 | "tokenizer" 303 | ], 304 | "time": "2017-03-07 08:21:50" 305 | }, 306 | { 307 | "name": "phpunit/phpunit", 308 | "version": "4.0.x-dev", 309 | "source": { 310 | "type": "git", 311 | "url": "https://github.com/sebastianbergmann/phpunit.git", 312 | "reference": "c4051e4b2bf52ba8be49c1eee67660e573323cf4" 313 | }, 314 | "dist": { 315 | "type": "zip", 316 | "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c4051e4b2bf52ba8be49c1eee67660e573323cf4", 317 | "reference": "c4051e4b2bf52ba8be49c1eee67660e573323cf4", 318 | "shasum": "" 319 | }, 320 | "require": { 321 | "ext-dom": "*", 322 | "ext-json": "*", 323 | "ext-pcre": "*", 324 | "ext-reflection": "*", 325 | "ext-spl": "*", 326 | "php": ">=5.3.3", 327 | "phpunit/php-code-coverage": ">=2.0.0,<2.1.0", 328 | "phpunit/php-file-iterator": "~1.3.1", 329 | "phpunit/php-text-template": "~1.2", 330 | "phpunit/php-timer": "~1.0.2", 331 | "phpunit/phpunit-mock-objects": ">=2.0.0,<2.1.0", 332 | "sebastian/diff": "~1.1", 333 | "sebastian/environment": "~1.0", 334 | "sebastian/exporter": "~1.0.1", 335 | "sebastian/version": "~1.0.3", 336 | "symfony/yaml": "~2.0" 337 | }, 338 | "suggest": { 339 | "phpunit/php-invoker": "~1.1" 340 | }, 341 | "bin": [ 342 | "phpunit" 343 | ], 344 | "type": "library", 345 | "extra": { 346 | "branch-alias": { 347 | "dev-master": "4.0.x-dev" 348 | } 349 | }, 350 | "autoload": { 351 | "classmap": [ 352 | "src/" 353 | ] 354 | }, 355 | "notification-url": "https://packagist.org/downloads/", 356 | "include-path": [ 357 | "", 358 | "../../symfony/yaml/" 359 | ], 360 | "license": [ 361 | "BSD-3-Clause" 362 | ], 363 | "authors": [ 364 | { 365 | "name": "Sebastian Bergmann", 366 | "email": "sebastian@phpunit.de", 367 | "role": "lead" 368 | } 369 | ], 370 | "description": "The PHP Unit Testing framework.", 371 | "homepage": "http://www.phpunit.de/", 372 | "keywords": [ 373 | "phpunit", 374 | "testing", 375 | "xunit" 376 | ], 377 | "time": "2015-02-09 06:32:56" 378 | }, 379 | { 380 | "name": "phpunit/phpunit-mock-objects", 381 | "version": "2.0.x-dev", 382 | "source": { 383 | "type": "git", 384 | "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", 385 | "reference": "e60bb929c50ae4237aaf680a4f6773f4ee17f0a2" 386 | }, 387 | "dist": { 388 | "type": "zip", 389 | "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/e60bb929c50ae4237aaf680a4f6773f4ee17f0a2", 390 | "reference": "e60bb929c50ae4237aaf680a4f6773f4ee17f0a2", 391 | "shasum": "" 392 | }, 393 | "require": { 394 | "php": ">=5.3.3", 395 | "phpunit/php-text-template": "~1.2" 396 | }, 397 | "require-dev": { 398 | "phpunit/phpunit": ">=4.0.0,<4.1.0" 399 | }, 400 | "suggest": { 401 | "ext-soap": "*" 402 | }, 403 | "type": "library", 404 | "extra": { 405 | "branch-alias": { 406 | "dev-master": "2.0.x-dev" 407 | } 408 | }, 409 | "autoload": { 410 | "classmap": [ 411 | "src/" 412 | ] 413 | }, 414 | "notification-url": "https://packagist.org/downloads/", 415 | "include-path": [ 416 | "" 417 | ], 418 | "license": [ 419 | "BSD-3-Clause" 420 | ], 421 | "authors": [ 422 | { 423 | "name": "Sebastian Bergmann", 424 | "email": "sb@sebastian-bergmann.de", 425 | "role": "lead" 426 | } 427 | ], 428 | "description": "Mock Object library for PHPUnit", 429 | "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", 430 | "keywords": [ 431 | "mock", 432 | "xunit" 433 | ], 434 | "time": "2014-06-12 07:19:48" 435 | }, 436 | { 437 | "name": "sebastian/diff", 438 | "version": "1.4.x-dev", 439 | "source": { 440 | "type": "git", 441 | "url": "https://github.com/sebastianbergmann/diff.git", 442 | "reference": "7f066a26a962dbe58ddea9f72a4e82874a3975a4" 443 | }, 444 | "dist": { 445 | "type": "zip", 446 | "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/7f066a26a962dbe58ddea9f72a4e82874a3975a4", 447 | "reference": "7f066a26a962dbe58ddea9f72a4e82874a3975a4", 448 | "shasum": "" 449 | }, 450 | "require": { 451 | "php": "^5.3.3 || ^7.0" 452 | }, 453 | "require-dev": { 454 | "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" 455 | }, 456 | "type": "library", 457 | "extra": { 458 | "branch-alias": { 459 | "dev-master": "1.4-dev" 460 | } 461 | }, 462 | "autoload": { 463 | "classmap": [ 464 | "src/" 465 | ] 466 | }, 467 | "notification-url": "https://packagist.org/downloads/", 468 | "license": [ 469 | "BSD-3-Clause" 470 | ], 471 | "authors": [ 472 | { 473 | "name": "Kore Nordmann", 474 | "email": "mail@kore-nordmann.de" 475 | }, 476 | { 477 | "name": "Sebastian Bergmann", 478 | "email": "sebastian@phpunit.de" 479 | } 480 | ], 481 | "description": "Diff implementation", 482 | "homepage": "https://github.com/sebastianbergmann/diff", 483 | "keywords": [ 484 | "diff" 485 | ], 486 | "time": "2017-05-22 07:24:03" 487 | }, 488 | { 489 | "name": "sebastian/environment", 490 | "version": "1.3.x-dev", 491 | "source": { 492 | "type": "git", 493 | "url": "https://github.com/sebastianbergmann/environment.git", 494 | "reference": "67f55699c2810ff0f2cc47478bbdeda8567e68ee" 495 | }, 496 | "dist": { 497 | "type": "zip", 498 | "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/67f55699c2810ff0f2cc47478bbdeda8567e68ee", 499 | "reference": "67f55699c2810ff0f2cc47478bbdeda8567e68ee", 500 | "shasum": "" 501 | }, 502 | "require": { 503 | "php": "^5.3.3 || ^7.0" 504 | }, 505 | "require-dev": { 506 | "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" 507 | }, 508 | "type": "library", 509 | "extra": { 510 | "branch-alias": { 511 | "dev-master": "1.3.x-dev" 512 | } 513 | }, 514 | "autoload": { 515 | "classmap": [ 516 | "src/" 517 | ] 518 | }, 519 | "notification-url": "https://packagist.org/downloads/", 520 | "license": [ 521 | "BSD-3-Clause" 522 | ], 523 | "authors": [ 524 | { 525 | "name": "Sebastian Bergmann", 526 | "email": "sebastian@phpunit.de" 527 | } 528 | ], 529 | "description": "Provides functionality to handle HHVM/PHP environments", 530 | "homepage": "http://www.github.com/sebastianbergmann/environment", 531 | "keywords": [ 532 | "Xdebug", 533 | "environment", 534 | "hhvm" 535 | ], 536 | "time": "2017-02-28 08:18:59" 537 | }, 538 | { 539 | "name": "sebastian/exporter", 540 | "version": "1.0.2", 541 | "source": { 542 | "type": "git", 543 | "url": "https://github.com/sebastianbergmann/exporter.git", 544 | "reference": "c7d59948d6e82818e1bdff7cadb6c34710eb7dc0" 545 | }, 546 | "dist": { 547 | "type": "zip", 548 | "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/c7d59948d6e82818e1bdff7cadb6c34710eb7dc0", 549 | "reference": "c7d59948d6e82818e1bdff7cadb6c34710eb7dc0", 550 | "shasum": "" 551 | }, 552 | "require": { 553 | "php": ">=5.3.3" 554 | }, 555 | "require-dev": { 556 | "phpunit/phpunit": "~4.0" 557 | }, 558 | "type": "library", 559 | "extra": { 560 | "branch-alias": { 561 | "dev-master": "1.0.x-dev" 562 | } 563 | }, 564 | "autoload": { 565 | "classmap": [ 566 | "src/" 567 | ] 568 | }, 569 | "notification-url": "https://packagist.org/downloads/", 570 | "license": [ 571 | "BSD-3-Clause" 572 | ], 573 | "authors": [ 574 | { 575 | "name": "Jeff Welch", 576 | "email": "whatthejeff@gmail.com" 577 | }, 578 | { 579 | "name": "Volker Dusch", 580 | "email": "github@wallbash.com" 581 | }, 582 | { 583 | "name": "Bernhard Schussek", 584 | "email": "bschussek@2bepublished.at" 585 | }, 586 | { 587 | "name": "Sebastian Bergmann", 588 | "email": "sebastian@phpunit.de" 589 | }, 590 | { 591 | "name": "Adam Harvey", 592 | "email": "aharvey@php.net" 593 | } 594 | ], 595 | "description": "Provides the functionality to export PHP variables for visualization", 596 | "homepage": "http://www.github.com/sebastianbergmann/exporter", 597 | "keywords": [ 598 | "export", 599 | "exporter" 600 | ], 601 | "time": "2014-09-10T00:51:36+00:00" 602 | }, 603 | { 604 | "name": "sebastian/version", 605 | "version": "1.0.6", 606 | "source": { 607 | "type": "git", 608 | "url": "https://github.com/sebastianbergmann/version.git", 609 | "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6" 610 | }, 611 | "dist": { 612 | "type": "zip", 613 | "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", 614 | "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", 615 | "shasum": "" 616 | }, 617 | "type": "library", 618 | "autoload": { 619 | "classmap": [ 620 | "src/" 621 | ] 622 | }, 623 | "notification-url": "https://packagist.org/downloads/", 624 | "license": [ 625 | "BSD-3-Clause" 626 | ], 627 | "authors": [ 628 | { 629 | "name": "Sebastian Bergmann", 630 | "email": "sebastian@phpunit.de", 631 | "role": "lead" 632 | } 633 | ], 634 | "description": "Library that helps with managing the version number of Git-hosted PHP projects", 635 | "homepage": "https://github.com/sebastianbergmann/version", 636 | "time": "2015-06-21T13:59:46+00:00" 637 | }, 638 | { 639 | "name": "symfony/yaml", 640 | "version": "2.8.x-dev", 641 | "source": { 642 | "type": "git", 643 | "url": "https://github.com/symfony/yaml.git", 644 | "reference": "effdc4dcc292af49c3e3944db313e29f4bb4ffcf" 645 | }, 646 | "dist": { 647 | "type": "zip", 648 | "url": "https://api.github.com/repos/symfony/yaml/zipball/effdc4dcc292af49c3e3944db313e29f4bb4ffcf", 649 | "reference": "effdc4dcc292af49c3e3944db313e29f4bb4ffcf", 650 | "shasum": "" 651 | }, 652 | "require": { 653 | "php": ">=5.3.9" 654 | }, 655 | "type": "library", 656 | "extra": { 657 | "branch-alias": { 658 | "dev-master": "2.8-dev" 659 | } 660 | }, 661 | "autoload": { 662 | "psr-4": { 663 | "Symfony\\Component\\Yaml\\": "" 664 | }, 665 | "exclude-from-classmap": [ 666 | "/Tests/" 667 | ] 668 | }, 669 | "notification-url": "https://packagist.org/downloads/", 670 | "license": [ 671 | "MIT" 672 | ], 673 | "authors": [ 674 | { 675 | "name": "Fabien Potencier", 676 | "email": "fabien@symfony.com" 677 | }, 678 | { 679 | "name": "Symfony Community", 680 | "homepage": "https://symfony.com/contributors" 681 | } 682 | ], 683 | "description": "Symfony Yaml Component", 684 | "homepage": "https://symfony.com", 685 | "time": "2017-09-15 16:59:28" 686 | } 687 | ], 688 | "aliases": [], 689 | "minimum-stability": "dev", 690 | "stability-flags": [], 691 | "prefer-stable": false, 692 | "prefer-lowest": false, 693 | "platform": { 694 | "php": ">= 5.3.0" 695 | }, 696 | "platform-dev": [] 697 | } 698 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Container.php: -------------------------------------------------------------------------------- 1 | has($id)) { 28 | throw new NotFoundException(sprintf('"%s" is not in the container', $id)); 29 | } 30 | 31 | return $this->entries[$id]; 32 | } 33 | 34 | /** 35 | * {@inheritdoc} 36 | */ 37 | public function has($id) 38 | { 39 | return array_key_exists($id, $this->entries); 40 | } 41 | 42 | /** 43 | * Add entry to container 44 | * 45 | * @param string $id 46 | * @param mixed $entry 47 | * 48 | * @return Container 49 | */ 50 | public function add($id, $entry) 51 | { 52 | if ($this->has($id)) { 53 | throw new AlreadyInContainerException(sprintf('Container already has entry with id "%s"', $id)); 54 | } 55 | 56 | $this->entries[$id] = $entry; 57 | 58 | return $this; 59 | } 60 | 61 | /** 62 | * Delete entry in container 63 | * 64 | * @param string $id 65 | * 66 | * @return Container 67 | */ 68 | public function delete($id) 69 | { 70 | if (!$this->has($id)) { 71 | throw new NotFoundException(sprintf('"%s" is not in the container', $id)); 72 | } 73 | 74 | unset($this->entries[$id]); 75 | 76 | return $this; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/Exception/AlreadyInContainerException.php: -------------------------------------------------------------------------------- 1 | add('config', ['hello' => 'world']); 20 | 21 | $this->assertTrue($container->has('config')); 22 | $this->assertFalse($container->has('nope')); 23 | 24 | $this->assertEquals(['hello' => 'world'], $container->get('config')); 25 | 26 | $container->delete('config'); 27 | 28 | $this->assertFalse($container->has('config')); 29 | } 30 | 31 | public function testContainerNotFoundException() 32 | { 33 | $container = new Container(); 34 | 35 | $this->setExpectedException(NotFoundException::class); 36 | 37 | $container->get('nope'); 38 | } 39 | 40 | public function testContainerAlreadyInContainerException() 41 | { 42 | $container = new Container(); 43 | 44 | $this->setExpectedException(AlreadyInContainerException::class); 45 | 46 | $container->add('nope', 'Hi mom!'); 47 | $container->add('nope', 'Hi again!'); 48 | } 49 | } 50 | --------------------------------------------------------------------------------