Skip to content

Commit 936a30e

Browse files
authored
Merge pull request #114 from rodrigolinsr/php-8-laravel-8
PHP 8 & Laravel 8 upgrade
2 parents 8ea1fc9 + e0863ca commit 936a30e

9 files changed

+61
-48
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ language: php
33
php:
44
- 7.3
55
- 7.4
6+
- 8.0
67

78
sudo: false
89

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
],
2020
"require": {
21-
"php": "^7.3",
21+
"php": "^7.3|^8.0",
2222
"illuminate/console": "^8.0",
2323
"illuminate/filesystem": "^8.0",
2424
"illuminate/support": "^8.0",
@@ -27,7 +27,7 @@
2727
"require-dev": {
2828
"illuminate/database": "^8.0",
2929
"mockery/mockery": "^1.0",
30-
"phpunit/phpunit": "^8.5"
30+
"phpunit/phpunit": "^9.0"
3131
},
3232
"autoload": {
3333
"psr-4": {

phpunit.xml

+13-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="phpunit.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnError="false"
11-
stopOnFailure="false"
12-
verbose="true"
13-
>
14-
<testsuites>
15-
<testsuite name="Laravel Annotations Test Suite">
16-
<directory suffix="Test.php">./tests</directory>
17-
</testsuite>
18-
</testsuites>
19-
<filter>
20-
<whitelist processUncoveredFilesFromWhitelist="true">
21-
<directory suffix=".php">./src</directory>
22-
</whitelist>
23-
</filter>
24-
</phpunit>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="phpunit.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnError="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage processUncoveredFiles="true">
4+
<include>
5+
<directory suffix=".php">./src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Laravel Annotations Test Suite">
10+
<directory suffix="Test.php">./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/AnnotationsServiceProvider.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
use Collective\Annotations\Database\Eloquent\Annotations\Scanner as ModelScanner;
99
use Collective\Annotations\Events\Annotations\Scanner as EventScanner;
1010
use Collective\Annotations\Routing\Annotations\Scanner as RouteScanner;
11-
use Illuminate\Container\Container;
1211
use Illuminate\Contracts\Foundation\Application;
1312
use Illuminate\Support\ServiceProvider;
1413

1514
class AnnotationsServiceProvider extends ServiceProvider
1615
{
16+
use DetectsApplicationNamespace;
17+
1718
/**
1819
* The commands to be registered.
1920
*
@@ -446,7 +447,7 @@ public function routeScans()
446447
if ($this->scanControllers) {
447448
$classes = array_merge(
448449
$classes,
449-
$this->getClassesFromNamespace(Container::getInstance()->getNamespace().'Http\\Controllers')
450+
$this->getClassesFromNamespace($this->getAppNamespace().'Http\\Controllers')
450451
);
451452
}
452453

@@ -477,7 +478,7 @@ public function modelScans()
477478
public function convertNamespaceToPath($namespace)
478479
{
479480
// remove the app namespace from the namespace if it is there
480-
$appNamespace = Container::getInstance()->getNamespace();
481+
$appNamespace = $this->getAppNamespace();
481482

482483
if (substr($namespace, 0, strlen($appNamespace)) == $appNamespace) {
483484
$namespace = substr($namespace, strlen($appNamespace));
@@ -510,6 +511,6 @@ public function getClassesFromNamespace($namespace, $base = null)
510511
*/
511512
protected function getAllClasses()
512513
{
513-
return $this->getClassesFromNamespace(Container::getInstance()->getNamespace());
514+
return $this->getClassesFromNamespace($this->getAppNamespace());
514515
}
515516
}

src/Console/RouteScanCommand.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace Collective\Annotations\Console;
44

55
use Collective\Annotations\AnnotationsServiceProvider;
6+
use Collective\Annotations\DetectsApplicationNamespace;
67
use Illuminate\Console\Command;
7-
use Illuminate\Container\Container;
88
use Illuminate\Filesystem\Filesystem;
99
use Symfony\Component\Console\Input\InputOption;
1010

1111
class RouteScanCommand extends Command
1212
{
13+
use DetectsApplicationNamespace;
14+
1315
/**
1416
* The console command name.
1517
*
@@ -99,7 +101,7 @@ protected function getOutputPath()
99101
*/
100102
protected function getOptions()
101103
{
102-
$namespace = Container::getInstance()->getNamespace().'Http\Controllers';
104+
$namespace = $this->getAppNamespace().'Http\Controllers';
103105

104106
return [
105107
['namespace', null, InputOption::VALUE_OPTIONAL, 'The root namespace for the controllers.', $namespace],

src/DetectsApplicationNamespace.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace Collective\Annotations;
3+
4+
use Illuminate\Container\Container;
5+
6+
/**
7+
* Trait DetectsApplicationNamespace
8+
*
9+
* This got deprecated in Laravel 7, so fastest solution would be to just have this here.
10+
*
11+
* @package Collective\Annotations
12+
*/
13+
trait DetectsApplicationNamespace
14+
{
15+
/**
16+
* Get the application namespace.
17+
*
18+
* @return string
19+
*/
20+
protected function getAppNamespace()
21+
{
22+
return Container::getInstance()->getNamespace();
23+
}
24+
}

src/Filesystem/ClassFinder.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ protected function tokenIsClassOrInterface($token)
110110
*/
111111
protected function isPartOfNamespace($token)
112112
{
113-
return is_array($token) && ($token[0] == T_STRING || $token[0] == T_NS_SEPARATOR);
113+
/**
114+
* T_NAME_QUALIFIED >= PHP 8.0
115+
* T_NS_SEPARATOR <= PHP 8.0
116+
*/
117+
$compare = (defined('T_NAME_QUALIFIED') ? T_NAME_QUALIFIED : T_NS_SEPARATOR);
118+
119+
return is_array($token) && ($token[0] == T_STRING || $token[0] == $compare);
114120
}
115121

116122
/**

src/NamespaceToPathConverterTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Collective\Annotations;
44

5-
use Illuminate\Container\Container;
65
use Illuminate\Support\Facades\App;
76

87
trait NamespaceToPathConverterTrait
98
{
9+
use DetectsApplicationNamespace;
1010

1111
/**
1212
* Convert the given namespace to a file path.
@@ -17,7 +17,7 @@ trait NamespaceToPathConverterTrait
1717
*/
1818
public function getPathFromNamespace($namespace, $base = null)
1919
{
20-
$appNamespace = Container::getInstance()->getNamespace();
20+
$appNamespace = $this->getAppNamespace();
2121

2222
// remove the app namespace from the namespace if it is there
2323
if (substr($namespace, 0, strlen($appNamespace)) == $appNamespace) {

tests/AnnotationsServiceProviderTest.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function setUp(): void
2727

2828
public function testConvertNamespaceToPath()
2929
{
30-
$this->provider = new AnnotationsServiceProviderAppNamespaceStub($this->app);
30+
$this->provider = new AnnotationsServiceProvider($this->app);
3131
$class = 'App\\Foo';
3232

3333
$result = $this->provider->convertNamespaceToPath($class);
@@ -41,7 +41,7 @@ public function testConvertNamespaceToPathWithoutRootNamespace()
4141
->andReturn('Foo\\');
4242
Container::setInstance($this->app);
4343

44-
$this->provider = new AnnotationsServiceProviderAppNamespaceStub($this->app);
44+
$this->provider = new AnnotationsServiceProvider($this->app);
4545
$class = 'App\\Foo';
4646

4747
$result = $this->provider->convertNamespaceToPath($class);
@@ -53,7 +53,7 @@ public function testGetClassesFromNamespace()
5353
{
5454
$this->app->shouldReceive('getNamespace')->once()
5555
->andReturn('App\\');
56-
$this->provider = new AnnotationsServiceProviderAppNamespaceStub($this->app);
56+
$this->provider = new AnnotationsServiceProvider($this->app);
5757
Container::setInstance($this->app);
5858

5959
$this->app->shouldReceive('make')
@@ -69,13 +69,3 @@ public function testGetClassesFromNamespace()
6969
$this->assertEquals(['classes'], $results);
7070
}
7171
}
72-
73-
class AnnotationsServiceProviderAppNamespaceStub extends AnnotationsServiceProvider
74-
{
75-
public $appNamespace = 'App';
76-
77-
public function getAppNamespace()
78-
{
79-
return $this->appNamespace;
80-
}
81-
}

0 commit comments

Comments
 (0)