-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW Add SubsiteState and initialisation middleware, replace Subsite::…
…currentSubsiteID use
- Loading branch information
1 parent
ce360aa
commit e129caf
Showing
16 changed files
with
232 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
<?php | ||
|
||
use SilverStripe\Dev\Deprecation; | ||
|
||
Deprecation::notification_version('2.0', 'subsites'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
Name: subsitesmiddleware | ||
After: | ||
- requestprocessors | ||
--- | ||
SilverStripe\Core\Injector\Injector: | ||
SilverStripe\Control\Director: | ||
properties: | ||
Middlewares: | ||
SubsitesStateMiddleware: %$SilverStripe\Subsites\Middleware\InitStateMiddleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Subsites\Middleware; | ||
|
||
use SilverStripe\Control\HTTPRequest; | ||
use SilverStripe\Control\Middleware\HTTPMiddleware; | ||
use SilverStripe\Core\Injector\Injector; | ||
use SilverStripe\Subsites\Model\Subsite; | ||
use SilverStripe\Subsites\State\SubsiteState; | ||
|
||
class InitStateMiddleware implements HTTPMiddleware | ||
{ | ||
public function process(HTTPRequest $request, callable $delegate) | ||
{ | ||
$state = SubsiteState::create(); | ||
Injector::inst()->registerService($state); | ||
|
||
$state->setSubsiteId($this->detectSubsiteId($request)); | ||
|
||
return $delegate($request); | ||
} | ||
|
||
/** | ||
* Use the given request to detect the current subsite ID | ||
* | ||
* @param HTTPRequest $request | ||
* @return int | ||
*/ | ||
protected function detectSubsiteId(HTTPRequest $request) | ||
{ | ||
$id = null; | ||
|
||
if ($request->getVar('SubsiteID')) { | ||
$id = (int) $request->getVar('SubsiteID'); | ||
} | ||
|
||
if (Subsite::$use_session_subsiteid) { | ||
$id = $request->getSession()->get('SubsiteID'); | ||
} | ||
|
||
if ($id === null) { | ||
$id = Subsite::getSubsiteIDForDomain(); | ||
} | ||
|
||
return (int) $id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Subsites\State; | ||
|
||
use SilverStripe\Core\Injector\Injectable; | ||
use SilverStripe\Core\Injector\Injector; | ||
|
||
/** | ||
* SubsiteState provides static access to the current state for subsite related data during a request | ||
*/ | ||
class SubsiteState | ||
{ | ||
use Injectable; | ||
|
||
/** | ||
* @var int|null | ||
*/ | ||
protected $subsiteId; | ||
|
||
/** | ||
* Get the current subsite ID | ||
* | ||
* @return int|null | ||
*/ | ||
public function getSubsiteId() | ||
{ | ||
return $this->subsiteId; | ||
} | ||
|
||
/** | ||
* Set the current subsite ID | ||
* | ||
* @param int $id | ||
* @return $this | ||
*/ | ||
public function setSubsiteId($id) | ||
{ | ||
$this->subsiteId = (int) $id; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Perform a given action within the context of a new, isolated state. Modifications are temporary | ||
* and the existing state will be restored afterwards. | ||
* | ||
* @param callable $callback Callback to run. Will be passed the nested state as a parameter | ||
* @return mixed Result of callback | ||
*/ | ||
public function withState(callable $callback) | ||
{ | ||
$newState = clone $this; | ||
try { | ||
Injector::inst()->registerService($newState); | ||
return $callback($newState); | ||
} finally { | ||
Injector::inst()->registerService($this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.