global $user is an abomination. Let us put it out of our misery.

Replace all references to global $user with $context['user']. Note that in most cases we'll need to call drupal_get_context() to get the object, and then access the user out of there. We also need a handler for the user object that wraps what the user init process does. Probably as a first cut it can just wrap global $user, but eventually the context handler should be what does the actual user initialization.

$grep -l -r "global.*user;" .

./lib/Drupal/Core/Cache/DatabaseBackend.php
./update.php
./includes/gettext.inc
./includes/theme.inc
./includes/form.inc
./includes/bootstrap.inc
./includes/file.inc
./includes/common.inc
./includes/install.core.inc
./includes/language.inc
./modules/toolbar/toolbar.module
./modules/system/system.api.php
./modules/system/system.module
./modules/system/tests/common.test
./modules/system/tests/modules/form_test/form_test.module
./modules/system/tests/file.test
./modules/php/php.module
./modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
./modules/field/field.module
./modules/node/node.pages.inc
./modules/node/node.module
./modules/poll/poll.module
./modules/filter/filter.module
./modules/overlay/overlay.module
./modules/comment/lib/Drupal/comment/CommentStorageController.php
./modules/comment/comment.module
./modules/forum/forum.module
./modules/statistics/statistics.module
./modules/shortcut/shortcut.admin.inc
./modules/shortcut/shortcut.module
./modules/user/user.pages.inc
./modules/user/user.module
./modules/language/language.negotiation.inc
./modules/contact/contact.module
./modules/contact/contact.pages.inc
./modules/file/file.module

$grep -r "global.*user;" . | wc -l
97

CommentFileSizeAuthor
#4 wscci-globaluser-0.patch1.35 KBtamasd
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Crell’s picture

Note that users as stdClass objects technically are not supported by context, but since we don't have the conversion of users to real classes yet we'll live with it for now so that we can make some progress.

tamasd’s picture

I am working on this issue, and we'll see how far I will get.

tamasd’s picture

FileSize
1.35 KB

First patch (far from being ready): adds ContextHandlerUser class and user_context_init().

tamasd’s picture

Assigned: Unassigned » tamasd
catch’s picture

Subscribing.

mr.moses’s picture

.

stevector’s picture

Yoririou, any progress? In today's WSCCI meeting I said I'd work on this issue.

Pounard mentioned that this issue should work in tandem with #335411: Switch to Symfony2-based session handling

sdboyer volunteered to work refactoring the session handling.

pounard’s picture

A quick and dirty design can be:

We inject a specific handler into root context (at bootstrap) for handling session.
This handler is lazy loaded when accessing the "user" context key.
It is able to detect if there's a session or not.
Then if context key is something like "user:authenticated" we only check session existence.
If context key is "user" we ask the session the user.
The session uses its backend (injected at bootstrap too) which loads the user and returns it.

Context -> UserContextHandler -> Session -> SessionBackendInterface

tamasd’s picture

I have a branch for this. The code mostly work, I was clicking around and I haven't found any bugs, but I haven't tested it properly with the test suite. If all tests pass then the code is ready to merge.

Crell’s picture

I suspect the branch is stale now that the context system is using PSR-0 for autoloading, and we did some other refactoring. Steve, Sam, I'd suggest branching off of the existing branch and working from there.

Crell’s picture

stevector, sdboyer, and progress here?

stevector’s picture

I'm getting errors when merging wscci into the 1260864 branch stemming from drupal_get_context().

Even in just the wscci branch I'm getting

Fatal error: Class 'Drupal\Context' not found in /Applications/MAMP/htdocs/stevector/wscci/includes/bootstrap.inc on line 2422

when calling drupal_get_context().

pounard’s picture

The real class name is Drupal\Context\Context you should either:

use Drupal\Context\Context;

$context = new Context;

Or

$context = new Drupal\Context\Context;

I personally prefer the first since the fully qualified class name will be resolved at compile time and not a runtime, so is more performant by definition.

Crell’s picture

Assigned: tamasd » Unassigned

I've pushed a fix to change it to return \Drupal\Context\Context::getActiveContext(); Since it's a fully qualified name from the get-go it should not need to be re-resolved. That may change later if/when we add more namespace using to bootstrap.inc, but we still need to do a lot to rip that file apart before we can get there. I expect we will convert to the "use" version eventually.

Steve, back to you!

stevector’s picture

I have merged WSCCI into the branch that Yorirou started. So far so good. I don't think this should be merged back to the main wscci branch yet. Not all of the global $user calls are out yet and #335411: Switch to Symfony2-based session handling still needs move forward.

cweagans’s picture

sub.

webchick’s picture

Project: WSCCI » Drupal core
Version: » 8.x-dev
Component: Code » wscci

Per catch, and blessed by Larry, moving this and all other WSCCI issues to the Drupal core queue.

Niklas Fiekas’s picture

Note from todays Windsprint: With one example conversion this would make a good novice task.

Crell’s picture

Status: Active » Closed (won't fix)

Actually this issue is dead, because this was for the stillborn "context API".

Crell’s picture

Issue summary: View changes

Add a list of files.