0

With the Zend Framework Tool (ZFTool) one can make the application faster, using the classmap- (instead of the namespace-) based class loading (s. here).

$ cd /path/to/my_project
$ cd module/MyModule/
$ zf classmap generate . ./autoload_classmap.php --overwrite

Is it possible / How to do the same for library modules (Zend library and custom libraries)?

automatix
  • 14,018
  • 26
  • 105
  • 230

2 Answers2

2

The classmap generator is not specific to classes under the Zend namespace, it will generate a classmap for any classes it finds. You can pass a path to the script to get it to search your library folder instead:

zf classmap generate . ./autoload_classmap.php --overwrite --library /path/to/my_project/library

See the docs for the full list of params.

Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • Thank you! I've tried this (so `[APP]/module/Application# zf classmap generate ./autoload_classmap.php --overwrite --library [APP]/vendor/MYLIB/library/MYLIB` and so `[APP]/module/Application# zf classmap generate ./autoload_classmap.php --overwrite --library ../../vendor/MYLIB/library/MYLIB`), but it's not working for me. The only way it works is `[APP]/module/Application# zf classmap generate ../../vendor/MYLIB/library/MYLIB ./autoload_classmap.php --overwrite`. But then I lose the classmap of my Application module. How to get both the module and the lib classmap into the same classmap file? – automatix Feb 19 '14 at 11:23
  • I'd imagine you want `zf classmap generate ./autoload_classmap.php --overwrite --library [APP]` - i.e. pass it a path that includes both folders (the root of your project). However if your vendor libraries are loaded via. Composer, just let composer handle the class map generation for you. – Tim Fountain Feb 19 '14 at 11:29
  • If I pass the application root as parameter, I get one classmap for the whole application (all modules, ZF lib, and my custom lib(-s)). What I want to achieve is, that the `[APP]/module/Application/autoload_classmap.php` contains (1) the Application module classes and (2) the classes placed in one of my custom libraries (and mybe later (3) the classes of the ZF library). The only way I see now is to write a shell script, that executes the `zf classmap generate` for each case and then merges several classmaps to one. – automatix Feb 19 '14 at 12:51
1

Doing this actually wouldn't be advantageous because the autoload file would simply become too big. What i can however suggest to you is to use a Module provided by Evan Coury called EdpSuperluminal.

What this Module does it, it "records" all the classes that are called within a given request and writes those into a classmap file.

The only downside to this Module is, that you'll have to create every single possible Request of your Application with a special Query-Parameter attached. That's a bit of a hassle but it's worth it. Doing it this way, you reduce the size of your Classmap to only those of the Zend Library as well as other Vendor Libraries, that you actually use. This way your classmap won't be oversized.

Other than that though, using the standard autoload_classmap.php on the /vendor directory should actually work.

And a last thing to note: Since you most likely include the Libraries (Zend, Doctrine, others...) via Composer, Composer itself creates a sort of a Classmap, but only specific to the Vendor-Namespaces. I.E "Zend", "Doctrine", "Mongo", etc.. Composer does this for the Top-Level-Namespaces only because of the above mentioned reasoning.

Community
  • 1
  • 1
Sam
  • 16,435
  • 6
  • 55
  • 89
  • Could you expand on your composer footnote? Composer has a `--optimize-auoloader` flag (recommended for use in production) which generates a classmap for every file in vendor. The only reason it doesn't always do this (according to its docs) is the time it takes to generate the file. I've never found the size of the classmap to be an issue, especially when using an opt cache. – Tim Fountain Oct 14 '13 at 18:43
  • 1
    @TimFountain I didn't know about this to be honest. With OpCache i can totally see a big file being loaded fast enough. However when it comes down to non-op-cache-systems i personally found (at least in my env), that a huge classmap actually decreases loadtime, EdpSuperluminal in my case has always been faster than a full classmap. This should hold true for opcache environments, too, tho. But Full classmap MAY be faster than partial after all, if composer says so i do trust them on their word ^^ – Sam Oct 14 '13 at 18:48