Skip to content

Instantly share code, notes, and snippets.

@joelharkes
Last active April 30, 2021 19:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelharkes/79d7fd89f752fd186be92001c018a06d to your computer and use it in GitHub Desktop.
Save joelharkes/79d7fd89f752fd186be92001c018a06d to your computer and use it in GitHub Desktop.
Sqreen implementation for laravel, SQREEN_ID configurable in environment variable
# only start screen when SQREEN_ID environment variable is provided
if [[ -n "$SQREEN_ID" ]]; then
echo "Enabling screen" 1>&2
sqreen-installer config "$SQREEN_ID" "VAIGO_API"
fi
# from php-fpm entrypoint:
# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- php-fpm "$@"
fi
exec "$@"
FROM php:7.2-fpm as base
RUN apt-get -qq update \
&& curl -s https://8dc0b36f0ea6f2f21b721765e10a7e02768cd1825b4551f4:@packagecloud.io/install/repositories/sqreen/sqreen/script.deb.sh | bash \
&& apt-get -qq install --no-install-recommends sqreen-agent sqreen-php-extension \
&& apt-get -qq -o=Dpkg::Use-Pty=0 clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
COPY ./scripts/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint
ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm"]
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
* Register any events for your application.
*/
public function boot()
{
// Add to config/auth.php: 'sqreen_id' => env('SQREEN_ID', ''),
if (config('auth.sqreen_id')) {
# only log when SQREEN_ID env variable is defined.
Event::listen(SqreenEvents::$listensTo, SqreenEvents::class);
}
}
}
<?php
namespace App\Lib;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Auth\Events\Login;
/**
* Notifies Sqreen about user events.
*/
class SqreenEvents
{
public static $listensTo = [
Login::class, Authenticated::class,
];
/**
* @param $event Login|Authenticated
*/
public function handle($event)
{
if ($event instanceof Login) {
\sqreen\auth_track(true, ['id' => $event->user->getAuthIdentifier()]);
}
if ($event instanceof Authenticated) {
\sqreen\identify(['id' => $event->user->getAuthIdentifier()]);
}
// add Illuminate\Auth\Events\Registered if your app allows user registrations
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment