Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
199 lars 1
<?php namespace Clockwork\Support\Laravel;
2
 
3
use Clockwork\Clockwork;
4
use Clockwork\Authentication\AuthenticatorInterface;
5
use Clockwork\DataSource\EloquentDataSource;
6
use Clockwork\DataSource\LaravelCacheDataSource;
7
use Clockwork\DataSource\LaravelDataSource;
8
use Clockwork\DataSource\LaravelEventsDataSource;
9
use Clockwork\DataSource\LaravelNotificationsDataSource;
10
use Clockwork\DataSource\LaravelQueueDataSource;
11
use Clockwork\DataSource\LaravelRedisDataSource;
12
use Clockwork\DataSource\LaravelTwigDataSource;
13
use Clockwork\DataSource\LaravelViewsDataSource;
14
use Clockwork\DataSource\SwiftDataSource;
15
use Clockwork\DataSource\TwigDataSource;
16
use Clockwork\DataSource\XdebugDataSource;
17
use Clockwork\Helpers\StackFilter;
18
use Clockwork\Request\Request;
19
use Clockwork\Storage\StorageInterface;
20
 
21
use Illuminate\Support\ServiceProvider;
22
 
23
class ClockworkServiceProvider extends ServiceProvider
24
{
25
	public function boot()
26
	{
27
		if ($this->app['clockwork.support']->isCollectingData()) {
28
			$this->registerEventListeners();
29
			$this->registerMiddleware();
30
		}
31
 
32
		$this->app['clockwork.support']->handleArtisanEvents();
33
		$this->app['clockwork.support']->handleOctaneEvents();
34
 
35
		// If Clockwork is disabled, return before registering middleware or routes
36
		if (! $this->app['clockwork.support']->isEnabled()) return;
37
 
38
		$this->registerRoutes();
39
 
40
		// register the Clockwork Web UI routes
41
		if ($this->app['clockwork.support']->isWebEnabled()) {
42
			$this->registerWebRoutes();
43
		}
44
	}
45
 
46
	public function register()
47
	{
48
		$this->registerConfiguration();
49
		$this->registerClockwork();
50
		$this->registerCommands();
51
		$this->registerDataSources();
52
		$this->registerAliases();
53
 
54
		$this->app->make('clockwork.request'); // instantiate the request to have id and time available as early as possible
55
 
56
		$this->app['clockwork.support']
57
			->configureSerializer()
58
			->configureShouldCollect()
59
			->configureShouldRecord();
60
 
61
		if ($this->app['clockwork.support']->getConfig('register_helpers', true)) {
62
			require __DIR__ . '/helpers.php';
63
		}
64
	}
65
 
66
	// Register the configuration file
67
	protected function registerConfiguration()
68
	{
69
		$this->publishes([ __DIR__ . '/config/clockwork.php' => config_path('clockwork.php') ]);
70
		$this->mergeConfigFrom(__DIR__ . '/config/clockwork.php', 'clockwork');
71
	}
72
 
73
	// Register core Clockwork components
74
	protected function registerClockwork()
75
	{
76
		$this->app->singleton('clockwork', function ($app) {
77
			return (new Clockwork)
78
				->authenticator($app['clockwork.authenticator'])
79
				->request($app['clockwork.request'])
80
				->storage($app['clockwork.storage']);
81
		});
82
 
83
		$this->app->singleton('clockwork.authenticator', function ($app) {
84
			return $app['clockwork.support']->makeAuthenticator();
85
		});
86
 
87
		$this->app->singleton('clockwork.request', function ($app) {
88
			return new Request;
89
		});
90
 
91
		$this->app->singleton('clockwork.storage', function ($app) {
92
			return $app['clockwork.support']->makeStorage();
93
		});
94
 
95
		$this->app->singleton('clockwork.support', function ($app) {
96
			return new ClockworkSupport($app);
97
		});
98
	}
99
 
100
	// Register the artisan commands
101
	protected function registerCommands()
102
	{
103
		$this->commands([
104
			ClockworkCleanCommand::class
105
		]);
106
	}
107
 
108
	// Register Clockwork data sources
109
	protected function registerDataSources()
110
	{
111
		$this->app->singleton('clockwork.cache', function ($app) {
112
			return (new LaravelCacheDataSource(
113
				$app['events'],
114
				$app['clockwork.support']->getConfig('features.cache.collect_queries'),
115
				$app['clockwork.support']->getConfig('features.cache.collect_values')
116
			));
117
		});
118
 
119
		$this->app->singleton('clockwork.eloquent', function ($app) {
120
			$dataSource = (new EloquentDataSource(
121
				$app['db'],
122
				$app['events'],
123
				$app['clockwork.support']->getConfig('features.database.collect_queries'),
124
				$app['clockwork.support']->getConfig('features.database.slow_threshold'),
125
				$app['clockwork.support']->getConfig('features.database.slow_only'),
126
				$app['clockwork.support']->getConfig('features.database.detect_duplicate_queries'),
127
				$app['clockwork.support']->getConfig('features.database.collect_models_actions'),
128
				$app['clockwork.support']->getConfig('features.database.collect_models_retrieved')
129
			));
130
 
131
			// if we are collecting queue jobs, filter out queries caused by the database queue implementation
132
			if ($app['clockwork.support']->isCollectingQueueJobs()) {
133
				$dataSource->addFilter(function ($query, $trace) {
134
					return ! $trace->first(StackFilter::make()->isClass(\Illuminate\Queue\DatabaseQueue::class));
135
				}, 'early');
136
			}
137
 
138
			if ($app->runningUnitTests()) {
139
				$dataSource->addFilter(function ($query, $trace) {
140
					return ! $trace->first(StackFilter::make()->isClass([
141
						\Illuminate\Database\Migrations\Migrator::class,
142
						\Illuminate\Database\Console\Migrations\MigrateCommand::class
143
					]));
144
				});
145
			}
146
 
147
			return $dataSource;
148
		});
149
 
150
		$this->app->singleton('clockwork.events', function ($app) {
151
			return (new LaravelEventsDataSource(
152
				$app['events'],
153
				$app['clockwork.support']->getConfig('features.events.ignored_events', [])
154
			));
155
		});
156
 
157
		$this->app->singleton('clockwork.laravel', function ($app) {
158
			return (new LaravelDataSource(
159
				$app,
160
				$app['clockwork.support']->isFeatureEnabled('log'),
161
				$app['clockwork.support']->isFeatureEnabled('routes'),
162
				$app['clockwork.support']->getConfig('features.routes.only_namespaces', [])
163
			));
164
		});
165
 
166
		$this->app->singleton('clockwork.notifications', function ($app) {
167
			return new LaravelNotificationsDataSource($app['events']);
168
		});
169
 
170
		$this->app->singleton('clockwork.queue', function ($app) {
171
			return (new LaravelQueueDataSource($app['queue']->connection()));
172
		});
173
 
174
		$this->app->singleton('clockwork.redis', function ($app) {
175
			$dataSource = new LaravelRedisDataSource($app['events']);
176
 
177
			// if we are collecting queue jobs, filter out commands executed by the redis queue implementation
178
			if ($app['clockwork.support']->isCollectingQueueJobs()) {
179
				$dataSource->addFilter(function ($query, $trace) {
180
					return ! $trace->first(StackFilter::make()->isClass([
181
						\Illuminate\Queue\RedisQueue::class,
182
						\Laravel\Horizon\Repositories\RedisJobRepository::class,
183
						\Laravel\Horizon\Repositories\RedisTagRepository::class,
184
						\Laravel\Horizon\Repositories\RedisMetricsRepository::class
185
					]));
186
				});
187
			}
188
 
189
			return $dataSource;
190
		});
191
 
192
		$this->app->singleton('clockwork.swift', function ($app) {
193
			return new SwiftDataSource($app['mailer']->getSwiftMailer());
194
		});
195
 
196
		$this->app->singleton('clockwork.twig', function ($app) {
197
			return new TwigDataSource($app['twig']);
198
		});
199
 
200
		$this->app->singleton('clockwork.views', function ($app) {
201
			return new LaravelViewsDataSource(
202
				$app['events'],
203
				$app['clockwork.support']->getConfig('features.views.collect_data')
204
			);
205
		});
206
 
207
		$this->app->singleton('clockwork.xdebug', function ($app) {
208
			return new XdebugDataSource;
209
		});
210
	}
211
 
212
	// Set up aliases for all Clockwork parts so they can be resolved by type-hinting
213
	protected function registerAliases()
214
	{
215
		$this->app->alias('clockwork', Clockwork::class);
216
 
217
		$this->app->alias('clockwork.authenticator', AuthenticatorInterface::class);
218
		$this->app->alias('clockwork.storage', StorageInterface::class);
219
		$this->app->alias('clockwork.support', ClockworkSupport::class);
220
 
221
		$this->app->alias('clockwork.cache', LaravelCacheDataSource::class);
222
		$this->app->alias('clockwork.eloquent', EloquentDataSource::class);
223
		$this->app->alias('clockwork.events', LaravelEventsDataSource::class);
224
		$this->app->alias('clockwork.laravel', LaravelDataSource::class);
225
		$this->app->alias('clockwork.notifications', LaravelNotificationsDataSource::class);
226
		$this->app->alias('clockwork.queue', LaravelQueueDataSource::class);
227
		$this->app->alias('clockwork.redis', LaravelRedisDataSource::class);
228
		$this->app->alias('clockwork.swift', SwiftDataSource::class);
229
		$this->app->alias('clockwork.xdebug', XdebugDataSource::class);
230
	}
231
 
232
	// Register event listeners
233
	protected function registerEventListeners()
234
	{
235
		$this->app->booted(function () {
236
			$this->app['clockwork.support']->addDataSources()->listenToEvents();
237
		});
238
	}
239
 
240
	// Register middleware
241
	protected function registerMiddleware()
242
	{
243
		$kernel = $this->app[\Illuminate\Contracts\Http\Kernel::class];
244
 
245
		if (method_exists($kernel, 'hasMiddleware') && $kernel->hasMiddleware(ClockworkMiddleware::class)) return;
246
 
247
		$kernel->prependMiddleware(ClockworkMiddleware::class);
248
	}
249
 
250
	protected function registerRoutes()
251
	{
252
		$this->app['router']->get('/__clockwork/{id}/extended', 'Clockwork\Support\Laravel\ClockworkController@getExtendedData')
253
			->where('id', '([0-9-]+|latest)');
254
		$this->app['router']->get('/__clockwork/{id}/{direction?}/{count?}', 'Clockwork\Support\Laravel\ClockworkController@getData')
255
			->where('id', '([0-9-]+|latest)')->where('direction', '(next|previous)')->where('count', '\d+');
256
		$this->app['router']->put('/__clockwork/{id}', 'Clockwork\Support\Laravel\ClockworkController@updateData');
257
		$this->app['router']->post('/__clockwork/auth', 'Clockwork\Support\Laravel\ClockworkController@authenticate');
258
	}
259
 
260
	protected function registerWebRoutes()
261
	{
262
		$this->app['clockwork.support']->webPaths()->each(function ($path) {
263
			$this->app['router']->get("{$path}", 'Clockwork\Support\Laravel\ClockworkController@webRedirect');
264
			$this->app['router']->get("{$path}/app", 'Clockwork\Support\Laravel\ClockworkController@webIndex');
265
			$this->app['router']->get("{$path}/{path}", 'Clockwork\Support\Laravel\ClockworkController@webAsset')
266
				->where('path', '.+');
267
		});
268
	}
269
 
270
	public function provides()
271
	{
272
		return [ 'clockwork' ];
273
	}
274
}