<commit_before><?php

namespace bazilio\yii\newrelic;

use bazilio\yii\newrelic\handlers\BaseHandler;
use bazilio\yii\newrelic\handlers\ConsoleHandler;
use bazilio\yii\newrelic\handlers\WebHandler;
use NewRelic\NewRelic as Agent;
use yii\base\BootstrapInterface;
use yii\base\Component;

/**
 * Class Newrelic
 * @package bazilio\yii\newrelic
 */
class Newrelic extends Component implements BootstrapInterface
{
    /**
     * @var bool Enable agent
     */
    public $enabled = true;
    /**
     * @var \NewRelic\NewRelic
     */
    public $agent;

    /**
     * @var string App name
     */
    public $name;

    /**
     * @var string Licence key
     */
    public $licence = 'newrelic.license';

    /**
     * @var string handlers\Handler
     */
    public $handler;

    /**
     * @var bool Enable view instrumentation with newrelic scripts
     */
    public $enableEndUser = true;

    /**
     * @inheritdoc
     */
    public function bootstrap($app)
    {
        if (!$this->enabled) {
            return;
        }

        if ($this->handler) {
            $handler = new $this->handler(['newrelic' => $this]);
        } elseif ($app instanceof \yii\web\Application) {
            $handler = new WebHandler(['newrelic' => $this]);
        } elseif ($app instanceof \yii\console\Application) {
            $handler = new ConsoleHandler(['newrelic' => $this]);
        } else {
            $handler = new BaseHandler(['newrelic' => $this]);
        }

        $handler->bootstrap($app);
    }

    public function init()
    {
        parent::init();

        if ($this->enabled && Agent::isLoaded()) {
            $this->name = $this->name ? $this->name : \Yii::$app->name;
            $this->agent = new Agent();
            $this->agent->setAppname($this->name, $this->licence);
        }
    }
}<commit_msg>Don't crush application if newrelic ext is missing<commit_after><?php

namespace bazilio\yii\newrelic;

use bazilio\yii\newrelic\handlers\BaseHandler;
use bazilio\yii\newrelic\handlers\ConsoleHandler;
use bazilio\yii\newrelic\handlers\WebHandler;
use NewRelic\NewRelic as Agent;
use yii\base\BootstrapInterface;
use yii\base\Component;

/**
 * Class Newrelic
 * @package bazilio\yii\newrelic
 */
class Newrelic extends Component implements BootstrapInterface
{
    /**
     * @var bool Enable agent
     */
    public $enabled = true;
    /**
     * @var \NewRelic\NewRelic
     */
    public $agent;

    /**
     * @var string App name
     */
    public $name;

    /**
     * @var string Licence key
     */
    public $licence = 'newrelic.license';

    /**
     * @var string handlers\Handler
     */
    public $handler;

    /**
     * @var bool Enable view instrumentation with newrelic scripts
     */
    public $enableEndUser = true;

    /**
     * @inheritdoc
     */
    public function bootstrap($app)
    {
        if (!$this->enabled) {
            return;
        }

        if ($this->handler) {
            $handler = new $this->handler(['newrelic' => $this]);
        } elseif ($app instanceof \yii\web\Application) {
            $handler = new WebHandler(['newrelic' => $this]);
        } elseif ($app instanceof \yii\console\Application) {
            $handler = new ConsoleHandler(['newrelic' => $this]);
        } else {
            $handler = new BaseHandler(['newrelic' => $this]);
        }

        $handler->bootstrap($app);
    }

    public function init()
    {
        parent::init();

        if ($this->enabled) {
            if (Agent::isLoaded()) {
                $this->name = $this->name ? $this->name : \Yii::$app->name;
                $this->agent = new Agent();
                $this->agent->setAppname($this->name, $this->licence);
            } else {
                $this->enabled = false;
                \Yii::$app->getLog()->getLogger()->log('Newrelic extension is not loaded', \yii\log\Logger::LEVEL_ERROR);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.1480632008154944, 'fraction_numerical': 0.0, 'mean_word_length': 2.0760188087774294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '3975180', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1198, 'n_words': 253}