
#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

package Common::YAML;

use cPstrict;
use Cpanel::Exception;

=head1 MODULE

C<Common::YAML>

=head1 DESCRIPTION

C<Common::YAML> is used to load the configuration file for the provided plugin's yaml.
This file contains various configuration options that are used by the plugin and its components.

=head1 SYNOPSIS

  use  Common::YAML();
  my $path = "/var/cpanel/plugins/test/test.yaml";
  my $config = Common::YAML::load($path);

=head1 PRIVATE PROPERTIES

=head2 $_config

The cached configuration.

=cut

my $_config;

=head1 METHODS

=head2 load

Load the configuration. If already loaded, it will return the cached configuration..

=cut

sub load ($config_file) {
    if ( !$_config ) {

        if ( !-e $config_file ) {
            die Cpanel::Exception::create( 'InvalidParameter', 'Please provide the YAML config file.' );
        }

        require YAML::XS;
        $_config = YAML::XS::LoadFile($config_file)
          or die sprintf( 'Failed to load %s with error: %s', $config_file, $! );
    }
    return $_config;
}

=head2 clear_cache

Clear the cached configuration. This is useful when you want to reload the configuration file.

=cut

sub clear_cache {
    $_config = undef;
}

1;
