.gitattributes000064400000000054151360553400007440 0ustar00/tests export-ignore /.github export-ignore .gitignore000064400000000044151360553400006534 0ustar00/vendor/ composer.lock *.cache *.log.php-cs-fixer.php000064400000005363151360553400007651 0ustar00setRiskyAllowed(true) ->setRules([ '@PSR2' => true, '@Symfony' => true, '@DoctrineAnnotation' => true, '@PhpCsFixer' => true, 'header_comment' => [ 'comment_type' => 'PHPDoc', 'header' => $header, 'separate' => 'none', 'location' => 'after_declare_strict', ], 'array_syntax' => [ 'syntax' => 'short' ], 'list_syntax' => [ 'syntax' => 'short' ], 'concat_space' => [ 'spacing' => 'one' ], 'blank_line_before_statement' => [ 'statements' => [ 'declare', ], ], 'general_phpdoc_annotation_remove' => [ 'annotations' => [ 'author' ], ], 'ordered_imports' => [ 'imports_order' => [ 'class', 'function', 'const', ], 'sort_algorithm' => 'alpha', ], 'single_line_comment_style' => [ 'comment_types' => [ ], ], 'yoda_style' => [ 'always_move_variable' => false, 'equal' => false, 'identical' => false, ], 'phpdoc_align' => [ 'align' => 'left', ], 'multiline_whitespace_before_semicolons' => [ 'strategy' => 'no_multi_line', ], 'constant_case' => [ 'case' => 'lower', ], 'global_namespace_import' => [ 'import_classes' => true, 'import_constants' => true, 'import_functions' => true, ], 'class_attributes_separation' => true, 'combine_consecutive_unsets' => true, 'declare_strict_types' => true, 'linebreak_after_opening_tag' => true, 'lowercase_static_reference' => true, 'no_useless_else' => true, 'no_unused_imports' => true, 'not_operator_with_successor_space' => true, 'not_operator_with_space' => false, 'ordered_class_elements' => true, 'php_unit_strict' => false, 'phpdoc_separation' => false, 'single_quote' => true, 'standardize_not_equals' => true, 'multiline_comment_opening_closing' => true, ]) ->setFinder( PhpCsFixer\Finder::create() ->exclude('bin') ->exclude('public') ->exclude('runtime') ->exclude('vendor') ->in(__DIR__) ) ->setUsingCache(false); .phpstorm.meta.php000064400000000164151360553400010137 0ustar00set(TranslatorLoaderInterface::class, function () use ($container) { $config = $container->get(ConfigInterface::class); $files = $container->get(Filesystem::class); $path = $config->get('translation.path'); return make(FileLoader::class, compact('files', 'path')); }); } } ``` ```php set(TranslatorInterface::class, function () use ($container) { $config = $container->get(ConfigInterface::class); $locale = $config->get('translation.locale'); $fallbackLocale = $config->get('translation.fallback_locale'); $loader = $container->get(TranslatorLoaderInterface::class); $translator = make(Translator::class, compact('loader', 'locale')); $translator->setFallback((string) $fallbackLocale); return $translator; }); } } ``` 3. `EasySwoole` 事件注册器在 `EasySwooleEvent.php` 中,所以我们需要在 `initialize()` 中初始化我们的容器和国际化组件。 > 以下 Config 组件,可以自行封装,这里方便起见直接配置。 ```php set(ConfigInterface::class, new Config([ 'translation' => [ 'locale' => 'zh_CN', 'fallback_locale' => 'en', 'path' => EASYSWOOLE_ROOT . '/storage/languages', ], ])); } } ``` 4. 修改控制器,使用国际化组件 ```php get(TranslatorInterface::class); $data = [ 'message' => $translator->trans('message.hello', ['name' => 'Hyperf']), ]; $this->response()->write(Json::encode($data)); } } ``` 5. 添加国际化配置 ```php // storage/languages/en/message.php return [ 'hello' => 'Hello :name', ]; // storage/languages/zh_CN/message.php return [ 'hello' => '你好 :name', ]; ``` 6. 测试 ``` $ curl http://127.0.0.1:9501/ {"message":"你好 Hyperf"} ``` composer.json000064400000002377151360553400007301 0ustar00{ "name": "hyperf/pimple", "type": "library", "license": "MIT", "keywords": [ "php", "hyperf", "container", "psr11" ], "description": "Pimple Container", "autoload": { "psr-4": { "Hyperf\\Pimple\\": "src/" } }, "autoload-dev": { "psr-4": { "HyperfTest\\": "tests" } }, "require": { "php": ">=8.0", "hyperf/context": "^3.0", "hyperf/contract": "^3.0", "pimple/pimple": "^3.3" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.0", "hyperf/support": "^3.0", "mockery/mockery": "^1.3", "phpstan/phpstan": "^1.0", "phpunit/phpunit": ">=7.0" }, "minimum-stability": "dev", "prefer-stable": true, "config": { "optimize-autoloader": true, "sort-packages": true }, "scripts": { "test": "phpunit -c phpunit.xml --colors=always", "analyse": "phpstan analyse --memory-limit 300M -l 0 ./src", "cs-fix": "php-cs-fixer fix $1" }, "extra": { "branch-alias": { "dev-master": "2.0-dev" }, "hyperf": { "config": "Hyperf\\Pimple\\ConfigProvider" } } } phpunit.xml000064400000000755151360553400006766 0ustar00 ./tests/ src/ConfigProvider.php000064400000001177151360553400010774 0ustar00 [ ], 'commands' => [ ], 'annotations' => [ 'scan' => [ 'paths' => [ __DIR__, ], ], ], ]; } } src/Container.php000064400000007753151360553400010004 0ustar00pimple[ContainerInterface::class] = $this; $this->pimple[PsrContainerInterface::class] = $this; } public function get($id) { if ($this->has($id)) { return $this->pimple[$id]; } return $this->pimple[$id] = $this->make($id); } public function has($id): bool { return isset($this->pimple[$id]); } public function make(string $name, array $parameters = []) { if (! class_exists($name)) { throw new NotFoundException("Entry {$name} is not found."); } $ref = $this->reflection[$name] ?? new ReflectionClass($name); $constructor = $ref->getConstructor(); $args = []; if ($constructor && $constructor->isPublic()) { $args = $this->resolveParameters($constructor, $parameters); } $instance = new $name(...$args); $this->reflection[$name] = $ref; return $instance; } public function set(string $name, $entry): void { $this->pimple[$name] = $entry; } public function define(string $name, $definition): void { throw new NotSupportException('Method define is not support.'); } public function unbind(string $name): void { $this->pimple[$name] = null; } protected function resolveParameters(ReflectionMethod $method, $parameters = []) { $args = []; foreach ($method->getParameters() as $index => $parameter) { if (array_key_exists($parameter->getName(), $parameters)) { $value = $parameters[$parameter->getName()]; } elseif (array_key_exists($index, $parameters)) { $value = $parameters[$index]; } elseif ($parameter->getType() && $this->has($parameter->getType()->getName())) { $value = $this->get($parameter->getType()->getName()); } else { if ($parameter->isDefaultValueAvailable() || $parameter->isOptional()) { $value = $this->getParameterDefaultValue($parameter, $method); } else { throw new InvalidDefinitionException(sprintf( 'Parameter $%s of %s has no value defined or guessable', $parameter->getName(), $this->getFunctionName($method) )); } } $args[] = $value; } return $args; } protected function getParameterDefaultValue(ReflectionParameter $parameter, ReflectionMethod $function) { try { return $parameter->getDefaultValue(); } catch (ReflectionException $e) { throw new InvalidDefinitionException(sprintf( 'The parameter "%s" of %s has no type defined or guessable. It has a default value, ' . 'but the default value can\'t be read through Reflection because it is a PHP internal class.', $parameter->getName(), $this->getFunctionName($function) )); } } private function getFunctionName(ReflectionMethod $method): string { return $method->getName() . '()'; } } src/ContainerFactory.php000064400000001420151360553400011315 0ustar00providers as $provider) { /** @var ProviderInterface $instance */ $instance = new $provider(); $instance->register($container); } return ApplicationContext::setContainer($container); } } src/Exception/InvalidDefinitionException.php000064400000000657151360553400015272 0ustar00