-
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
- Loading branch information
1 parent
197446d
commit 2017d1c
Showing
15 changed files
with
238 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"standard.enable": true, | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": "always" | ||
}, | ||
"editor.formatOnSave": true, | ||
"[javascript]": { | ||
"editor.defaultFormatter": null | ||
}, | ||
"[typescript]": { | ||
"editor.defaultFormatter": null | ||
}, | ||
"standard.autoFixOnSave": true, | ||
"standard.validate": [ | ||
"javascript", | ||
"javascriptreact", | ||
"typescript", | ||
"typescriptreact" | ||
], | ||
} |
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,48 @@ | ||
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) | ||
const definitionsToOverride = container.instanceManager.searchNotOverrideDefinitionsByObject(definitionToOverride.Object) | ||
|
||
for (const overrideArg of definitionToOverride.overrideArgs) { | ||
const argumentsToOverride = this._searchDefinitionsByClassName(overrideArg.id) | ||
const references = argumentsToOverride.map(arg => new Reference(arg.key)) | ||
|
||
for (const [, definitionFromOverride] of definitionsToOverride) { | ||
definitionFromOverride.args = [...references] | ||
} | ||
} | ||
} | ||
|
||
this._removeDefinitions(toDelete) | ||
} | ||
|
||
/** | ||
* @param {string} className | ||
* @returns {Array} - Retorna los resultados de la búsqueda | ||
*/ | ||
_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