-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Ability to override services from autowire (#216)
feat: ✨ Ability to override services from autowire #213
- Loading branch information
1 parent
197446d
commit 4504a5e
Showing
16 changed files
with
239 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,16 @@ name: Build | |
|
||
on: | ||
push: | ||
branches: [ master ] | ||
branches: [master] | ||
pull_request: | ||
branches: [ master ] | ||
branches: [master] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node-version: [14.x, 15.x, 16.x, 17.x] | ||
node-version: [14.x, 15.x, 16.x, 17.x, 18.x, 19.x, 20.x] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
|
@@ -24,18 +24,18 @@ jobs: | |
- run: npm test | ||
|
||
coverage: | ||
needs: [ build ] | ||
needs: [build] | ||
name: coverage | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@master | ||
- uses: actions/setup-node@master | ||
with: | ||
node-version: '17' | ||
- run: npm ci | ||
- uses: paambaati/[email protected] | ||
env: | ||
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} | ||
with: | ||
coverageCommand: npm run test:coverage | ||
coverageLocations: ${{github.workspace}}/coverage/lcov.info:lcov | ||
- uses: actions/checkout@master | ||
- uses: actions/setup-node@master | ||
with: | ||
node-version: "17" | ||
- run: npm ci | ||
- uses: paambaati/[email protected] | ||
env: | ||
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} | ||
with: | ||
coverageCommand: npm run test:coverage | ||
coverageLocations: ${{github.workspace}}/coverage/lcov.info:lcov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ node_modules/ | |
coverage/ | ||
coverage.lcov | ||
npm-debug.log | ||
.DS_STORE | ||
.DS_STORE | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Reference from '../Reference' | ||
|
||
export default class AutowireOverridePass { | ||
/** | ||
* @param {ContainerBuilder} container | ||
*/ | ||
process (container) { | ||
this._definitions = container.instanceManager.definitions | ||
const overrideDefinitions = container.instanceManager.searchDefinitionsToOverrideArgs() | ||
const toDelete = [] | ||
|
||
for (const [key, definitionToOverride] of overrideDefinitions) { | ||
toDelete.push(key) | ||
this._processOverride(definitionToOverride, container) | ||
} | ||
|
||
this._removeDefinitions(toDelete) | ||
} | ||
|
||
_processOverride (definitionToOverride, container) { | ||
const definitionsToOverride = container.instanceManager.searchNotOverrideDefinitionsByObject(definitionToOverride.Object) | ||
|
||
for (const overrideArg of definitionToOverride.overrideArgs) { | ||
const references = this._getReferencesForOverrideArg(overrideArg.id) | ||
|
||
for (const [, definitionFromOverride] of definitionsToOverride) { | ||
definitionFromOverride.args = [...references] | ||
} | ||
} | ||
} | ||
|
||
_getReferencesForOverrideArg (overrideArgId) { | ||
const argumentsToOverride = this._searchDefinitionsByClassName(overrideArgId) | ||
return argumentsToOverride.map(arg => new Reference(arg.key)) | ||
} | ||
|
||
_searchDefinitionsByClassName (className) { | ||
const result = [] | ||
for (const [key, definition] of this._definitions) { | ||
if (definition.Object?.name === className) { | ||
result.push({ key, definition }) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
_removeDefinitions (keysToDelete) { | ||
for (const key of keysToDelete) { | ||
this._definitions.delete(key) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default class CannotAutowireOverrideSearch extends Error { | ||
constructor (className = null) { | ||
super(`Cannot Autowire Override ${className}`) | ||
this.name = 'CannotAutowireOverrideSearch' | ||
this.stack = (new Error()).stack | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
test/Resources-ts/Autowire-Override/config/services-not-exists.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
rootDir: ../src | ||
|
||
App.NoExists: | ||
class: './FooBarAutowireOverride' | ||
override_arguments: | ||
- '@Caca' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
rootDir: ../src | ||
|
||
App.FooBarAutowireOverride: | ||
class: './FooBarAutowireOverride' | ||
override_arguments: | ||
- '@CiAdapter' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default interface Adapter { | ||
toString(): string; | ||
} |
7 changes: 7 additions & 0 deletions
7
test/Resources-ts/Autowire-Override/src/Adapters/BarAdapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Adapter from "../Adapter"; | ||
|
||
export default class BarAdapter implements Adapter { | ||
toString(): string { | ||
return "bar"; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/Resources-ts/Autowire-Override/src/Adapters/CiAdapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Adapter from "../Adapter"; | ||
|
||
export default class CiAdapter implements Adapter { | ||
toString(): string { | ||
return 'ci'; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/Resources-ts/Autowire-Override/src/Adapters/FooAdapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Adapter from "../Adapter"; | ||
|
||
export default class FooAdapter implements Adapter { | ||
toString(): string { | ||
return "foo"; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
test/Resources-ts/Autowire-Override/src/FooBarAutowireOverride.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Adapter from "./Adapter"; | ||
|
||
export default class FooBarAutowireOverride { | ||
constructor(private readonly _adapter: Adapter) {} | ||
|
||
getString(): string { | ||
return this._adapter.toString(); | ||
} | ||
|
||
get adapter() { | ||
return this._adapter | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters