Skip to content

Instantly share code, notes, and snippets.

@mneuhaus
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mneuhaus/31bd10aa077edd6acdfe to your computer and use it in GitHub Desktop.
Save mneuhaus/31bd10aa077edd6acdfe to your computer and use it in GitHub Desktop.
Extending existing Models/Classes

Flow builds for most classes a proxy for Dependency injection, lazy loading, etc it does this by creating a new file in the Code Cache that contains the contents of the original class appended with the suffix "_Original" and a class with the name of the real className that inherits from that, in this class is some magic for dependency injection, aop and lazy loading. For example:

Given we have a class named \My\Package\Foo
Then Flow will create a file called My_Package_Foo.php
This file contains the code of the class "\My\Package\Foo" with a className suffix "_Original" -> \My\Package\Foo_Original
This file also contains di, aop, and lazyloading code in the generated "\My\Package\Foo" class that inherits from \My\Package\Foo_Original

Proposal

We could use this mechanism, to enable users to "Inject" whole classes in between the generated and the original class like this:

Objects.yaml

'\My\Package\Foo'
  inject:
    - 
      className: \Some\Package\FooExtension
      position: start
    

This would trigger the ProxyBuilder to create an extension chain like this:

<?php
namespace \My\Package;
class Foo_Original {
  // Code from the original classname
}

namespace \Some\Package;
class FooExtension extends \My\Package\Foo_Original {

}

namespace \My\Package;
class Foo extends Foo_Original extends \Some\Package\FooExtension {
  // Generated by the ProxyBuilder containing di, aop, etc
}
@cognifloyd
Copy link

Instead of this:

'\My\Package\Foo'
  inject:
    - 
      className: \Some\Package\FooExtension
      position: start

what about this:

class Foo {
    //original stuff here
}

/**
 * @Flow\Expands(\My\Package\Foo)
 */
class FooExtension {
    //extra stuff here
}

@cognifloyd
Copy link

Another thought: instead of making FooExtension be a parent class of class Foo, we could have AOP introduce the extra features as traits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment