Proposal: Move classes/w2p to PSR-4 namespaces #470
caseysoftware
started this conversation in
Ideas / Feature Requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Status: Draft v1 (proposal) · Milestone: web2project v6.0 · Baseline:
dev-5.0.0@179b4eafRelated issues: #467, #468 and #469 are small fixes to make first. #468 goes away entirely once step 4 lands.
Related proposals: Pluggable File Backend (#404) and Identity Layer (#435). Both name their new classes in the
w2p_*style. If this proposal is accepted, they should use the namespaced names instead.Summary
Move the core classes in
classes/w2p/from web2project's own underscore-style autoloading to Composer PSR-4. Every class gets a real namespace:w2p_Core_CAppUIbecomesWeb2project\Core\CAppUI. A compatibility autoloader keeps every oldw2p_*name working, so modules, themes and third-party add-ons don't break.Where things are today
The classes aren't PSR-0 either.
composer.jsonhas apsr-0entry forWeb2project→src/, butsrc/doesn't exist. Nothing inclasses/goes through Composer.The classes load through
web2project_autoload()inclasses/w2p/web2project.php. It stripsw2p_, turns_into/, and includes<path>.class.php, sow2p_Core_CAppUIloadsclasses/w2p/Core/CAppUI.class.php. Neither PSR standard allows the.class.phpsuffix.PSR-4 has no rule for turning underscores into folders. Moving to it therefore means adding namespaces: a straight Composer swap isn't possible.
Size
classes/w2pw2p_*class names outsidelib/andvendor/modules231,style46,unit_tests26,includesand root scripts about 15w2p_Utilities_Date558,w2p_Core_CAppUI338,w2p_Database_Query199extends w2p_Core_BaseObject21,w2p_System_Setup8,w2p_Theme_Baseandw2p_Theme_TabBox4Proposed changes
1. One namespace root. New 6.0 code (services, API, auth) is already expected to live under
Web2project\insrc/. Every class file gets renamed anyway, so move the existing classes there too and keep a single root:The alternative is to leave the files where they are and map
"Web2project\\": "classes/w2p/". Either works. The point is to avoid two roots.2. Rename the files and drop
.class.php, for examplegit mv classes/w2p/Core/CAppUI.class.php src/Core/CAppUI.php.3. Add a namespace to each class, and qualify global names. Inside a namespaced file, a bare global name stops resolving. Each of these needs a leading
\or ausestatement:\Exception, used by both exception classes.\gacl_apiand\xajaxResponse(6.0 has already replaced most of these).\CTask(w2p_Actions_BulkTasks extends CTask), plusCProject,CUserand so on inside core methods.newcalls, such asw2p_System_EventinsideBaseObject.Keep the short class names as they are:
CAppUIstaysCAppUI. Renames likeCAppUI→System\AppUIcan come later as separate, reviewable changes.Naming:
Base(Authenticators\Base,Controllers\Base,Theme\Base,Output\HTML\Base). Callers that import more than one needuse ... as ....w2p_Exception_FileSystemlives inExceptions/, so it becomesWeb2project\Exceptions\FileSystem(w2p_Exception_FileSystem cannot be autoloaded (class name says Exception, folder says Exceptions) #467).4. Remove the custom autoloader. Delete
classes/w2p/web2project.php, remove itsrequire_onceandspl_autoload_register('web2project_autoload')fromincludes/main_functions.php, and runcomposer dump-autoload -o. Keepw2p_old_autoload(), because module classes (CProject,CTask, ...) stay global.5. Add a compatibility autoloader for the old names. This has to ship. Modules, themes and third-party add-on modules all use
w2p_*names, and so do callback names already stored in the database and theCAppUIobject stored in active sessions. The loader aliases each class the first time its old name is used, rather than callingclass_alias70+ times on every request:Aliases are the same class, so
instanceof, type hints andextends w2p_Core_BaseObjectkeep working.6. Update callers inside the repo. This is a mechanical rewrite of about 1,750 references in 390 files. Rector's
RenameClassRectorwith an explicit old → new map handles it, including@var w2p_Core_CAppUIdocblocks instyle/and@returntags. ItsPseudoNamespaceToNamespaceRectoris an option too, but the explicit map is easier to review.7. Behaviour that changes:
get_class()and::classnow returnWeb2project\....w2p_System_EventQueue::add()storesget_class()inqueue_callback. New rows will holdWeb2project\Utilities\Mail::SendQueuedMail, which still fitsvarchar(127), andnew $classstill works. Rows already queued under the old names are covered by step 5. Without step 5, add an upgradeUPDATE.CAppUIis serialized into the session. With step 5, restoring it after deploy still works. Without step 5, every user is logged out once.w2p_System_Dispatcherkeys subscriptions onget_class($this), but subscribers are the global module classes, so nothing changes there.get_class($this)change text. This only matters to tests that assert on the message.8. Tests and tooling.
unit_tests/classes.assertInstanceOf('w2p_...')calls to::classconstants.unit_tests/src/Web2project/(Move the stray unit_tests/src/Web2project tests under unit_tests/classes/w2p #469).unit_tests/build.xml.Sequencing
E_USER_DEPRECATEDwith the new name, then remove it.Rough size: commit A is half a day. Commit B is about a day, plus a few days of testing across modules and themes.
Open questions
src/, or map PSR-4 ontoclasses/w2p/where they are now?w2p_*compatibility loader stay: all of 6.x, or until 7.0?CAppUI,iCalendar,HTMLHelper) while every caller is being touched anyway, or leave that for later?All reactions