<commit_before><?php
declare(strict_types=1);

namespace PhpList\PhpList4\Core;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Setup;

/**
 * This class bootstraps the phpList core system.
 *
 * Include it in from the entry point and call Bootstrap::getInstance()->configure().
 *
 * This class is the only "real" singleton in the system. All other singletons will use
 * Symfony dependency injection.
 *
 * @author <NAME> <<EMAIL>>
 */
class Bootstrap
{
    /**
     * application context for running a live site
     *
     * @var string
     */
    const APPLICATION_CONTEXT_PRODUCTION = 'Production';

    /**
     * application context for developing locally
     *
     * @var string
     */
    const APPLICATION_CONTEXT_DEVELOPMENT = 'Development';

    /**
     * application context for running automated tests
     *
     * @var string
     */
    const APPLICATION_CONTEXT_TESTING = 'Testing';

    /**
     * @var string
     */
    const DEFAULT_APPLICATION_CONTEXT = self::APPLICATION_CONTEXT_PRODUCTION;

    /**
     * @var string[]
     */
    private static $validApplicationContexts = [
        self::APPLICATION_CONTEXT_PRODUCTION,
        self::APPLICATION_CONTEXT_DEVELOPMENT,
        self::APPLICATION_CONTEXT_TESTING,
    ];

    /**
     * @var Bootstrap|null
     */
    private static $instance = null;

    /**
     * @var string
     */
    private $applicationContext = self::DEFAULT_APPLICATION_CONTEXT;

    /**
     * @var EntityManager
     */
    private $entityManager = null;

    /**
     * Disable direct cloning of this object.
     */
    protected function __clone()
    {
    }

    /**
     * Returns 'this' as singleton.
     *
     * @return Bootstrap
     */
    public static function getInstance(): Bootstrap
    {
        if (static::$instance === null) {
            self::$instance = new static();
        }

        return static::$instance;
    }

    /**
     * Purges the singleton instance.
     *
     * Note: This method is intended to be use for tests only.
     *
     * @return void
     */
    public static function purgeInstance()
    {
        self::$instance = null;
    }

    /**
     * @param string $context must be one of the APPLICATION_CONTEXT_* constants
     *
     * @return Bootstrap fluent interface
     *
     * @throws \UnexpectedValueException
     */
    public function setApplicationContext(string $context): Bootstrap
    {
        if (!in_array($context, self::$validApplicationContexts, true)) {
            throw new \UnexpectedValueException(
                '$context must be one of "Production", "Development", or "Testing", but actually is: ' . $context,
                1499112172108
            );
        }

        $this->applicationContext = $context;

        return $this;
    }

    /**
     * @return string
     */
    public function getApplicationContext(): string
    {
        return $this->applicationContext;
    }

    /**
     * @return bool
     */
    private function isDoctrineOrmDevelopmentModeEnabled(): bool
    {
        return $this->applicationContext !== self::APPLICATION_CONTEXT_PRODUCTION;
    }

    /**
     * Main entry point called at every request usually from global scope. Checks if everything is correct
     * and loads the configuration.
     *
     * @return Bootstrap fluent interface
     */
    public function configure(): Bootstrap
    {
        $packageRootPath = dirname(__DIR__ . '/../../');
        $domainModelPath = $packageRootPath . 'Classes/Domain/Model/';
        $domainModelPaths = [$domainModelPath];

        // The getenv calls will be replaced by YAML configuration later
        // (with the option to use environment variables as overrides).
        $databaseConfiguration = [
            'driver' => 'pdo_mysql',
            'user' => getenv('PHPLIST_DATABASE_USER'),
            'password' => getenv('<PASSWORD>'),
            'dbname' => getenv('PHPLIST_DATABASE_NAME'),
        ];

        $ormConfiguration = Setup::createAnnotationMetadataConfiguration(
            $domainModelPaths,
            $this->isDoctrineOrmDevelopmentModeEnabled()
        );
        $this->entityManager = EntityManager::create($databaseConfiguration, $ormConfiguration);

        return $this;
    }

    /**
     * @return EntityManagerInterface
     */
    public function getEntityManager(): EntityManagerInterface
    {
        return $this->entityManager;
    }
}
<commit_msg>[BUGFIX] Prevent direct instatiation of the Bootstrap class (#111)<commit_after><?php
declare(strict_types=1);

namespace PhpList\PhpList4\Core;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Setup;

/**
 * This class bootstraps the phpList core system.
 *
 * Include it in from the entry point and call Bootstrap::getInstance()->configure().
 *
 * This class is the only "real" singleton in the system. All other singletons will use
 * Symfony dependency injection.
 *
 * @author <NAME> <<EMAIL>>
 */
class Bootstrap
{
    /**
     * application context for running a live site
     *
     * @var string
     */
    const APPLICATION_CONTEXT_PRODUCTION = 'Production';

    /**
     * application context for developing locally
     *
     * @var string
     */
    const APPLICATION_CONTEXT_DEVELOPMENT = 'Development';

    /**
     * application context for running automated tests
     *
     * @var string
     */
    const APPLICATION_CONTEXT_TESTING = 'Testing';

    /**
     * @var string
     */
    const DEFAULT_APPLICATION_CONTEXT = self::APPLICATION_CONTEXT_PRODUCTION;

    /**
     * @var string[]
     */
    private static $validApplicationContexts = [
        self::APPLICATION_CONTEXT_PRODUCTION,
        self::APPLICATION_CONTEXT_DEVELOPMENT,
        self::APPLICATION_CONTEXT_TESTING,
    ];

    /**
     * @var Bootstrap|null
     */
    private static $instance = null;

    /**
     * @var string
     */
    private $applicationContext = self::DEFAULT_APPLICATION_CONTEXT;

    /**
     * @var EntityManager
     */
    private $entityManager = null;

    /**
     * Protected constructor to avoid direct instantiation of this class.
     *
     * Please use getInstance instead.
     */
    protected function __construct()
    {
    }

    /**
     * Disable direct cloning of this object.
     */
    protected function __clone()
    {
    }

    /**
     * Returns 'this' as singleton.
     *
     * @return Bootstrap
     */
    public static function getInstance(): Bootstrap
    {
        if (static::$instance === null) {
            self::$instance = new static();
        }

        return static::$instance;
    }

    /**
     * Purges the singleton instance.
     *
     * Note: This method is intended to be use for tests only.
     *
     * @return void
     */
    public static function purgeInstance()
    {
        self::$instance = null;
    }

    /**
     * @param string $context must be one of the APPLICATION_CONTEXT_* constants
     *
     * @return Bootstrap fluent interface
     *
     * @throws \UnexpectedValueException
     */
    public function setApplicationContext(string $context): Bootstrap
    {
        if (!in_array($context, self::$validApplicationContexts, true)) {
            throw new \UnexpectedValueException(
                '$context must be one of "Production", "Development", or "Testing", but actually is: ' . $context,
                1499112172108
            );
        }

        $this->applicationContext = $context;

        return $this;
    }

    /**
     * @return string
     */
    public function getApplicationContext(): string
    {
        return $this->applicationContext;
    }

    /**
     * @return bool
     */
    private function isDoctrineOrmDevelopmentModeEnabled(): bool
    {
        return $this->applicationContext !== self::APPLICATION_CONTEXT_PRODUCTION;
    }

    /**
     * Main entry point called at every request usually from global scope. Checks if everything is correct
     * and loads the configuration.
     *
     * @return Bootstrap fluent interface
     */
    public function configure(): Bootstrap
    {
        $packageRootPath = dirname(__DIR__ . '/../../');
        $domainModelPath = $packageRootPath . 'Classes/Domain/Model/';
        $domainModelPaths = [$domainModelPath];

        // The getenv calls will be replaced by YAML configuration later
        // (with the option to use environment variables as overrides).
        $databaseConfiguration = [
            'driver' => 'pdo_mysql',
            'user' => getenv('PHPLIST_DATABASE_USER'),
            'password' => getenv('<PASSWORD>'),
            'dbname' => getenv('PHPLIST_DATABASE_NAME'),
        ];

        $ormConfiguration = Setup::createAnnotationMetadataConfiguration(
            $domainModelPaths,
            $this->isDoctrineOrmDevelopmentModeEnabled()
        );
        $this->entityManager = EntityManager::create($databaseConfiguration, $ormConfiguration);

        return $this;
    }

    /**
     * @return EntityManagerInterface
     */
    public function getEntityManager(): EntityManagerInterface
    {
        return $this->entityManager;
    }
}

{'fraction_non_alphanumeric': 0.1056238442293049, 'fraction_numerical': 0.0035896878059393017, 'mean_word_length': 2.327542526239595, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2456463', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2414, 'n_words': 636}