交响申请中的全局功能

发布于 2025-02-13 15:58:48 字数 1934 浏览 1 评论 0原文

我认为这很容易。对于故障排除,我希望从应用程序中的任何地方调用我的_logger()函数。

function _logger(?string $msg=null, int $offset=0, bool $time=false):void
{
    $d1 = debug_backtrace()[$offset+1];
    $d2 = debug_backtrace()[$offset+2];
    syslog(LOG_INFO, sprintf('_logger%s: %s::%s(%s) called by %s::%s on line %s%s', $time?'('.date("Y-m-d H:i:s").')':'', $d1['class'], $d1['function'], json_encode($d1['args']), $d2['class'], $d2['function'], $d2['line'], $msg?' | '.$msg:''));
}

首先,我尝试将其添加到index.php中。直接在主体中添加时,我会遇到服务器错误,并且要么有错误报告,因此无法获得任何信息(只是好奇,但有人知道为什么吗?)。然后,我将其放入闭合功能中,仅适用于HTTP请求。

<?php

use App\Kernel;

// Added my logger() function here resulted in unexplained server error.

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
    // Added my logger() function here works
    return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

然后,我尝试将其添加到kernal.php中,并且似乎可以工作,但必须称为\ app \ _logger()

<?php

declare(strict_types=1);

namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;
}
if(!function_exists('_logger')) {
    // Added my logger() function here works but must be called as \App\_logger()
}

因此,然后我创建了一个名为_logger.php的新文件,该文件有时可以工作,但有时会收到以下错误,并且需要清除缓存以使其再次工作。

<?php

declare(strict_types=1);

if(!function_exists('_logger')) {
    // Added my logger() function sometimes works but other times doesn't
}

期望在文件中找到类“ app_logger” 从资源导入服务时,“/var/www/src/_logger.php” “ ../ src/”,但找不到!检查使用的名称空间前缀 /var/www/config/services.yaml中的资源(正在 从“/var/www/src/kernel.php”中导入)。 (500个内部服务器错误)

因此,我又回到了kernel.php中找到它,所有这些都很好,但确实知道添加全局功能的正确方法,当我尝试我的同时,交响乐的缓存有什么含义各种方法。

I thought this would be easy. For troubleshooting, I wish to call my _logger() function from anywhere in my application.

function _logger(?string $msg=null, int $offset=0, bool $time=false):void
{
    $d1 = debug_backtrace()[$offset+1];
    $d2 = debug_backtrace()[$offset+2];
    syslog(LOG_INFO, sprintf('_logger%s: %s::%s(%s) called by %s::%s on line %s%s', $time?'('.date("Y-m-d H:i:s").')':'', $d1['class'], $d1['function'], json_encode($d1['args']), $d2['class'], $d2['function'], $d2['line'], $msg?' | '.$msg:''));
}

First, I tried adding it to index.php. When added directly in the main body, I got a server error and either with error reporting, couldn't get any info about it (just curious, but anyone know why?). I then placed it into the closure function and it works for HTTP requests only.

<?php

use App\Kernel;

// Added my logger() function here resulted in unexplained server error.

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
    // Added my logger() function here works
    return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

Then I tried adding it to to Kernal.php, and it seems to work, but must be called as \App\_logger().

<?php

declare(strict_types=1);

namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;
}
if(!function_exists('_logger')) {
    // Added my logger() function here works but must be called as \App\_logger()
}

So, then I created a new file called _logger.php which I located in src, and it works sometimes but other times the get the below error and need to clear cache to get it working again.

<?php

declare(strict_types=1);

if(!function_exists('_logger')) {
    // Added my logger() function sometimes works but other times doesn't
}

Expected to find class "App_logger" in file
"/var/www/src/_logger.php" while importing services from resource
"../src/", but it was not found! Check the namespace prefix used with
the resource in /var/www/config/services.yaml (which is being
imported from "/var/www/src/Kernel.php"). (500 Internal Server Error)

So, I went back to locating it in Kernel.php, and all is kind of good, but would really know the correct way to add a global function and what are the implications of Symphony's caching when I tried my various approaches.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2025-02-20 15:58:48

除了设计决策,您可以通过在kernal.php文件中覆盖名称空间来实现这一目标:

<?php

declare(strict_types=1);

namespace App { 
  // Must use bracket notation in order to use global namespace in the same file
  // See https://www.php.net/manual/en/language.namespaces.definitionmultiple.php#example-295

  use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
  use Symfony\Component\HttpKernel\Kernel as BaseKernel;

  class Kernel extends BaseKernel
  {
      use MicroKernelTrait;
  }
}

namespace {
  // Begin the global namespace

  if(!function_exists('_logger')) {
      // Added my logger() function here works but must be called as 
  \App\_logger()
  }
}

注意:此代码未经测试。

来源:

Design decisions aside, you can achieve this by overriding the namespace in the Kernal.php file:

<?php

declare(strict_types=1);

namespace App { 
  // Must use bracket notation in order to use global namespace in the same file
  // See https://www.php.net/manual/en/language.namespaces.definitionmultiple.php#example-295

  use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
  use Symfony\Component\HttpKernel\Kernel as BaseKernel;

  class Kernel extends BaseKernel
  {
      use MicroKernelTrait;
  }
}

namespace {
  // Begin the global namespace

  if(!function_exists('_logger')) {
      // Added my logger() function here works but must be called as 
  \App\_logger()
  }
}

Note: this code is untested.

Sources:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文