<commit_before><?php
namespace Bolt;

use Bolt\Extensions\ExtensionInterface;
use Symfony\Component\Console\Command\Command;
use Composer\Json\JsonFile;

abstract class BaseExtension extends \Twig_Extension implements ExtensionInterface
{
    protected $app;
    protected $basepath;
    protected $namespace;
    protected $functionlist;
    protected $filterlist;
    protected $snippetlist;

    private $extensionConfig;
    private $composerJsonLoaded;
    private $composerJson;
    private $configLoaded;

    public function __construct(Application $app)
    {
        $this->app = $app;

        $this->setBasepath();

        // Don't load config just yet. Let 'Extensions' handle this when
        // activating, just clear the "configLoaded" flag to tell the
        // lazy-loading mechanism to do its thing.
        // $this->getConfig();
        $this->configLoaded = false;
        $this->extensionConfig = null;
        $this->composerJsonLoaded = false;

        $this->functionlist = array();
        $this->filterlist = array();
        $this->snippetlist = array();
    }

    /**
     * Set the 'basepath' and the 'namespace' for the extension. We can't use
     * __DIR__, because that would give us the base path for BaseExtension.php
     * (the file you're looking at), rather than the base path for the actual,
     * derived, extension class.
     *
     * @see http://stackoverflow.com/questions/11117637/getting-current-working-directory-of-an-extended-class-in-php
     *
     */
    private function setBasepath()
    {
        $reflection = new \ReflectionClass($this);
        $this->basepath = dirname($reflection->getFileName());
        $this->namespace = basename(dirname($reflection->getFileName()));
    }

    /**
     * Get the base path, that is, the directory where the (derived) extension
     * class file is located. The base path is the "root directory" under which
     * all files related to the extension can be found.
     *
     * @return string
     */
    public function getBasePath()
    {
        return $this->basepath;
    }

    public function getBaseUrl()
    {
        $relative = str_replace($this->app['resources']->getPath('extensions'), "", $this->basepath);

        return $this->app['resources']->getUrl('extensions') . ltrim($relative, "/") . "/";
    }

    /**
     * Gets the Composer name, e.g. 'bolt/foobar-extension'.
     * @return string The Composer name for this extension, or NULL if the
     *                extension is not composerized.
     */
    public function getComposerName()
    {
        $composerjson = $this->getComposerJSON();
        if (isset($composerjson['name'])) {
            return $composerjson['name'];
        }
        else {
            return null;
        }
    }

    /**
     * Gets a 'machine name' for this extension.
     * The machine name is the composer name, if available, or a slugified
     * version of the name as reported by getName() otherwise.
     *
     * @return string
     */
    public function getMachineName()
    {
        $composerName = $this->getComposerName();
        if (empty($composerName)) {
            return util::slugify($this->getName());
        }
        else {
            return $composerName;
        }
    }

    /**
     * Get the contents of the extension's composer.json file, lazy-loading
     * as needed.
     */
    private function getComposerJSON()
    {
        if (!$this->composerJsonLoaded) {
            $this->composerJsonLoaded = true;
            $this->composerJson = null;
            $jsonFile = new JsonFile($this->getBasepath() . '/composer.json');
            if ($jsonFile->exists()) {
                $this->composerJson = $jsonFile->read();
            }
        }
        return $this->composerJson;
    }

    /**
     * Builds an array suitable for conversion to JSON, which in turn will end
     * up in a consolidated JSON file containing the configurations of all
     * installed extensions.
     */
    public function getExtensionConfig()
    {
        $composerjson = $this->getComposerJSON();
        if (!is_array($this->extensionConfig)) {
            $composerjson = $this->getComposerJSON();
            if (is_array($composerjson)) {
                $this->extensionConfig = array(strtolower($composerjson['name']) => array(
                    'name' => $this->getName(),
                    'json' => $composerjson
                ));
            }
            else {
                $this->extensionConfig = array($this->getName() => array(
                    'name' => $this->getName(),
                    'json' => array()
                ));
            }
        }
        return $this->extensionConfig;
    }

    /**
     * Override this to provide a default configuration, which will be used
     * in the absence of a config.yml file.
     * @return array
     */
    protected function getDefaultConfig()
    {
        return array();
    }

    /**
     * Load the configuration files, creating missing files as needed based on
     * the .dist default.
     *
     * @return array
     */
    public function getConfig()
    {
        if ($this->configLoaded) {
            return $this->config;
        }

        $this->config = $this->getDefaultConfig();

        $basefile = $this->app['resources']->getPath('extensionsconfig') . '/' . $this->getMachineName();

        // Load main config
        if ($this->isConfigValid($basefile . '.yml', true)) {
            $this->loadConfigFile($basefile . '.yml');
        }

        // Load local config
        if ($this->isConfigValid($basefile . '_local.yml', false)) {
            $this->loadConfigFile($basefile . '.yml');
        }

        $this->configLoaded = true;

        return $this->config;
    }

    /**
     * Test if a given config file is valid (exists and is readable) and create
     * if required.
     *
     * @param  string  $configfile Fully qualified file path
     * @param  boolean $create     True - create file is non-existant
     *                             False - Only test for file existance
     * @return boolean
     */
    private function isConfigValid($configfile, $create)
    {
        if (file_exists($configfile)) {
            if (is_readable($configfile)) {
                return true;
            } else {<commit_msg>Config file name now follow the format of {ext_name}.{vendor}.yml<commit_after><?php
namespace Bolt;

use Bolt\Extensions\ExtensionInterface;
use Symfony\Component\Console\Command\Command;
use Composer\Json\JsonFile;

abstract class BaseExtension extends \Twig_Extension implements ExtensionInterface
{
    protected $app;
    protected $basepath;
    protected $namespace;
    protected $functionlist;
    protected $filterlist;
    protected $snippetlist;

    private $extensionConfig;
    private $composerJsonLoaded;
    private $composerJson;
    private $configLoaded;

    public function __construct(Application $app)
    {
        $this->app = $app;

        $this->setBasepath();

        // Don't load config just yet. Let 'Extensions' handle this when
        // activating, just clear the "configLoaded" flag to tell the
        // lazy-loading mechanism to do its thing.
        // $this->getConfig();
        $this->configLoaded = false;
        $this->extensionConfig = null;
        $this->composerJsonLoaded = false;

        $this->functionlist = array();
        $this->filterlist = array();
        $this->snippetlist = array();
    }

    /**
     * Set the 'basepath' and the 'namespace' for the extension. We can't use
     * __DIR__, because that would give us the base path for BaseExtension.php
     * (the file you're looking at), rather than the base path for the actual,
     * derived, extension class.
     *
     * @see http://stackoverflow.com/questions/11117637/getting-current-working-directory-of-an-extended-class-in-php
     *
     */
    private function setBasepath()
    {
        $reflection = new \ReflectionClass($this);
        $this->basepath = dirname($reflection->getFileName());
        $this->namespace = basename(dirname($reflection->getFileName()));
    }

    /**
     * Get the base path, that is, the directory where the (derived) extension
     * class file is located. The base path is the "root directory" under which
     * all files related to the extension can be found.
     *
     * @return string
     */
    public function getBasePath()
    {
        return $this->basepath;
    }

    public function getBaseUrl()
    {
        $relative = str_replace($this->app['resources']->getPath('extensions'), "", $this->basepath);

        return $this->app['resources']->getUrl('extensions') . ltrim($relative, "/") . "/";
    }

    /**
     * Gets the Composer name, e.g. 'bolt/foobar-extension'.
     * @return string The Composer name for this extension, or NULL if the
     *                extension is not composerized.
     */
    public function getComposerName()
    {
        $composerjson = $this->getComposerJSON();
        if (isset($composerjson['name'])) {
            return $composerjson['name'];
        }
        else {
            return null;
        }
    }

    /**
     * Gets a 'machine name' for this extension.
     * The machine name is the composer name, if available, or a slugified
     * version of the name as reported by getName() otherwise.
     *
     * @return string
     */
    public function getMachineName()
    {
        $composerName = $this->getComposerName();
        if (empty($composerName)) {
            return util::slugify($this->getName());
        }
        else {
            return $composerName;
        }
    }

    /**
     * Get the contents of the extension's composer.json file, lazy-loading
     * as needed.
     */
    private function getComposerJSON()
    {
        if (!$this->composerJsonLoaded) {
            $this->composerJsonLoaded = true;
            $this->composerJson = null;
            $jsonFile = new JsonFile($this->getBasepath() . '/composer.json');
            if ($jsonFile->exists()) {
                $this->composerJson = $jsonFile->read();
            }
        }
        return $this->composerJson;
    }

    /**
     * Builds an array suitable for conversion to JSON, which in turn will end
     * up in a consolidated JSON file containing the configurations of all
     * installed extensions.
     */
    public function getExtensionConfig()
    {
        $composerjson = $this->getComposerJSON();
        if (!is_array($this->extensionConfig)) {
            $composerjson = $this->getComposerJSON();
            if (is_array($composerjson)) {
                $this->extensionConfig = array(strtolower($composerjson['name']) => array(
                    'name' => $this->getName(),
                    'json' => $composerjson
                ));
            }
            else {
                $this->extensionConfig = array($this->getName() => array(
                    'name' => $this->getName(),
                    'json' => array()
                ));
            }
        }
        return $this->extensionConfig;
    }

    /**
     * Override this to provide a default configuration, which will be used
     * in the absence of a config.yml file.
     * @return array
     */
    protected function getDefaultConfig()
    {
        return array();
    }

    /**
     * Load the configuration files, creating missing files as needed based on
     * the .dist default.
     *
     * @return array
     */
    public function getConfig()
    {
        if ($this->configLoaded) {
            return $this->config;
        }

        $this->config = $this->getDefaultConfig();

        // Config file name should follow the format of {ext_name}.{vendor}.yml
        // and be in the root of the extension config directory
        $basefile = explode('/', $this->getMachineName());
        $basefile = isset($basefile[1]) ? $basefile[1] . '.' . $basefile[0] : $basefile[0];
        $basefile = $this->app['resources']->getPath('extensionsconfig') . '/' . $basefile;

        // Load main config
        if ($this->isConfigValid($basefile . '.yml', true)) {
            $this->loadConfigFile($basefile . '.yml');
        }

        // Load local config
        if ($this->isConfigValid($basefile . '_local.yml', false)) {
            $this->loadConfigFile($basefile . '.yml');
        }

        $this->configLoaded = true;

        return $this->config;
    }

    /**
     * Test if a given config file is valid (exists and is readable) and create
     * if required.
     *
     * @param  string  $configfile Fully qualified file path
     * @param  boolean $create     True - create file is non-existant
     *                             False - Only test for file existance
     * @return boolean
     */
    private function isConfigValid($configfile, $create)
    {
        if (file_exists($configfile)) {
            if (is_readable($configfile)) {
                return true;
            } else {
{'fraction_non_alphanumeric': 0.1212214445042689, 'fraction_numerical': 0.0015383432043688946, 'mean_word_length': 2.0414035087719298, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6061793', 'n_tokens_mistral': 3554, 'n_tokens_neox': 3448, 'n_words': 969}