Skip to content

Instantly share code, notes, and snippets.

@AmyStephen
Last active December 19, 2015 00:58
Show Gist options
  • Save AmyStephen/5872139 to your computer and use it in GitHub Desktop.
Save AmyStephen/5872139 to your computer and use it in GitHub Desktop.
It's a namespace.
           
| Acme                          
| | Foobar                       
| | | Controller                
| | | | FoobarController     
| | | |                           
| | | Form                       
| | | | FoobarConfigForm.php     
| | | | FoobarForm.php  
| | |                    
| | foobar.info.yml           
| | foobar.routing.yml    
| | foobar.settings                     

If it's not a namespace, then we have virtual paths - and we have: namespaces. Two separate things, two sets of maps, two types of matching, and so on.

Consider this bit of documentation for newbies.

<?php  
// Since PHP 5.3, classes have namespaces [ Yea! ] 
// ... but resources still have to use physical paths [ **gasp!!!** ] 

$config = $this->config->get('path/to/foobar.routing.yml');

[ dream sequence ]

<?php
// Tomorrow, [ drum roll ] **Virtual Paths** for resources 
// *and* Namespaces for class! [ crowd goes *crazy!* ]

$config = $this->config->get($this->locator->get('Acme/Foobar/foobar.info.yml')));

Black Magic, you say?

No, not at all. Simple, in fact.

This just builds on PHP namespaces (for classes).

Which means...

<?php
// When we define the Class Namespace, **exactly** like before...

$locator->addPath('/Acme/Foobar/', '/path/to/Acme/Foobar');
<?php
// Lo and behold, **Virtual Paths** for resources! 
// [ crowd looks *perplexed!* ]

$config = $this->config->get($this->locator->get('Acme/Foobar/foobar.info.yml')));

Them: Err? I don't get it. I see where the class namespace gets created. But, where are the virtual paths added??

Us: Well, "virtual paths" are also "virtual." That part we are being quite literal about. Really, we copied the search code out of the autoloader, removed the .php restriction, passed the locator into this class, then, when the file is requested, the locator uses the same search against the same path statements to see if a match is found -- just like it does for class. But, [ and here's where it gets confusing ] the path is returned, but the file is not included.

Them: K, so, why not just say namespace?

Us: Well, that's confusing. Better to use a different name and act as though it is two different search methods and two different sets of paths and prefixes. Later, when you get it, then, we'll call it namespaces.

Them: So, why create two other standards so that it's clear -- both use the same search and prefixes?

Us: BECAUSE IT DOES! See? Later, when it makes sense, we'll call it namespaces.

Them: Well, I guess you're right, I am confused.

Us: No, you're NOT confused. You WOULD HAVE BEEN confused if we had just called it namespaces. /mutters "newbies..."

Them: I'm sorry. I think I made it too easy.

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