Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Resonance
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
distantmagic
Resonance
Commits
d9328fa7
Commit
d9328fa7
authored
1 year ago
by
Mateusz Charytoniuk
Browse files
Options
Downloads
Patches
Plain Diff
fix: race condition in the DI container
parent
94ca91a0
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/DependencyInjectionContainer.php
+45
-38
45 additions, 38 deletions
src/DependencyInjectionContainer.php
src/SingletonProvider/ConfigurationProvider/RedisConfigurationProvider.php
+1
-0
1 addition, 0 deletions
...ider/ConfigurationProvider/RedisConfigurationProvider.php
with
46 additions
and
38 deletions
src/DependencyInjectionContainer.php
+
45
−
38
View file @
d9328fa7
...
@@ -14,7 +14,6 @@ use ReflectionFunction;
...
@@ -14,7 +14,6 @@ use ReflectionFunction;
use
RuntimeException
;
use
RuntimeException
;
use
Swoole\Coroutine\WaitGroup
;
use
Swoole\Coroutine\WaitGroup
;
use
function
Swoole\Coroutine\go
;
use
function
Swoole\Coroutine\run
;
use
function
Swoole\Coroutine\run
;
readonly
class
DependencyInjectionContainer
readonly
class
DependencyInjectionContainer
...
@@ -78,31 +77,10 @@ readonly class DependencyInjectionContainer
...
@@ -78,31 +77,10 @@ readonly class DependencyInjectionContainer
*/
*/
public
function
make
(
string
$className
):
object
public
function
make
(
string
$className
):
object
{
{
if
(
$this
->
singletons
->
has
(
$className
))
{
return
$this
->
singletons
->
get
(
$className
);
}
if
(
$this
->
providers
->
hasKey
(
$className
))
{
/**
* @var bool $coroutineResult
*/
$coroutineResult
=
run
(
function
()
use
(
$className
)
{
$this
->
makeSingleton
(
$className
,
new
Set
());
});
if
(
!
$coroutineResult
)
{
throw
new
RuntimeException
(
'Container event loop failed'
);
}
return
$this
->
singletons
->
get
(
$className
);
}
$reflectionClass
=
new
ReflectionClass
(
$className
);
/**
/**
* @var
Map<string,object> $parameters
* @var
null|TSingleton
*/
*/
$
parameters
=
new
Map
()
;
$
ret
=
null
;
/**
/**
* WaitGroup is not necesary here since `run` is going to wait for all
* WaitGroup is not necesary here since `run` is going to wait for all
...
@@ -110,26 +88,19 @@ readonly class DependencyInjectionContainer
...
@@ -110,26 +88,19 @@ readonly class DependencyInjectionContainer
*
*
* @var bool $coroutineResult
* @var bool $coroutineResult
*/
*/
$coroutineResult
=
run
(
function
()
use
(
$parameters
,
$reflectionClass
)
{
$coroutineResult
=
run
(
function
()
use
(
$className
,
&
$ret
)
{
foreach
(
new
SingletonConstructorParametersIterator
(
$reflectionClass
)
as
$name
=>
$typeClassName
)
{
$ret
=
$this
->
doMake
(
$className
);
$cid
=
go
(
function
()
use
(
$name
,
$parameters
,
$typeClassName
)
{
$parameters
->
put
(
$name
,
$this
->
makeSingleton
(
$typeClassName
,
new
Set
()),
);
});
if
(
!
is_int
(
$cid
))
{
throw
new
RuntimeException
(
'Unable to start Container coroutine'
);
}
}
});
});
if
(
!
$coroutineResult
)
{
if
(
!
$coroutineResult
)
{
throw
new
RuntimeException
(
'Container event loop failed'
);
throw
new
RuntimeException
(
'Container event loop failed'
);
}
}
return
$reflectionClass
->
newInstance
(
...
$parameters
->
toArray
());
if
(
!
(
$ret
instanceof
$className
))
{
throw
new
LogicException
(
'Unable to make an instance of '
.
$className
);
}
return
$ret
;
}
}
public
function
registerSingletons
():
void
public
function
registerSingletons
():
void
...
@@ -176,6 +147,42 @@ readonly class DependencyInjectionContainer
...
@@ -176,6 +147,42 @@ readonly class DependencyInjectionContainer
$this
->
collections
->
get
(
$collectionName
)
->
add
(
$providedClassName
);
$this
->
collections
->
get
(
$collectionName
)
->
add
(
$providedClassName
);
}
}
/**
* @template TSingleton
*
* @param class-string<TSingleton> $className
*
* @return TSingleton
*/
private
function
doMake
(
string
$className
):
object
{
if
(
$this
->
singletons
->
has
(
$className
))
{
return
$this
->
singletons
->
get
(
$className
);
}
if
(
$this
->
providers
->
hasKey
(
$className
))
{
$this
->
makeSingleton
(
$className
,
new
Set
());
return
$this
->
singletons
->
get
(
$className
);
}
$reflectionClass
=
new
ReflectionClass
(
$className
);
/**
* @var Map<string,object> $parameters
*/
$parameters
=
new
Map
();
foreach
(
new
SingletonConstructorParametersIterator
(
$reflectionClass
)
as
$name
=>
$typeClassName
)
{
$parameters
->
put
(
$name
,
$this
->
makeSingleton
(
$typeClassName
,
new
Set
()),
);
}
return
$reflectionClass
->
newInstance
(
...
$parameters
->
toArray
());
}
/**
/**
* @template TSingleton
* @template TSingleton
*
*
...
...
This diff is collapsed.
Click to expand it.
src/SingletonProvider/ConfigurationProvider/RedisConfigurationProvider.php
+
1
−
0
View file @
d9328fa7
...
@@ -18,6 +18,7 @@ use Nette\Schema\Schema;
...
@@ -18,6 +18,7 @@ use Nette\Schema\Schema;
* db_index: int,
* db_index: int,
* host: string,
* host: string,
* password: string,
* password: string,
* pool_size: int,
* port: int,
* port: int,
* prefix: string,
* prefix: string,
* timeout: int,
* timeout: int,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment