Skip to content

Commit

Permalink
[TASK] update to sentry SDK 2.0
Browse files Browse the repository at this point in the history
Now requires Shopware 5.5 or higher
Only compatible with PHP >= 7.1

Relates #24
  • Loading branch information
Hans Höchtl committed Jul 22, 2019
1 parent 4be752c commit 9f2c2bc
Show file tree
Hide file tree
Showing 14 changed files with 1,710 additions and 354 deletions.
36 changes: 36 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in('./custom/project/')
->in('./repository/')
;

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'single_quote' => true,
'function_typehint_space' => true,
'hash_to_slash_comment' => true,
'method_separation' => true,
'no_blank_lines_after_phpdoc' => true,
'no_blank_lines_before_namespace' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'phpdoc_align' => true,
'phpdoc_order' => true,
'phpdoc_scalar' => true,
'pre_increment' => true,
'short_scalar_cast' => true,
'space_after_semicolon' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline_array' => true,
'semicolon_after_instruction' => true,
'trim_array_spaces' => true,
'whitespace_after_comma_in_array' => true,
'phpdoc_add_missing_param_annotation' => true,
'binary_operator_spaces' => ['align_equals' => false, 'align_double_arrow' => true],
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
])
->setFinder($finder)
;
113 changes: 0 additions & 113 deletions Components/SentryClient.php

This file was deleted.

8 changes: 3 additions & 5 deletions Components/SentryCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OdSentry\Components;


use Enlight_Controller_Exception;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand All @@ -26,8 +24,8 @@ class SentryCompilerPass implements CompilerPassInterface
private $defaultConfiguration = [
'skip_capture' => [
CommandNotFoundException::class,
Enlight_Controller_Exception::class
]
Enlight_Controller_Exception::class,
],
];

/**
Expand All @@ -41,4 +39,4 @@ public function process(ContainerBuilder $container)
$container->setParameter('shopware.sentry', $this->defaultConfiguration);
}
}
}
}
189 changes: 1 addition & 188 deletions OdSentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,17 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OdSentry;

use Doctrine\Common\Collections\ArrayCollection;
use Enlight_Event_EventArgs;
use OdSentry\Components\SentryClient;
use OdSentry\Components\SentryCompilerPass;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Theme\LessDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class OdSentry extends Plugin
{
/**
* @var SentryClient
*/
protected $sentryClient;

/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Enlight_Controller_Front_StartDispatch' => 'onStartDispatch',
'Enlight_Controller_Action_PreDispatch_Frontend_Error' => 'onPreDispatchError',
'Enlight_Controller_Action_PreDispatch_Backend_Error' => 'onPreDispatchBackendError',
'Theme_Compiler_Collect_Plugin_Javascript' => 'addJsFiles',
'Theme_Compiler_Collect_Plugin_Less' => 'addLessFiles',
'Theme_Compiler_Collect_Javascript_Files_FilterResult' => 'sortJs',
'Enlight_Controller_Action_PostDispatch_Frontend' => 'onPostDispatchFrontend',
'Enlight_Controller_Front_DispatchLoopShutdown' => 'onDispatchLoopShutdown',
'Shopware_Console_Add_Command' => 'onStartDispatch'
];
}
const PLUGIN_NAME = 'OdSentry';

/**
* Theme must be recompiled to include JS
Expand All @@ -61,165 +35,4 @@ public function build(ContainerBuilder $container)
parent::build($container);
$container->addCompilerPass(new SentryCompilerPass());
}


/**
* Use the autoloader from the Raven library to load all necessary classes
*/
public function onStartDispatch()
{
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}

if (Shopware()->Config()->getByNamespace('OdSentry', 'sentryLogPhp')) {
$privateDsn = Shopware()->Config()->getByNamespace('OdSentry', 'sentryPrivateDsn');
$this->sentryClient = new SentryClient($privateDsn, [
'release' => \Shopware::VERSION,
'environment' => $this->container->getParameter('kernel.environment'),
'install_default_breadcrumb_handlers' => false
]);
$this->sentryClient->setContainer($this->container);

// Register global error handler
$errorHandler = new \Raven_ErrorHandler($this->sentryClient);
$errorHandler->registerExceptionHandler();
$errorHandler->registerShutdownFunction();
// Restore Shopware default error handler
restore_error_handler();
restore_exception_handler();
}
}

/**
* @param Enlight_Event_EventArgs $args
*
* @return void
*/
public function onPreDispatchBackendError(Enlight_Event_EventArgs $args)
{
/** @var \Shopware_Controllers_Frontend_Error $subject */
$subject = $args->getSubject();
if ($this->sentryClient) {
$error = $subject->Request()->getParam('error_handler');
if ($error && $error->exception) {
$this->sentryClient->captureException($error->exception);
}
}
}

/**
* @param Enlight_Event_EventArgs $args
*
* @return void
*/
public function onPreDispatchError(Enlight_Event_EventArgs $args)
{
/** @var \Shopware_Controllers_Frontend_Error $subject */
$subject = $args->getSubject();
if ($this->sentryClient) {
$error = $subject->Request()->getParam('error_handler');
if ($error && $error->exception) {
$id = $this->sentryClient->captureException($error->exception);
if (!Shopware()->Config()->getByNamespace('OdSentry', 'sentryUserfeedback')) {
return;
}
$this->container->get('template')->assignGlobal('sentryId', $id);
$this->container->get('template')->addTemplateDir($this->getPath() . '/Resources/views/');
}
}
}

/**
* Add the raven-js library to the compiled JS.
* It will we reordered by the sortJs function to be included
* before the bootstrapping of most JS to track these errors too.
*
* @return ArrayCollection
*/
public function addJsFiles()
{
$jsFiles = [];
$jsDir = __DIR__ . '/Resources/views/frontend/_public/src/js/';
if (Shopware()->Config()->getByNamespace('OdSentry', 'sentryLogJs') || Shopware()->Config()->getByNamespace('OdSentry', 'sentryUserfeedback')) {
$jsFiles[] = $jsDir . 'vendor/raven.min.js';
}
return new ArrayCollection($jsFiles);
}

/**
* @return ArrayCollection
*/
public function addLessFiles()
{
$less = new LessDefinition(
[],
[__DIR__ . '/Resources/views/frontend/_public/src/less/all.less'],
__DIR__
);

return new ArrayCollection([$less]);
}

/**
* Sort the raven-js library to the front of the JS compilation pipeline
* so that it can track errors in the initialization of other JS libraries.
*
* @param Enlight_Event_EventArgs $args
* @return array
*/
public function sortJs(\Enlight_Event_EventArgs $args)
{
$files = $args->getReturn();
$fileIdx = -1;
foreach ($files as $idx => $file) {
if (strpos($file, 'raven.min.js') !== false && strpos($file, 'OdSentry') !== false) {
$fileIdx = $idx;
break;
}
}
if ($fileIdx > -1) {
$tmp = array_splice($files, $fileIdx, 1);
// the 5th position is usually after the vendor libraries
array_splice($files, 5, 0, $tmp);
}
return $files;
}

/**
* Like the \Shopware_Plugins_Core_ErrorHandler_Bootstrap we want to catch all errors
* that occured during a request and send them to Sentry
*
* @param \Enlight_Controller_EventArgs $args
*/
public function onDispatchLoopShutdown(\Enlight_Controller_EventArgs $args)
{
$response = $args->getSubject()->Response();
$exceptions = $response->getException();
if (empty($exceptions) || !$this->sentryClient) {
return;
}
foreach ($exceptions as $exception) {
$this->sentryClient->captureException($exception);
}
}

/**
* We add templates from our plugin to include raven-js initialization after the JS libs
*
* @param \Enlight_Event_EventArgs $args
*/
public function onPostDispatchFrontend(\Enlight_Event_EventArgs $args)
{
/** @var \Enlight_Controller_Action $controller */
$controller = $args->get('subject');
$request = $controller->Request();
$response = $controller->Response();
$view = $controller->View();

if (!$request->isDispatched() || $response->isException() || $request->getModuleName() !== 'frontend' || !$view->hasTemplate()) {
return;
}
$view->addTemplateDir($this->getPath() . '/Resources/views/');
}
}
Loading

0 comments on commit 9f2c2bc

Please sign in to comment.