Nano and AOP #5035
-
Is it possible to use AOP with Nano? There are any examples, please? I'm not being able to make it work. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Example:
<?php
return [
'scan' => [
'paths' => [
BASE_PATH . '/app',
],
],
];
<?php
use Hyperf\Di\ClassLoader;
use Hyperf\Nano\Factory\AppFactory;
require_once __DIR__ . '/vendor/autoload.php';
define('BASE_PATH', __DIR__);
ClassLoader::init();
$app = AppFactory::create();
$app->run();
<?php
namespace App\Command;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerInterface;
use Hyperf\Command\Annotation\Command;
#[Command()]
class FooCommand extends \Hyperf\Command\Command
{
protected ?string $signature = "foo";
#[Inject]
protected ContainerInterface $container;
public function handle() { }
} Run [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
Console Tool
Usage:
command [options] [arguments]
Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
completion Dump the shell completion script
foo
help Display help for a command
list List commands
start Start hyperf servers. Run runtime
└── container
├── aspects.cache
├── classes.cache
├── proxy
│ └── App_Command_FooCommand.proxy.php
└── scan.cache
2 directories, 4 files
|
Beta Was this translation helpful? Give feedback.
-
Thank you very much, @huangdijia. |
Beta Was this translation helpful? Give feedback.
-
A fell workarounds, but works like a charm! |
Beta Was this translation helpful? Give feedback.
-
Single file solution: <?php
declare(strict_types=1);
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\ClassLoader;
use Hyperf\Metric\Adapter\Prometheus\Constants;
use Hyperf\Metric\Adapter\Prometheus\MetricFactory;
use Hyperf\Nano\Factory\AppFactory;
use Hyperf\Tracer\Adapter\JaegerTracerFactory;
use Hyperf\Utils\Composer;
use Jaeger\Config;
use Psr\Log\LogLevel;
use const Jaeger\SAMPLER_TYPE_CONST;
define('BASE_PATH', __DIR__);
require_once BASE_PATH . '/vendor/autoload.php';
class ConfigProvider
{
public function __invoke()
{
return [
'app_name' => 'tracing-nano',
'app_env' => 'dev',
'scan_cacheable' => false,
'annotations' => [
'scan' => [
'paths' => [
// BASE_PATH . '/',
],
],
],
StdoutLoggerInterface::class => [
'log_level' => [
LogLevel::ALERT,
LogLevel::CRITICAL,
LogLevel::DEBUG,
LogLevel::EMERGENCY,
LogLevel::ERROR,
LogLevel::INFO,
LogLevel::NOTICE,
LogLevel::WARNING,
],
],
'aspects' => [
// ...
],
'metric' => [
'default' => 'prometheus',
'metric' => [
'prometheus' => [
'driver' => MetricFactory::class,
'mode' => Constants::SCRAPE_MODE,
'namespace' => 'tracing_nano',
'scrape_host' => '0.0.0.0',
'scrape_port' => '9502',
'scrape_path' => '/metrics',
],
],
],
'middlewares' => [
'http' => [
MetricMiddleware::class,
TraceMiddleware::class,
],
],
'opentracing' => [
'default' => 'jaeger',
'enable' => [
'guzzle' => true,
],
'tracer' => [
'jaeger' => [
'driver' => JaegerTracerFactory::class,
'name' => 'tracing-nano',
'options' => [
'sampler' => [
'type' => SAMPLER_TYPE_CONST,
'param' => true,
],
'logging' => true,
'dispatch_mode' => Config::JAEGER_OVER_BINARY_HTTP,
],
],
],
],
];
}
}
// Inject ConfigProvider into Hyperf\Utils\Composer::$extra
Closure::bind(function () {
self::getLockContent();
self::$extra[uniqid()] = [
'hyperf' => [
'config' => ConfigProvider::class,
],
];
}, null, Composer::class)();
ClassLoader::init();
$app = AppFactory::createBase('0.0.0.0', 9501);
$app->addCommand('foo', function () {
var_dump(config('annotations.scan'));
});
$app->run(); |
Beta Was this translation helpful? Give feedback.
Example:
config/autoload/annotations.php
ClassLoader::init()
beforeAppFactory::create()
inapp.php
app/Command/FooCommand