Skip to content

Commit

Permalink
Merge pull request #136 from ametad/fix-135
Browse files Browse the repository at this point in the history
Fix #135 Compatible with Laravel Telescope in Composer require-dev
  • Loading branch information
matchish authored Dec 3, 2020
2 parents 06c9091 + 2cbcefa commit 131fc3d
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/)

## [Unreleased]

## [4.0.3] - 2020-12-02
### Fixed
- Compatible with Laravel Telescope as dev requirement [#135](https://github.com/matchish/laravel-scout-elasticsearch/issues/135)

## [4.0.2] - 2020-10-18
### Added
- Laravel 8 Support
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "matchish/laravel-scout-elasticsearch",
"description": "Search among multiple models with ElasticSearch and Laravel Scout",
"version": "4.0.2",
"version": "4.0.3",
"keywords": [
"laravel",
"scout",
Expand Down
63 changes: 61 additions & 2 deletions src/Searchable/SearchableListFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,75 @@ private function isSearchableModel($class): bool
private function getProjectClasses(): array
{
if (self::$declaredClasses === null) {
self::$declaredClasses = [];

$configFiles = Finder::create()->files()->name('*.php')->in($this->appPath);

foreach ($configFiles->files() as $file) {
require_once $file;
if ($className = $this->classNameFromFileContents($file->getPathname())) {
self::$declaredClasses[] = $className;
}
}
self::$declaredClasses = get_declared_classes();
}

return self::$declaredClasses;
}

/**
* @param string $path
* @return string|null
* @link https://stackoverflow.com/a/7153391/1359273
*/
private function classNameFromFileContents($path)
{
$fp = fopen($path, 'r');

if (false === $fp) {
return null;
}

$class = $namespace = $buffer = '';

while (! $class) {
if (feof($fp)) {
break;
}

$buffer .= fread($fp, 512);
$tokens = token_get_all($buffer);

if (strpos($buffer, '{') === false) {
continue;
}

for ($i = 0, $iMax = count($tokens); $i < $iMax; $i++) {
if ($tokens[$i][0] === T_NAMESPACE) {
for ($j = $i + 1, $jMax = count($tokens); $j < $jMax; $j++) {
if ($tokens[$j][0] === T_STRING) {
$namespace .= $tokens[$j][1];
} elseif ($tokens[$j] === '{' || $tokens[$j] === ';') {
break;
}
}
}

if ($tokens[$i][0] === T_CLASS) {
for ($j = $i + 1, $jMax = count($tokens); $j < $jMax; $j++) {
if ($tokens[$j] === '{') {
$class = $tokens[$i + 2][1];
}
}
}
}
}

if (! $class) {
return null;
}

return $namespace ? "{$namespace}\\{$class}" : $class;
}

/**
* @return Collection
*/
Expand Down
11 changes: 4 additions & 7 deletions tests/Feature/ImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,9 @@ public function test_progress_report_in_queue()
$output = new BufferedOutput();
Artisan::call('scout:import', [], $output);

$output = explode("\n", $output->fetch());
$this->assertEquals(
trans('scout::import.start', ['searchable' => Product::class]),
trim($output[0]));
$this->assertEquals(
'[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]),
trim($output[2]));
$output = array_map('trim', explode("\n", $output->fetch()));

$this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output);
$this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output);
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Searchable/SearchableListFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests\Unit\Searchable;

use Matchish\ScoutElasticSearch\Searchable\SearchableListFactory;
use Tests\TestCase;

class SearchableListFactoryTest extends TestCase
{
public function test_only_load_seachable_classes()
{
$this->assertFileExists(app()->path().'/Providers/TelescopeServiceProvider.php');

// This should NOT throw "Error: Class 'Laravel\Telescope\TelescopeApplicationServiceProvider' not found"
$factory = new SearchableListFactory(app()->getNamespace(), app()->path());

$searchable = $factory->make();

// There are 4 searchable models: Book, BookWithCustomKey, Product and Ticket
$this->assertCount(4, $searchable);
}
}
75 changes: 75 additions & 0 deletions tests/laravel/app/Providers/TelescopeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
use Laravel\Telescope\TelescopeApplicationServiceProvider;

class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
{
/*
* This class is intentionally here, not used anywhere but in a test. Do not delete.
*/

/**
* Register any application services.
*
* @return void
*/
public function register()
{
// Telescope::night();

$this->hideSensitiveRequestDetails();

Telescope::filter(function (IncomingEntry $entry) {
if ($this->app->environment('local')) {
return true;
}

return $entry->isReportableException() ||
$entry->isFailedRequest() ||
$entry->isFailedJob() ||
$entry->isScheduledTask() ||
$entry->hasMonitoredTag();
});
}

/**
* Prevent sensitive request details from being logged by Telescope.
*
* @return void
*/
protected function hideSensitiveRequestDetails()
{
if ($this->app->environment('local')) {
return;
}

Telescope::hideRequestParameters(['_token']);

Telescope::hideRequestHeaders([
'cookie',
'x-csrf-token',
'x-xsrf-token',
]);
}

/**
* Register the Telescope gate.
*
* This gate determines who can access Telescope in non-local environments.
*
* @return void
*/
protected function gate()
{
Gate::define('viewTelescope', function ($user) {
return in_array($user->email, [
//
]);
});
}
}

0 comments on commit 131fc3d

Please sign in to comment.