Skip to content

Instantly share code, notes, and snippets.

@jrbasso
Created December 10, 2011 23:39
Show Gist options
  • Select an option

  • Save jrbasso/1457087 to your computer and use it in GitHub Desktop.

Select an option

Save jrbasso/1457087 to your computer and use it in GitHub Desktop.
CakePHP 3.0 Namespace

CakePHP Core

  • Core namespace: Cake
  • Namespace will follow the directories, ie. Cake\Cache\Engine\ApcEngine, Cake\Controller\Controller
  • View files don't need namespaces
  • Basic functions will not be namespaced as well
  • Use the class loader defined by the PHP Standard Group, see https://gist.github.com/562509
  • Suffixes will not be removed (ie. HtmlHelper will be Cake\View\Helper\HtmlHelper instead of Cake\View\Helper\Html)
  • Remove App::uses()
  • Remove filemap
  • Support full class name in configurations, ie. DebugKit\Controller\Component\ToolbarComponent instead of DebugKit.Toolbar

Plugins

  • Plugin must use namespaces
  • The default top level namespace will be the same of plugin name, but can be configured a custom namespace using the Plugin::load()

Applications

  • Application must use namespaces
  • Include in core.php a configuration to indicate the application namespace (empty for global)
@ADmad
Copy link
Copy Markdown

ADmad commented May 22, 2012

For plugins we should use author/creator name as top level in the namespace. For eg. if i want to use simultaneously CakeDC's Search plugin and ADmad' Search plugin (with all classes named similarly), it would be possible only if author name is top level in namespace.

@markstory
Copy link
Copy Markdown

@ADmad won't that create an issue in the filesystem as well? Both plugins will be expecting to be in app/Plugin/Search/. We'd have to add even more configuration to allow custom filepath -> namespace pairs. And additionally require more namespaces. I think its worth asking how often we expect this to happen and whether or not this can be solved by ignoring the problem. If the community just chooses more unique names for things, then there is no need for all this additional configuration, namespace & filepath resolution hijinks. It feels like we're trying to solve a people/communication issue with computers.

@ADmad
Copy link
Copy Markdown

ADmad commented May 22, 2012

@markstory Well if were to include author name as top level in namespace, then as per PSR-0 wouldn't the folders be app/Plugin/CakeDC/Search and app/Plugin/ADmad/Search ?

@jrbasso
Copy link
Copy Markdown
Author

jrbasso commented May 22, 2012

@ADmad I implemented and forgot to update this document. You will be able to put the files as you described, but you will have to setup the custom namespace in the Plugin::load(). Ie, Plugin::load('ADSearch', array('namespace' => 'ADmad\Search')) will load files on APP/Plugin/ADmad/Search/....

So you can use like $components = array('ADSearch.PrgComponent');.

I will update the document with that.

@ADmad
Copy link
Copy Markdown

ADmad commented May 23, 2012

@jrbasso That takes care of two plugins with same name. Will that also allow me to simultaneously load PrgComponent from both the Search plugins? If that's the case then I am happy :)

@jrbasso
Copy link
Copy Markdown
Author

jrbasso commented May 23, 2012

Yes, you can use the className option available in the collections, so you can load simultaneously like that:

public $components = array(
  'ADPgr' => array(
    'className' => 'ADmad\Search\Controller\Component\PgrComponent'
  ),
  'CakePgr' => array(
    'className' => 'CakeDC\Search\Controller\Component\PgrComponent'
  )
);
// use $this->ADPgr and $this->CakePgr

@ADmad
Copy link
Copy Markdown

ADmad commented May 23, 2012

Silly me, totally forget we already have classname aliasing.

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