<commit_before><?php

/**
 *	@author <NAME> <<EMAIL>>
 *	@license FreeBSD License (http://opensource.org/licenses/BSD-2-Clause)
 */

namespace fg\Essence;

use fg\Essence\Utility\Package;
use fg\Essence\Utility\Set;



/**
 *	A collection of providers which can find the provider of an url.
 *
 *	@package fg.Essence
 */

class ProviderCollection {

	/**
	 *	A list of providers.
	 *
	 *	@var array
	 */

	protected $_Package = null;



	/**
	 *	A list of providers.
	 *
	 *	@var array
	 */

	protected $_providers = array( );



	/**
	 *	Loads the given providers.
	 *
	 *	@see load( )
	 *	@param array $providers An array of provider class names, relative to
	 *		the 'Provider' folder.
	 */

	public function __construct( array $providers = array( )) {

		$this->_Package = new Package(
			dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'Provider'
		);

		$this->load( $providers );
	}



	/**
	 *	Loads the given providers. If no particular provider is specified,
	 *	then all the available providers are loaded.
	 *	Throws an exception if a Provider couldn't be found.
	 *
	 *	@param array $providers An array of provider class names, relative to
	 *		the 'Provider' folder.
	 *	@throws fg\Essence\Exception
	 */

	public function load( array $providers = array( )) {

		$excludeGenerics = false;

		if ( empty( $providers )) {
			$providers = $this->_Package->classes( );
			$excludeGenerics = true;
		}

		$providers = Set::normalize( $providers, array( ));
		$this->_providers = array( );

		foreach ( $providers as $name => $options ) {
			// check if the given provider name start with a slash
			// if the first char is a slash a FQCN is given
			// otherwise we need to transform it into a FQCN relative to the Essence buildin providers namespace
			if ( substr($name, 0, 1) === '\\' ) {
			    $className = $name;
			} else {
			    $className = '\\fg\\Essence\\Provider\\' . str_replace( '/', '\\', $name );
			}

			$Reflection = new \ReflectionClass( $className );

			if ( !$Reflection->isAbstract( )) {
				$Provider = $Reflection->newInstance( $options );

				if ( $Provider->isGeneric( )) {
					if ( !$excludeGenerics ) {
						$this->_providers[ ] = $Provider;
					}
				} else {
					// regular providers are pushed to the front to take
					// precedence over the generic ones.
					array_unshift( $this->_providers, $Provider );
				}
			}
		}
	}



	/**
	 *	Tells if a provider was found for the given url.
	 *<commit_msg>Refactored load( )<commit_after><?php

/**
 *	@author <NAME> <<EMAIL>>
 *	@license FreeBSD License (http://opensource.org/licenses/BSD-2-Clause)
 */

namespace fg\Essence;

use fg\Essence\Utility\Package;
use fg\Essence\Utility\Set;



/**
 *	A collection of providers which can find the provider of an url.
 *
 *	@package fg.Essence
 */

class ProviderCollection {

	/**
	 *	A list of providers.
	 *
	 *	@var array
	 */

	protected $_Package = null;



	/**
	 *	A list of providers.
	 *
	 *	@var array
	 */

	protected $_providers = array( );



	/**
	 *	Loads the given providers.
	 *
	 *	@see load( )
	 *	@param array $providers An array of provider class names, relative to
	 *		the 'Provider' folder.
	 */

	public function __construct( array $providers = array( )) {

		$this->_Package = new Package(
			dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'Provider'
		);

		$this->load( $providers );
	}



	/**
	 *	Loads the given providers. If no particular provider is specified,
	 *	then all the available providers are loaded.
	 *	Throws an exception if a Provider couldn't be found.
	 *
	 *	@param array $providers An array of provider class names, relative to
	 *		the 'Provider' folder.
	 *	@throws fg\Essence\Exception
	 */

	public function load( array $providers = array( )) {

		$excludeGenerics = empty( $providers );

		if ( $excludeGenerics ) {
			$providers = $this->_Package->classes( );
		}

		$providers = Set::normalize( $providers, array( ));

		foreach ( $providers as $name => $options ) {
			$Reflection = new \ReflectionClass(
				$this->_fullyQualified( $name )
			);

			if ( !$Reflection->isAbstract( )) {
				$Provider = $Reflection->newInstance( $options );

				if ( $Provider->isGeneric( )) {
					if ( !$excludeGenerics ) {
						$this->_providers[ ] = $Provider;
					}
				} else {
					// regular providers are pushed to the front to take
					// precedence over the generic ones.
					array_unshift( $this->_providers, $Provider );
				}
			}
		}
	}



	/**
	 *	Returns the fully qualified class name (FQCN) for the given provider
	 *	name. If the name happens to be a FQCN, it is returned as is.
	 *
	 *	@param string $name Provider name.
	 *	@param string FQCN.
	 */

	protected function _fullyQualified( $name ) {

		return ( $name[ 0 ] !== '\\' )
			? '\\fg\\Essence\\Provider\\' . str_replace( '/', '\\', $name )
			: $name;
	}



	/**
	 *	Tells if a provider was found for the given url.
	 *
{'fraction_non_alphanumeric': 0.13934426229508196, 'fraction_numerical': 0.0010245901639344263, 'mean_word_length': 2.7604006163328196, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3208044', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1542, 'n_words': 479}