Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce unnecessary calls #7

Open
biplobice opened this issue May 14, 2024 · 0 comments
Open

Reduce unnecessary calls #7

biplobice opened this issue May 14, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@biplobice
Copy link

public function view()
{
$this->set('corp', $this->getPackageConfig()->get('security.cross_origin_resource_policy'));
$this->set('coop', $this->getPackageConfig()->get('security.cross_origin_opener_policy'));
$this->set('coep', $this->getPackageConfig()->get('security.cross_origin_embedder_policy'));
$this->set('accessControlAllowOrigin', $this->getPackageConfig()->get('security.access_control_allow_origin'));
$this->set('nosniff', $this->getPackageConfig()->get('security.x_content_type_options'));
}
protected function getPackageConfig()
{
/** @var PackageService $packageService */
$packageService = $this->app->make(PackageService::class);
$package = $packageService->getClass('md_security_header_extended');
return $package->getFileConfig();
}

I noticed that in the code snippet above, the PackageService class is instantiated multiple times by $this->getPackageConfig() method calls, which can impact performance.

To optimize this, we can implement two approaches:

Approach 1

Retrieve the config array once and use its values directly:

$security = $this->getPackageConfig()->get('security');

Approach 2

Introduce a new property to cache the package config and reuse it:

if (!$this->pkgConfig) {
    $this->pkgConfig = // Set the property here
}

Implementing either of these approaches can help optimize performance by reducing redundant calls to $this->getPackageConfig().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant