Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,7 @@
'OC\\AppFramework\\Services\\InitialState' => $baseDir . '/lib/private/AppFramework/Services/InitialState.php',
'OC\\AppFramework\\Utility\\ControllerMethodReflector' => $baseDir . '/lib/private/AppFramework/Utility/ControllerMethodReflector.php',
'OC\\AppFramework\\Utility\\QueryNotFoundException' => $baseDir . '/lib/private/AppFramework/Utility/QueryNotFoundException.php',
'OC\\AppFramework\\Utility\\ServiceFactory' => $baseDir . '/lib/private/AppFramework/Utility/ServiceFactory.php',
'OC\\AppFramework\\Utility\\SimpleContainer' => $baseDir . '/lib/private/AppFramework/Utility/SimpleContainer.php',
'OC\\AppFramework\\Utility\\TimeFactory' => $baseDir . '/lib/private/AppFramework/Utility/TimeFactory.php',
'OC\\AppScriptDependency' => $baseDir . '/lib/private/AppScriptDependency.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\AppFramework\\Services\\InitialState' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Services/InitialState.php',
'OC\\AppFramework\\Utility\\ControllerMethodReflector' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Utility/ControllerMethodReflector.php',
'OC\\AppFramework\\Utility\\QueryNotFoundException' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Utility/QueryNotFoundException.php',
'OC\\AppFramework\\Utility\\ServiceFactory' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Utility/ServiceFactory.php',
'OC\\AppFramework\\Utility\\SimpleContainer' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Utility/SimpleContainer.php',
'OC\\AppFramework\\Utility\\TimeFactory' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Utility/TimeFactory.php',
'OC\\AppScriptDependency' => __DIR__ . '/../../..' . '/lib/private/AppScriptDependency.php',
Expand Down
4 changes: 2 additions & 2 deletions lib/private/AppFramework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function getAppIdForClass(string $className, string $topNamespace
* @param string $controllerName the name of the controller under which it is
* stored in the DI container
* @param string $methodName the method that you want to call
* @param DIContainer $container an instance of a pimple container.
* @param DIContainer $container an instance of a container.
* @param array $urlParams list of URL parameters (optional)
* @throws HintException
*/
Expand Down Expand Up @@ -231,7 +231,7 @@ public static function main(string $controllerName, string $methodName, DIContai
* stored in the DI container
* @param string $methodName the method that you want to call
* @param array $urlParams an array with variables extracted from the routes
* @param DIContainer $container an instance of a pimple container.
* @param DIContainer $container an instance of container.
*/
public static function part(string $controllerName, string $methodName, array $urlParams,
DIContainer $container) {
Expand Down
27 changes: 20 additions & 7 deletions lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace OC\AppFramework\DependencyInjection;

use OC;
use OC\AppFramework\App;
use OC\AppFramework\Http;
use OC\AppFramework\Http\Dispatcher;
use OC\AppFramework\Http\Output;
Expand Down Expand Up @@ -57,6 +58,7 @@
*/
class DIContainer extends SimpleContainer implements IAppContainer {
private string $appName;
private string $nameSpace;

/**
* @var array
Expand All @@ -73,8 +75,8 @@ class DIContainer extends SimpleContainer implements IAppContainer {
* @param ServerContainer|null $server
*/
public function __construct(string $appName, array $urlParams = [], ?ServerContainer $server = null) {
parent::__construct();
$this->appName = $appName;
$this->nameSpace = App::buildAppNamespace($this->appName);
$this['appName'] = $appName;
$this['urlParams'] = $urlParams;

Expand Down Expand Up @@ -398,21 +400,30 @@ public function has($id): bool {
return false;
}

public function offsetSet($offset, $value): void {
if ($offset === 'AppName' || $offset === 'appName') {
$this->appName = $value;
}
$this->items[$offset] = $value;
}

public function query(string $name, bool $autoload = true) {
if ($name === 'AppName' || $name === 'appName') {
return $this->appName;
}
$name = $this->sanitizeName($name);
$name = $this->resolveAlias($name);

$isServerClass = str_starts_with($name, 'OCP\\') || str_starts_with($name, 'OC\\');
if ($isServerClass && !$this->has($name)) {
return $this->getServer()->query($name, $autoload);
return $this->server->queryNoApps($name, $autoload);
}

try {
return $this->queryNoFallback($name);
} catch (QueryException $firstException) {
try {
return $this->getServer()->query($name, $autoload);
return $this->server->query($name, $autoload);
} catch (QueryException $secondException) {
if ($firstException->getCode() === 1) {
throw $secondException;
Expand All @@ -427,20 +438,22 @@ public function query(string $name, bool $autoload = true) {
* @return mixed
* @throws QueryException if the query could not be resolved
*/
public function queryNoFallback($name) {
$name = $this->sanitizeName($name);

public function queryNoFallback(string $name) {
if ($this->offsetExists($name)) {
return parent::query($name);
} elseif ($this->appName === 'settings' && str_starts_with($name, 'OC\\Settings\\')) {
return parent::query($name);
} elseif ($this->appName === 'core' && str_starts_with($name, 'OC\\Core\\')) {
return parent::query($name);
} elseif (str_starts_with($name, \OC\AppFramework\App::buildAppNamespace($this->appName) . '\\')) {
} elseif (str_starts_with($name, $this->nameSpace)) {
return parent::query($name);
}

throw new QueryException('Could not resolve ' . $name . '!' .
' Class can not be instantiated', 1);
}

protected function resolveAlias(string $name): string {
return parent::resolveAlias($this->server->resolveAlias($name));
}
}
19 changes: 19 additions & 0 deletions lib/private/AppFramework/Utility/ServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace OC\AppFramework\Utility;

use Psr\Container\ContainerInterface;

class ServiceFactory {
private $factory;
private ContainerInterface $container;

public function __construct(ContainerInterface $container, callable $factory) {
$this->container = $container;
$this->factory = $factory;
}

public function get() {
return ($this->factory)($this->container);
}
}
100 changes: 61 additions & 39 deletions lib/private/AppFramework/Utility/SimpleContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Closure;
use OCP\AppFramework\QueryException;
use OCP\IContainer;
use Pimple\Container;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use ReflectionClass;
Expand All @@ -21,16 +20,12 @@
use function class_exists;

/**
* SimpleContainer is a simple implementation of a container on basis of Pimple
* SimpleContainer is a simple implementation of a container
*/
class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer {
public static bool $useLazyObjects = false;

private Container $container;

public function __construct() {
$this->container = new Container();
}
protected array $items = [];
protected array $aliases = [];

/**
* @template T
Expand All @@ -46,12 +41,13 @@

public function has(string $id): bool {
// If a service is no registered but is an existing class, we can probably load it
return isset($this->container[$id]) || class_exists($id);
return array_key_exists($id, $this->items) || array_key_exists($id, $this->aliases) || class_exists($id);
}

/**
* @param ReflectionClass $class the class to instantiate
* @return object the created class
* @template T of object
* @param ReflectionClass<T> $class the class to instantiate
* @return T the created class
* @suppress PhanUndeclaredClassInstanceof
*/
private function buildClass(ReflectionClass $class): object {
Expand Down Expand Up @@ -79,7 +75,7 @@
$resolveName = $parameter->getName();

// try to find out if it is a class or a simple parameter
if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) {
if (($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) {
$resolveName = $parameterType->getName();
}

Expand All @@ -93,7 +89,7 @@
return $parameter->getDefaultValue();
}

if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) {
if (($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) {
$resolveName = $parameter->getName();
try {
return $this->query($resolveName);
Expand Down Expand Up @@ -131,27 +127,34 @@

public function query(string $name, bool $autoload = true) {
$name = $this->sanitizeName($name);
if (isset($this->container[$name])) {
return $this->container[$name];
$name = $this->resolveAlias($name);

if (array_key_exists($name, $this->items)) {
$item = $this->items[$name];
if ($item instanceof ServiceFactory) {
return $item->get();
} elseif (is_callable($item)) {
$this->items[$name] = $item($this);

Check failure on line 137 in lib/private/AppFramework/Utility/SimpleContainer.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedCallable

lib/private/AppFramework/Utility/SimpleContainer.php:137:27: TaintedCallable: Detected tainted text (see https://psalm.dev/243)

Check failure on line 137 in lib/private/AppFramework/Utility/SimpleContainer.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedCallable

lib/private/AppFramework/Utility/SimpleContainer.php:137:27: TaintedCallable: Detected tainted text (see https://psalm.dev/243)

Check failure

Code scanning / Psalm

TaintedCallable Error

Detected tainted text

Check failure

Code scanning / Psalm

TaintedCallable Error

Detected tainted text
}
return $this->items[$name];
}

if ($autoload) {
$object = $this->resolve($name);
$this->registerService($name, function () use ($object) {
return $object;
});
$this->items[$name] = $object;
return $object;
}

throw new QueryNotFoundException('Could not resolve ' . $name . '!');
throw new QueryNotFoundException('Could not resolve ' . $name . '!' . get_class($this));
}

/**
* @param string $name
* @param mixed $value
*/
public function registerParameter($name, $value) {
$this[$name] = $value;
$this->items[$name] = $value;
unset($this->aliases[$name]);
}

/**
Expand All @@ -164,17 +167,12 @@
* @param bool $shared
*/
public function registerService($name, Closure $closure, $shared = true) {
$wrapped = function () use ($closure) {
return $closure($this);
};
$name = $this->sanitizeName($name);
if (isset($this->container[$name])) {
unset($this->container[$name]);
}
unset($this->aliases[$name]);
if ($shared) {
$this->container[$name] = $wrapped;
$this->items[$name] = $closure;
} else {
$this->container[$name] = $this->container->factory($wrapped);
$this->items[$name] = new ServiceFactory($this, $closure);
}
}

Expand All @@ -186,27 +184,28 @@
* @param string $target the target that should be resolved instead
*/
public function registerAlias($alias, $target) {
$this->registerService($alias, function (ContainerInterface $container) use ($target) {
return $container->get($target);
}, false);
$alias = $this->sanitizeName($alias);
$target = $this->sanitizeName($target);
if ($alias === $target) {
throw new QueryNotFoundException('Can\'t alias to self');
}
unset($this->items[$alias]);
$this->aliases[$alias] = $target;
}

/**
* @param string $name
* @return string
*/
protected function sanitizeName($name) {
if (isset($name[0]) && $name[0] === '\\') {
return ltrim($name, '\\');
}
return $name;
return ltrim($name, '\\');
}

/**
* @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has
*/
public function offsetExists($id): bool {
return $this->container->offsetExists($id);
return array_key_exists($id, $this->items) || array_key_exists($id, $this->aliases);
}

/**
Expand All @@ -215,20 +214,43 @@
*/
#[\ReturnTypeWillChange]
public function offsetGet($id) {
return $this->container->offsetGet($id);
return $this->query($id);
}

/**
* @deprecated 20.0.0 use \OCP\IContainer::registerService
*/
public function offsetSet($offset, $value): void {
$this->container->offsetSet($offset, $value);
$this->items[$offset] = $value;
}

/**
* @deprecated 20.0.0
*/
public function offsetUnset($offset): void {
$this->container->offsetUnset($offset);
unset($this->items[$offset]);
unset($this->aliases[$offset]);
}

/**
* Check if we already have a resolved instance of $name
*/
public function isResolved($name): bool {
if (!array_key_exists($name, $this->items)) {
return false;
}
$item = $this->items[$name];
if ($item instanceof ServiceFactory) {
return false;
} else {
return !is_callable($item);
}
}

protected function resolveAlias(string $name): string {
while (array_key_exists($name, $this->aliases)) {
$name = $this->aliases[$name];
}
return $name;
}
}
Loading
Loading