Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
Updated for pthreads 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Jan 23, 2023
1 parent d5ff1b5 commit 407caf5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ jobs:
- uses: actions/checkout@v3

- name: Setup PHP
uses: pmmp/setup-php-action@87cc7c2f65e10e0e64be54a3e41a62a4c61bd1a9
uses: pmmp/setup-php-action@6dd74c145250109942b08fc63cd5118edd2fd256
with:
php-version: ${{ matrix.php }}
install-path: "./bin"
pm-version-major: "5"

- name: Cache Composer packages
id: composer-cache
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "LGPL-3.0",
"require": {
"php": "^8.0",
"ext-pthreads": "~3.2.0 || ^4.0",
"ext-pthreads": "^5.0",
"ext-reflection": "*"
},
"require-dev": {
Expand Down
37 changes: 23 additions & 14 deletions src/BaseClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@

declare(strict_types=1);

class BaseClassLoader extends \Threaded implements DynamicClassLoader{
class BaseClassLoader extends \ThreadedBase implements DynamicClassLoader{

/** @var \Threaded|string[] */
/**
* @var \ThreadedArray|string[]
* @phpstan-var \ThreadedArray<int, string>
*/
private $fallbackLookup;
/** @var \Threaded|string[][] */
/**
* @var \ThreadedArray|string[][]
* @phpstan-var \ThreadedArray<string, \ThreadedArray<int, string>>
*/
private $psr4Lookup;

public function __construct(){
$this->fallbackLookup = new \Threaded;
$this->psr4Lookup = new \Threaded;
$this->fallbackLookup = new \ThreadedArray();
$this->psr4Lookup = new \ThreadedArray();
}

protected function normalizePath(string $path) : string{
Expand All @@ -46,17 +52,19 @@ public function addPath(string $namespacePrefix, string $path, bool $prepend = f
}else{
$namespacePrefix = trim($namespacePrefix, '\\') . '\\';
$this->psr4Lookup->synchronized(function() use ($namespacePrefix, $path, $prepend) : void{
/** @var \Threaded|null $list */
$list = $this->psr4Lookup[$namespacePrefix] ?? null;
if($list === null){
$list = $this->psr4Lookup[$namespacePrefix] = new \Threaded;
$list = $this->psr4Lookup[$namespacePrefix] = new \ThreadedArray();
}
$this->appendOrPrependLookupEntry($list, $path, $prepend);
});
}
}

protected function appendOrPrependLookupEntry(\Threaded $list, string $entry, bool $prepend) : void{
/**
* @phpstan-param \ThreadedArray<int, string> $list
*/
protected function appendOrPrependLookupEntry(\ThreadedArray $list, string $entry, bool $prepend) : void{
if($prepend){
$entries = $this->getAndRemoveLookupEntries($list);
$list[] = $entry;
Expand All @@ -69,12 +77,15 @@ protected function appendOrPrependLookupEntry(\Threaded $list, string $entry, bo
}

/**
* @return mixed[]
* @return string[]
*
* @phpstan-param \ThreadedArray<int, string> $list
* @phpstan-return list<string>
*/
protected function getAndRemoveLookupEntries(\Threaded $list) : array{
protected function getAndRemoveLookupEntries(\ThreadedArray $list) : array{
$entries = [];
while($list->count() > 0){
$entries[] = $list->shift();
while(($entry = $list->shift()) !== null){
$entries[] = $entry;
}
return $entries;
}
Expand Down Expand Up @@ -127,8 +138,6 @@ public function findClass(string $name) : ?string{
while(false !== $lastPos = strrpos($subPath, '\\')){
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath . '\\';

/** @var \Threaded|null $lookup */
$lookup = $this->psr4Lookup[$search] ?? null;
if($lookup !== null){
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
Expand Down
26 changes: 22 additions & 4 deletions tests/phpstan/stubs/pthreads.stub
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
<?php

/**
* @implements \Traversable<array-key, mixed>
* @implements \IteratorAggregate<array-key, mixed>
*/
class Threaded implements \Traversable{
abstract class ThreadedBase implements \IteratorAggregate{

/**
* @template TReturn
* @param callable() : TReturn $function
* @param \Closure() : TReturn $function
* @param mixed ...$args
* @return TReturn
*/
public function synchronized(callable $function, ...$args){}
public function synchronized(\Closure $function, mixed ...$args) : mixed{}
}

/**
* @template TKey of array-key
* @template TValue
* @implements ArrayAccess<TKey, TValue>
*/
final class ThreadedArray extends ThreadedBase implements Countable, ArrayAccess{

/**
* @return TValue|null
*/
public function pop() : mixed{}

/**
* @return TValue|null
*/
public function shift() : mixed{}
}

0 comments on commit 407caf5

Please sign in to comment.