Quantcast

ZF2 Modules - simplify autoloading

classic Classic list List threaded Threaded
48 messages Options
123
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

ZF2 Modules - simplify autoloading

Artur Bodera
Hi guys!

After browsing those few beta modules that have been created for ZF2....

Most modules have those files exactly the same
- autoload_classmap.php
- autoload_function.php
- autoload_register.php
(hence redundant)

Can we please streamline the autoloading story?
Currently nearly all modules share the same autoloading scenario, which
is PSR-0 in a subdir of module's dir.



I think it would be better if a module loader handled that.
Or ModuleAutoloader?

For example: adding a module to an application, adds a path to
ModuleAutoloader PSR-0 folder stack:
 + /modules/ModuleA/src
 + /modules/ModuleB/src

If a classmap exists, a ModuleClassmapAutoloader adds paths
to its stack:
 + /modules/ModuleA/.classmap.php
 + /modules/ModuleB/.classmap.pho


Stat calls are cached by php, so we could even go with:

    if( is_directory($modulePath . '/src') ){
        $stdLoader->addPath($modulePath . '/src' );
    }

    if( is_file($modulePath . '/.classmap.php') ){
        $classmapLoader->registerAutoloadMap($modulePath . '/library' );
    }


This would allow for having modules that just provide classes:

/modules/SomeAdapter
    - README.md
    - src/
        - Zend
            - Db
                - Adapter
                    - Foobar.php

[CUT]
 - nothing more to find in this module



How do you like that?
Is there any special, hidden reasoning behind autoload_*.php files? :-)


A.

--
      __
     /.)\   +48 695 600 936
     \(./   [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

robertbasic
For some reason, but I might be wrong here, I think it's already changed to
something like in Akrabat's tutorial:

https://github.com/akrabat/zf2-tutorial/blob/master/modules/Album/Module.php#L17

I *think* I saw someone somewhere suggest that we should just go with this
kind of autoloading.

On Fri, Oct 28, 2011 at 3:54 PM, Artur Bodera <[hidden email]> wrote:

> Hi guys!
>
> After browsing those few beta modules that have been created for ZF2....
>
> Most modules have those files exactly the same
> - autoload_classmap.php
> - autoload_function.php
> - autoload_register.php
> (hence redundant)
>
> Can we please streamline the autoloading story?
> Currently nearly all modules share the same autoloading scenario, which
> is PSR-0 in a subdir of module's dir.
>
>
>
> I think it would be better if a module loader handled that.
> Or ModuleAutoloader?
>
> For example: adding a module to an application, adds a path to
> ModuleAutoloader PSR-0 folder stack:
>  + /modules/ModuleA/src
>  + /modules/ModuleB/src
>
> If a classmap exists, a ModuleClassmapAutoloader adds paths
> to its stack:
>  + /modules/ModuleA/.classmap.php
>  + /modules/ModuleB/.classmap.pho
>
>
> Stat calls are cached by php, so we could even go with:
>
>    if( is_directory($modulePath . '/src') ){
>        $stdLoader->addPath($modulePath . '/src' );
>    }
>
>    if( is_file($modulePath . '/.classmap.php') ){
>        $classmapLoader->registerAutoloadMap($modulePath . '/library' );
>    }
>
>
> This would allow for having modules that just provide classes:
>
> /modules/SomeAdapter
>    - README.md
>    - src/
>        - Zend
>            - Db
>                - Adapter
>                    - Foobar.php
>
> [CUT]
>  - nothing more to find in this module
>
>
>
> How do you like that?
> Is there any special, hidden reasoning behind autoload_*.php files? :-)
>
>
> A.
>
> --
>      __
>     /.)\   +48 695 600 936
>     \(./   [hidden email]
>



--
~Robert Basic;
http://robertbasic.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

Artur Bodera
On Fri, Oct 28, 2011 at 3:57 PM, Robert Basic <[hidden email]>wrote:

> For some reason, but I might be wrong here, I think it's already changed
> to something like in Akrabat's tutorial:
>
>
> https://github.com/akrabat/zf2-tutorial/blob/master/modules/Album/Module.php#L17
>
> Nah, this above is the same as other modules. It just registers another
2 autoloaders
via AutoloaderFactory for this single module.


--
      __
     /.)\   +48 695 600 936
     \(./   [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

EvanDotPro
On Fri, Oct 28, 2011 at 7:04 AM, Artur Bodera <[hidden email]> wrote:

> On Fri, Oct 28, 2011 at 3:57 PM, Robert Basic <[hidden email]>wrote:
>
>> For some reason, but I might be wrong here, I think it's already changed
>> to something like in Akrabat's tutorial:
>>
>>
>> https://github.com/akrabat/zf2-tutorial/blob/master/modules/Album/Module.php#L17
>>
>> Nah, this above is the same as other modules. It just registers another
> 2 autoloaders
> via AutoloaderFactory for this single module.

Just to clarify, I fixed the AutoloaderFactory so that doing this does
not register two _new_ autoloaders. It is much more efficient than
that: it will only register one autolaoder of each type, and
subsequent calls to the same type of autoloader will simply set
options on the instance that is already registered. This way, if you
use the AutoloaderFactory, it will help keep your spl autoloader stack
clean. The change I'm referring to is here:
https://github.com/zendframework/zf2/commit/2c80c9682ae5b851b7c689227dbc1c9570ae4446

--
Evan Coury

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

Artur Bodera
On Fri, Oct 28, 2011 at 5:36 PM, Evan Coury <[hidden email]> wrote:

> Just to clarify, I fixed the AutoloaderFactory so that doing this does
> not register two _new_ autoloaders. It is much more efficient than
> that: it will only register one autolaoder of each type, and
> subsequent calls to the same type of autoloader will simply set
> options on the instance that is already registered. This way, if you
> use the AutoloaderFactory, it will help keep your spl autoloader stack
> clean. The change I'm referring to is here:
>
> https://github.com/zendframework/zf2/commit/2c80c9682ae5b851b7c689227dbc1c9570ae4446
>

You are correct.
Good job :-)

Now back to my proposal... can we kill those redundant autoload_* files and
sort it out for 99% of modules via moduleautoloader and is_dir() ?


--
      __
     /.)\   +48 695 600 936
     \(./   [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

weierophinney
Administrator
In reply to this post by Artur Bodera
-- Artur Bodera <[hidden email]> wrote
(on Friday, 28 October 2011, 03:54 PM +0200):
> Hi guys!
>
> After browsing those few beta modules that have been created for ZF2....
>
> Most modules have those files exactly the same
> - autoload_classmap.php
> - autoload_function.php
> - autoload_register.php
> (hence redundant)

<snip>
> Is there any special, hidden reasoning behind autoload_*.php files? :-)
<snip>

Please, please, please don't ignore the various posts (both on the ML
and in RFCs). We've indicated why we're recommending this structure, but
also that it's NOT REQUIRED.

As a refresher, the above three files allow a module to provide
autoloading information for a variety of use cases, some of which might
enable usage OUTSIDE Zend Framework. One goal of the module system is
re-usable, portable code... regardless of framework.

 * autoload_classmap.php returns an associative array indicating a
   classmap of PHP files available in the module. This can then be fed
   into autoloaders that consume class maps.
 * autoload_function.php returns a lambda that consumes the classmap
   returned by autoload_classmap.php, suitable for feeding to
   spl_autoload_register(). This is particularly useful if you want to
   craft the priority at which the autoloader is registered, or if you
   are using something like the autoloader in ZF1 (which implements its
   own stack).
 * autoload_register.php pass the lambda from autoload_function.php to
   spl_autoload_register() with default priority. This is useful for
   quickly consuming a module:

    require_once 'path/to/module/autoload_register.php';
    $foo = new module\Foo;

Again, these are NOT REQUIRED, but useful for telling a story of
re-usability outside the framework and/or module manager.

> Can we please streamline the autoloading story?
> Currently nearly all modules share the same autoloading scenario,
> which is PSR-0 in a subdir of module's dir.

This is true. But because we don't enforce the module structure, it's up
to the module class to tell us how to autoload; module developers do not
_need_ to follow PSR-0. Additionally, the StandardAutoloader is
substantially slower than using a classmap (even when having
namespace/directory pairs), and if you have a classmap, this will be a
faster solution.

> I think it would be better if a module loader handled that.
> Or ModuleAutoloader?

We have good autoloaders for this already, and Evan and Rob did some
great work with the AutoloaderFactory already to make this simpler.

If you use the AutoloaderFactory, and pass an autoloader class that has
already been loaded, it will simply _add_ configuration to it. Thus, the
following works:

    AutoloaderFactory::factory(array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',
        ),
    ));

This will _add_ the classmap in our module to an existing
ClassMapAutoloader if one already exists, or create a new one otherwise.

During development, you may want to have your autoload_classmap.php
empty at first, so that you don't need to worry about populating it
until your class structure is finalized. In that case, the following
will work:

    AutoloaderFactory::factory(array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',
        ),
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
            ),
        ),
    ));

Like the previous example, if another StandardAutoloader has been
created, then this will simply _add_ configuration to it. No new
autoloader.

Once your class structure is finalized, you can generate your final
classmap, and remove the reference to the StandardAutoloader -- but that
would be a microoptimization at best.

By taking this approach, we don't have to worry about module structure;
the module developer can do that. If they don't want to put code in a
"src/" tree, they can register the StandardAutoloader differently, or
use an entirely different autoloading system. This leaves the framework
code simpler, and puts the onus on the developer to choose a sane
strategy.

What we _could_ do is provide the above in a base Module implementation.

> For example: adding a module to an application, adds a path to
> ModuleAutoloader PSR-0 folder stack:
>  + /modules/ModuleA/src
>  + /modules/ModuleB/src
>
> If a classmap exists, a ModuleClassmapAutoloader adds paths
> to its stack:
>  + /modules/ModuleA/.classmap.php
>  + /modules/ModuleB/.classmap.pho

What you describe here is exactly how what I describe above works.


--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

EvanDotPro
In reply to this post by Artur Bodera
On Fri, Oct 28, 2011 at 6:54 AM, Artur Bodera <[hidden email]> wrote:
> Hi guys!
>
> After browsing those few beta modules that have been created for ZF2....
>
> Most modules have those files exactly the same
> - autoload_classmap.php
> - autoload_function.php
> - autoload_register.php
> (hence redundant)

These files are completely optional and are only suggested to make it
simple to consume classes from modules from non-ZF2 applications.

> Can we please streamline the autoloading story?
> Currently nearly all modules share the same autoloading scenario, which
> is PSR-0 in a subdir of module's dir.
>
>
>
> I think it would be better if a module loader handled that.
> Or ModuleAutoloader?

Zend\Loader\ModuleAutoloader has a very specific purpose of loading
ONLY the "Module" class from within modules; I'm not sure we should
overload it with additional responsibilities like this.

> For example: adding a module to an application, adds a path to
> ModuleAutoloader PSR-0 folder stack:
>  + /modules/ModuleA/src
>  + /modules/ModuleB/src

Currently the StandardAutoloader works by registering
prefixes/namespaces to certain paths. Are you suggesting we register
the module's namespace to ModuleA/src/ModuleA if it exists? Or that we
use the fallback autoloader functionality to allow any namespace to be
loaded from under ModuleA/src?

> If a classmap exists, a ModuleClassmapAutoloader adds paths
> to its stack:
>  + /modules/ModuleA/.classmap.php
>  + /modules/ModuleB/.classmap.pho
>
>
> Stat calls are cached by php, so we could even go with:
>
>    if( is_directory($modulePath . '/src') ){
>        $stdLoader->addPath($modulePath . '/src' );
>    }
>
>    if( is_file($modulePath . '/.classmap.php') ){
>        $classmapLoader->registerAutoloadMap($modulePath . '/library' );
>    }

I'm curious, does PHP maintain this cache across requests, or is it
per request? Is it different for mod_php vs php-fpm? I assume each FPM
worker process would have its own stat cache.

> This would allow for having modules that just provide classes:
>
> /modules/SomeAdapter
>    - README.md
>    - src/
>        - Zend
>            - Db
>                - Adapter
>                    - Foobar.php
>
> [CUT]
>  - nothing more to find in this module

So again, how did it know to register the 'Zend' namespace to the path
modules/SomeAdapter/src/Zend without relying on the fallback
autoloader behavior?


> How do you like that?

I'm not sure I like the idea of the framework making assumptions like
this and automatically registering autoloaders on behalf of the
modules as well as having the framework introspect into the modules
directories and files. It is very easy to explain that the framework
makes no assumptions beyond the Module class (which as I've explained
in other threads, can be bypassed via other means). If we were to add
what you've described, the only way for module authors to opt out of
this automatic behavior would be to put their classes under some other
directory name rather than src/ and rename the classmap file as well.

> Is there any special, hidden reasoning behind autoload_*.php files? :-)

See above. They are completely optional.

--
Evan Coury

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

ralphschindler
In reply to this post by Artur Bodera
Just to be clear, the original intention for my blog post on this
approach was for library code.  Using this approach guarantees that
however you plan to use this set of classes, you can find an autoloading
strategy that works for you.

Since we are talking about ZF modules, and they have a dependency on
some kind of module infrastructure, I think for distributable modules,
this autoloading story can be simplified.


>
> If a classmap exists, a ModuleClassmapAutoloader adds paths
> to its stack:
>   + /modules/ModuleA/.classmap.php
>   + /modules/ModuleB/.classmap.pho

As I'm sure others have pointed out, code paths that are stick inside
"hidden" files have proven to become hazardous to developers- this has
bitten me on a number of occasions: "where is that coming from? why
can't i see it?"

-ralph

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

weierophinney
Administrator
-- Ralph Schindler <[hidden email]> wrote
(on Friday, 28 October 2011, 11:15 AM -0500):
> Just to be clear, the original intention for my blog post on this
> approach was for library code.  Using this approach guarantees that
> however you plan to use this set of classes, you can find an
> autoloading strategy that works for you.
>
> Since we are talking about ZF modules, and they have a dependency on
> some kind of module infrastructure, I think for distributable
> modules, this autoloading story can be simplified.

Yes and no -- for modules intended to work specifically with the MVC,
yes, we can likely simplify. In cases of library code or resources,
where the functionality may be re-usable outside ZF applications easily,
I'd argue we should recommend the three files.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

ralphschindler
Yep, there-in is the crux of it.

Do we intend our ZF Modules to compose generic solutions, or do they
compose solutions that are ZF specific or, for ZF stack based applications?

I understand where Evan is coming from when he says that he cares not
what goes into a module and what it's called- but in my experience, that
kind of ambiguity leads to confusion.

Can symfony bundles work in a ZF application?

And, if our modules are so in-specific, does the concept of the vendor/
directory go away?

-ralph

On 10/28/11 11:29 AM, Matthew Weier O'Phinney wrote:

> -- Ralph Schindler<[hidden email]>  wrote
> (on Friday, 28 October 2011, 11:15 AM -0500):
>> Just to be clear, the original intention for my blog post on this
>> approach was for library code.  Using this approach guarantees that
>> however you plan to use this set of classes, you can find an
>> autoloading strategy that works for you.
>>
>> Since we are talking about ZF modules, and they have a dependency on
>> some kind of module infrastructure, I think for distributable
>> modules, this autoloading story can be simplified.
>
> Yes and no -- for modules intended to work specifically with the MVC,
> yes, we can likely simplify. In cases of library code or resources,
> where the functionality may be re-usable outside ZF applications easily,
> I'd argue we should recommend the three files.
>


--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

Artur Bodera
In reply to this post by weierophinney
On Fri, Oct 28, 2011 at 6:29 PM, Matthew Weier O'Phinney
<[hidden email]>wrote:

> -- Ralph Schindler <[hidden email]> wrote
> (on Friday, 28 October 2011, 11:15 AM -0500):
> > Just to be clear, the original intention for my blog post on this
> > approach was for library code.  Using this approach guarantees that
> > however you plan to use this set of classes, you can find an
> > autoloading strategy that works for you.
> >
> > Since we are talking about ZF modules, and they have a dependency on
> > some kind of module infrastructure, I think for distributable
> > modules, this autoloading story can be simplified.
>
> Yes and no -- for modules intended to work specifically with the MVC,
> yes, we can likely simplify. In cases of library code or resources,
> where the functionality may be re-usable outside ZF applications easily,
> I'd argue we should recommend the three files.


A collective reply below:

Thanks for explaining why one might consider shipping autoload_* files.

I could not find this notion and autoload_* schema even though I was on the
IRC meeting when we discussed modules :) I'm scanning ML throughly.
Sorry, I guess.


I agree with Ralph on modules (including the ambiguous Module class)
having roots in Zend\* components, which is slightly against the idea of
using a ZF module without ZF. I also don't think that autoloading is the
prime issue with 3rd party ZF code. I can easily imagine most ZF modules
being truly dependent on many ZF components and other ZF modules,
which brings us to "ZF module _must_ have a working ZF" to work.

"Explaining autoloading story" with those 3 files does not help with
refactoring zf-specific module to work with any other framework, which
means we're back to square one.

I stopped having a strong opinion on this one, because I see you have
more goals and assumptions for modules that I had. I was fine
with zf modules being kinda zf-specific, with the ability to rip-out src/
directory if anyone wants, and try to adopt it to SF2 or whatever.


As for my example of a module holding _just_ extra classes.....

It does not require "Module" class. It does not require special autoloading
logic. It might represent a lot of PSR-0 compliant modules that contribute
a set of extra classes to the application. Is it the same as
application-level
library/ or vendor/ ? No.
These are explicitly defined, accessed and are dedicated to third-party,
non-zf2 specific libraries. They are not auto-processed in any way.

I won't argue over that one because, yes I know, autoloaderfactory
currently
maintains a single stack of directories and classmaps. It would contribute
to clarity though. (i.e. organization of code, view scripts, etc. without
every module having "Module" class and performing any explicit tasks).


The overal goal of mine is modules being more static, declarative,
structured.
I don't like modules being just
"a dummy class that has init() method which does whatever the hell it wants"


--
      __
     /.)\   +48 695 600 936
     \(./   [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

weierophinney
Administrator
-- Artur Bodera <[hidden email]> wrote
(on Friday, 28 October 2011, 06:50 PM +0200):
> The overal goal of mine is modules being more static, declarative,
> structured.  I don't like modules being just "a dummy class that has
> init() method which does whatever the hell it wants"

What we decided during the IRC meetings is that we don't want to dictate
structure, but that we'll provide a _recommended_ structure. This can be
enforced by tooling -- i.e., generating a new module or adding code to a
module using a console tool would automatically do so in the recommended
structure.

Beyond that, though, no enforcement is necessary beyond having a Module
class in the module's namespace, optionally with an init() method if it
wants to tie into the module manager's intialization routines.

These are separate concerns, really -- structure is about
discoverability of what a module provides, and the Module class is about
providing information to the module manager.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

Artur Bodera
On Fri, Oct 28, 2011 at 7:05 PM, Matthew Weier O'Phinney
<[hidden email]>wrote:

> What we decided during the IRC meetings is that we don't want to dictate
> structure, but that we'll provide a _recommended_ structure. [...]
>

Agreed.
What do we need that structure for, though?
(apart from the looks)

I'd say discoverability, easier configuration, defaults allowing for having
less code, more functions, less verbosiness, room for optimization
(compilation, caching etc.)



> These are separate concerns, really -- structure is about
> discoverability of what a module provides, and the Module class is about
> providing information to the module manager.


If "structure" is a source of information, why do we need the Module class
for?

IMO "Module class" should _only_ be used for the following 2 cases:

   1. Overriding defaults (such as exotic autoloading)
   2. Implementing additional PHP logic (i.e. with init() method that can
   perform any tasks imaginable)

Structure could be used for holding recognizable (common) assets
that are (one way or another) handled by ZF in an automated way.



A.

--
      __
     /.)\   +48 695 600 936
     \(./   [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

EvanDotPro
In reply to this post by Artur Bodera
On Fri, Oct 28, 2011 at 9:50 AM, Artur Bodera <[hidden email]> wrote:
> I agree with Ralph on modules (including the ambiguous Module class)
> having roots in Zend\* components, which is slightly against the idea of
> using a ZF module without ZF. I also don't think that autoloading is the
> prime issue with 3rd party ZF code. I can easily imagine most ZF modules
> being truly dependent on many ZF components and other ZF modules,
> which brings us to "ZF module _must_ have a working ZF" to work.

It's true that modules are ambiguous, but aside from educating users,
I do not see this as a negative. I agree with how Ralph Schindler put
it on IRC a few minutes ago: "This is not a technical issue, it's a
documentation/education issue ultimately."

> "Explaining autoloading story" with those 3 files does not help with
> refactoring zf-specific module to work with any other framework, which
> means we're back to square one.
>
> I stopped having a strong opinion on this one, because I see you have
> more goals and assumptions for modules that I had. I was fine
> with zf modules being kinda zf-specific, with the ability to rip-out src/
> directory if anyone wants, and try to adopt it to SF2 or whatever.

That's fine... the entire point here is that it doesn't matter, though.

> As for my example of a module holding _just_ extra classes.....
>
> It does not require "Module" class. It does not require special autoloading
> logic. It might represent a lot of PSR-0 compliant modules that contribute
> a set of extra classes to the application. Is it the same as
> application-level
> library/ or vendor/ ? No.
> These are explicitly defined, accessed and are dedicated to third-party,
> non-zf2 specific libraries. They are not auto-processed in any way.

I understand what you're saying: You want to be able to use the module
system to automatically set up the autoloading for class files in
module directories, even if they don't have a module class. You want
to accomplish this by scanning for ModuleFoo/src and/or a classmap. I
get it. I don't see why the framework should make these assumptions
and do this work automatically.

> The overal goal of mine is modules being more static, declarative,
> structured.
> I don't like modules being just
> "a dummy class that has init() method which does whatever the hell it wants"

So is checking for src/ and/or a classmap on each module your
suggestion for this? Or do you have some suggestions for how we should
somehow limit modules to make them less ambiguous?

--
Evan Coury

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

EvanDotPro
In reply to this post by Artur Bodera
Hi Artur,

On Fri, Oct 28, 2011 at 8:42 AM, Artur Bodera <[hidden email]> wrote:

> On Fri, Oct 28, 2011 at 5:36 PM, Evan Coury <[hidden email]> wrote:
>>
>> Just to clarify, I fixed the AutoloaderFactory so that doing this does
>> not register two _new_ autoloaders. It is much more efficient than
>> that: it will only register one autolaoder of each type, and
>> subsequent calls to the same type of autoloader will simply set
>> options on the instance that is already registered. This way, if you
>> use the AutoloaderFactory, it will help keep your spl autoloader stack
>> clean. The change I'm referring to is here:
>>
>> https://github.com/zendframework/zf2/commit/2c80c9682ae5b851b7c689227dbc1c9570ae4446
>
> You are correct.
> Good job :-)
> Now back to my proposal... can we kill those redundant autoload_* files and
> sort it out for 99% of modules via moduleautoloader and is_dir() ?

I've created a module for you: https://github.com/EvanDotPro/EdpAutoModules :)

It automatically sets up the autoloaders for each enabled module
exactly how you described in this thread so that the modules
themselves do not need to set up autoloaders if they provide either an
src/ dir or an autoload_classmap.php file.

It does still require at least an empty Module class to be present for
the enabled modules simply because that's what the module loader and
module manager looks for currently.

Personally, I would not use this module in my applications.

--
Evan Coury

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

Artur Bodera
On Fri, Oct 28, 2011 at 9:17 PM, Evan Coury <[hidden email]> wrote:

> I've created a module for you:
> https://github.com/EvanDotPro/EdpAutoModules :)
> [...]


Thank you for your time writing this :-)

It misses the point though. Please see my post above.

It should be ModuleManager/ModuleAutoloader's responsibility to load
modules.
And it does that!... but skips autoloading of their classes, which is
counter-intuitive (for me) and requires extra explicit logic inside Module
class
that every module has to have (otherwise it will fail to find any class).

--
      __
     /.)\   +48 695 600 936
     \(./   [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

EvanDotPro
On Fri, Oct 28, 2011 at 12:21 PM, Artur Bodera <[hidden email]> wrote:

> On Fri, Oct 28, 2011 at 9:17 PM, Evan Coury <[hidden email]> wrote:
>>
>> I've created a module for you:
>> https://github.com/EvanDotPro/EdpAutoModules :)
>> [...]
>
> Thank you for your time writing this :-)
> It misses the point though. Please see my post above.
> It should be ModuleManager/ModuleAutoloader's responsibility to load
> modules.

Zend\Loader\ModuleAutoloader's responsibility is to load the Module
class, not the classes within a module. That's the module's
responsibility if it wants them autoloaded. I don't see why we would
add this to Zend\Loader\ModuleAutoloader; that is beyond its
responsibility.

> And it does that!... but skips autoloading of their classes, which is
> counter-intuitive (for me) and requires extra explicit logic inside Module
> class
> that every module has to have (otherwise it will fail to find any class).

I have modules I've written for an internal project that are nothing
more than some DI config and some phtml files.

--
Evan Coury.

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

Gregory
In reply to this post by EvanDotPro
On Fri, Oct 28, 2011 at 1:50 PM, Evan Coury <[hidden email]> wrote:
> I don't see why the framework should make these assumptions
> and do this work automatically.

<snip>

>> I don't like modules being just
>> "a dummy class that has init() method which does whatever the hell it wants"

Is it late for me or do the above two comments sound contradicting :)

I may be wrong but it does seem like there are two objectives here,
one is naturally dependent on the other but what we're not seeing is
how the former is going to play out nicely... bare with me..

[1] Discrete components
[2] An application framework

If I'm understanding Arthur I think inline with [2] one might expect
the array of autoloader options to be returned to the Application
layer, the Application will then aggregate these options and optimally
create a single autoload config that on subsequent requests will
circumvent any need to query individual modules for their autoloading
strategies (this would be the 80% case for most ZF2 Applications?).

I realize some might argue this is a micro optimization, but lets not
lose all of the governing architecture in previous PHP/ZF1
applications? i.e. typically before anything or much else, the
autoloading strategy for the entire application is configured... the
application then moves onto to bootstrapping the next priority -
config?.


What might also be concerning is that the Module Manager loads all of
the modules in its constructor. Doesn't this limit it's
purpose/functionality? E.g. Instantiating the Session Manager does not
(necessarily) automatically start a PHP session.



--
Greg

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

EvanDotPro
On Sat, Oct 29, 2011 at 2:16 AM, Greg <[hidden email]> wrote:

> On Fri, Oct 28, 2011 at 1:50 PM, Evan Coury <[hidden email]> wrote:
>> I don't see why the framework should make these assumptions
>> and do this work automatically.
>
> <snip>
>
>>> I don't like modules being just
>>> "a dummy class that has init() method which does whatever the hell it wants"
>
> Is it late for me or do the above two comments sound contradicting :)
>
> I may be wrong but it does seem like there are two objectives here,
> one is naturally dependent on the other but what we're not seeing is
> how the former is going to play out nicely... bare with me..
>
> [1] Discrete components
> [2] An application framework
>
> If I'm understanding Arthur I think inline with [2] one might expect
> the array of autoloader options to be returned to the Application
> layer, the Application will then aggregate these options and optimally
> create a single autoload config that on subsequent requests will
> circumvent any need to query individual modules for their autoloading
> strategies (this would be the 80% case for most ZF2 Applications?).

I already implemented this in a branch weeks ago with classmap merging
and caching and I was unable to measure any performance gain.

> I realize some might argue this is a micro optimization, but lets not
> lose all of the governing architecture in previous PHP/ZF1
> applications? i.e. typically before anything or much else, the
> autoloading strategy for the entire application is configured... the
> application then moves onto to bootstrapping the next priority -
> config?.
>
>
> What might also be concerning is that the Module Manager loads all of
> the modules in its constructor. Doesn't this limit it's
> purpose/functionality? E.g. Instantiating the Session Manager does not
> (necessarily) automatically start a PHP session.

This is something I've been meaning to change. It shouldn't
immediately call the loadModules method, you're right.

--
Evan Coury

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Modules - simplify autoloading

weierophinney
Administrator
In reply to this post by Gregory
-- Greg <[hidden email]> wrote
(on Saturday, 29 October 2011, 04:16 AM -0500):

> On Fri, Oct 28, 2011 at 1:50 PM, Evan Coury <[hidden email]> wrote:
> > I don't see why the framework should make these assumptions
> > and do this work automatically.
>
> <snip>
>
> >> I don't like modules being just
> >> "a dummy class that has init() method which does whatever the hell it wants"
>
> Is it late for me or do the above two comments sound contradicting :)
>
> I may be wrong but it does seem like there are two objectives here,
> one is naturally dependent on the other but what we're not seeing is
> how the former is going to play out nicely... bare with me..
>
> [1] Discrete components
> [2] An application framework
>
> If I'm understanding Arthur I think inline with [2] one might expect
> the array of autoloader options to be returned to the Application
> layer, the Application will then aggregate these options and optimally
> create a single autoload config that on subsequent requests will
> circumvent any need to query individual modules for their autoloading
> strategies (this would be the 80% case for most ZF2 Applications?).
>
> I realize some might argue this is a micro optimization, but lets not
> lose all of the governing architecture in previous PHP/ZF1
> applications? i.e. typically before anything or much else, the
> autoloading strategy for the entire application is configured... the
> application then moves onto to bootstrapping the next priority -
> config?.

Actually, yes and no.

Regarding configuring autoloading, this is typically done before any
other bootstrapping. It happens when the module manager loads modules,
assuming the modules configure autoloading in their init() method.
Configuration is aggregated the first time you call getMergedConfig().

These are module manager tasks, not bootstrapping tasks. Why? Because an
application consists of many discrete modules. The first step is
identifying those and aggregating autoloading and configuration.  Once
those tasks are complete, we have the information necessary to perform
bootstrapping. In a way, this is similar to Zend_Application in ZF1 --
the first steps that Zend_Application performs are setting up
autoloading and configuration, and then it passes execution on to the
bootstrap.

This is abundantly clear in the ZendSkeletonApplication's index.php:

    $moduleManager = new Zend\Module\Manager(
        $appConfig['modules'],
        new Zend\Module\ManagerOptions($appConfig['module_manager_options'])
    );

    // Create application, bootstrap, and run
    $bootstrap      = new Zend\Mvc\Bootstrap($moduleManager);

What happens here is we have a configured module manager that we pass to
the bootstrap. Again: ensure we have the environment ready (autoloading
and configuration), and then bootstrap.

> What might also be concerning is that the Module Manager loads all of
> the modules in its constructor.

Actually, it doesn't. The modules are loaded when loadModules() is
called. Currently, that happens the first time we call
getMergedConfig(), which happens inside the bootstrap. It could be
called whenever we want. But the point remains -- loading modules is not
done during construction.

> Doesn't this limit it's purpose/functionality? E.g. Instantiating the
> Session Manager does not (necessarily) automatically start a PHP
> session.

See above.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


123
Loading...