Skip to content

Commit

Permalink
chore(views): refactor views service to store absolute paths
Browse files Browse the repository at this point in the history
Refs #6844
  • Loading branch information
ewinslow committed Sep 8, 2014
1 parent cd87181 commit 6f1eaa7
Show file tree
Hide file tree
Showing 18 changed files with 1,125 additions and 805 deletions.
58 changes: 29 additions & 29 deletions engine/classes/Elgg/Di/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
* @property-read \Elgg\Http\Request $request
* @property-read \Elgg\Router $router
* @property-read \ElggSession $session
* @property-read \Elgg\ViewsService $views
* @property-read \Elgg\Views\Registry $views
* @property-read \Elgg\WidgetsService $widgets
*
* @package Elgg.Core
* @access private
*/
class ServiceProvider extends \Elgg\Di\DiContainer {
class ServiceProvider extends DiContainer {

/**
* Constructor
Expand Down Expand Up @@ -63,41 +63,41 @@ public function __construct(\Elgg\AutoloadManager $autoload_manager) {
/**
* Database factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\Database
*/
protected function getDatabase(\Elgg\Di\ServiceProvider $c) {
protected function getDatabase(ServiceProvider $c) {
global $CONFIG;
return new \Elgg\Database(new \Elgg\Database\Config($CONFIG), $c->logger);
}

/**
* Events service factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\EventsService
*/
protected function getEvents(\Elgg\Di\ServiceProvider $c) {
protected function getEvents(ServiceProvider $c) {
return $this->resolveLoggerDependencies('events');
}

/**
* Logger factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\Logger
*/
protected function getLogger(\Elgg\Di\ServiceProvider $c) {
protected function getLogger(ServiceProvider $c) {
return $this->resolveLoggerDependencies('logger');
}

/**
* Plugin hooks service factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\PluginHooksService
*/
protected function getHooks(\Elgg\Di\ServiceProvider $c) {
protected function getHooks(ServiceProvider $c) {
return $this->resolveLoggerDependencies('hooks');
}

Expand All @@ -124,20 +124,21 @@ protected function resolveLoggerDependencies($service_needed) {
/**
* Views service factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @return \Elgg\ViewsService
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\Views\Registry
*/
protected function getViews(\Elgg\Di\ServiceProvider $c) {
return new \Elgg\ViewsService($c->hooks, $c->logger);
protected function getViews(ServiceProvider $c) {
global $CONFIG;
return new \Elgg\Views\Registry($c->hooks, $c->events, $c->logger, $c->request, $CONFIG);
}

/**
* AMD Config factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\Amd\Config
*/
protected function getAmdConfig(\Elgg\Di\ServiceProvider $c) {
protected function getAmdConfig(ServiceProvider $c) {
$obj = new \Elgg\Amd\Config();
$obj->setBaseUrl(_elgg_get_simplecache_root() . "js/");
return $obj;
Expand All @@ -146,10 +147,10 @@ protected function getAmdConfig(\Elgg\Di\ServiceProvider $c) {
/**
* Session factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \ElggSession
*/
protected function getSession(\Elgg\Di\ServiceProvider $c) {
protected function getSession(ServiceProvider $c) {
global $CONFIG;

// account for difference of session_get_cookie_params() and ini key names
Expand All @@ -171,31 +172,31 @@ protected function getSession(\Elgg\Di\ServiceProvider $c) {
/**
* Request factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\Http\Request
*/
protected function getRequest(\Elgg\Di\ServiceProvider $c) {
protected function getRequest(ServiceProvider $c) {
return \Elgg\Http\Request::createFromGlobals();
}

/**
* Router factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\Router
*/
protected function getRouter(\Elgg\Di\ServiceProvider $c) {
protected function getRouter(ServiceProvider $c) {
// TODO(evan): Init routes from plugins or cache
return new \Elgg\Router($c->hooks);
}

/**
* Notification service factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\Notifications\NotificationsService
*/
protected function getNotifications(\Elgg\Di\ServiceProvider $c) {
protected function getNotifications(ServiceProvider $c) {
// @todo move queue in service provider
$queue = new \Elgg\Queue\DatabaseQueue(\Elgg\Notifications\NotificationsService::QUEUE_NAME, $c->db);
$sub = new \Elgg\Notifications\SubscriptionsService($c->db);
Expand All @@ -206,10 +207,10 @@ protected function getNotifications(\Elgg\Di\ServiceProvider $c) {
/**
* Persistent login service factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\PersistentLoginService
*/
protected function getPersistentLogin(\Elgg\Di\ServiceProvider $c) {
protected function getPersistentLogin(ServiceProvider $c) {
$cookies_config = elgg_get_config('cookies');
$remember_me_cookies_config = $cookies_config['remember_me'];
$cookie_name = $remember_me_cookies_config['name'];
Expand All @@ -220,11 +221,10 @@ protected function getPersistentLogin(\Elgg\Di\ServiceProvider $c) {
/**
* Query counter factory
*
* @param \Elgg\Di\ServiceProvider $c Dependency injection container
* @param ServiceProvider $c Dependency injection container
* @return \Elgg\Database\QueryCounter
*/
protected function getQueryCounter(\Elgg\Di\ServiceProvider $c) {
protected function getQueryCounter(ServiceProvider $c) {
return new \Elgg\Database\QueryCounter($c->db);
}
}

53 changes: 44 additions & 9 deletions engine/classes/Elgg/Filesystem/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,17 @@ public function getExtension() {
}

/**
* @return string The file's basename.
* @return string The part of the file after the directory, except the suffix.
*/
public function getBasename() {
return pathinfo($this->path, PATHINFO_BASENAME);
public function getBasename($suffix = '') {
return basename($this->path, $suffix);
}

/**
* @return string The directory path without the final file name.
*/
public function getDirname() {
return pathinfo($this->path, PATHINFO_DIRNAME);
}

/**
Expand All @@ -60,18 +67,46 @@ public function getContents() {
}

/**
* Do a PHP include of the file and return the result.
* Set the content of this file, overwriting old content if necessary.
*
* TODO: This may only work for local filesystem?
* @return File
*/
public function putContents($content) {
$this->filesystem->put($this->path, $content);

return $this;
}

/**
* True if the file begins with a dot or is in a directory that begins with a dot.
*
* @return mixed
* @return bool
*/
public function includeFile() {
return $this->filesystem->includeFile($this->path);
public function isPrivate() {
return strpos($this->getPath(), "/.") !== false ||
strpos($this->getPath(), ".") === 0;
}

/**
* Get the path relative to the current filesystem.
*
* @return string
*/
public function getPath() {
return $this->path;
}

/**
* Get the entire path including that of the containing filesystem.
*
* @return string
*/
public function getFullPath() {
return $this->filesystem->getFullPath($this->path);
}

/** @inheritDoc */
public function __toString() {
return $this->path;
return $this->getFullPath();
}
}
28 changes: 10 additions & 18 deletions engine/classes/Elgg/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ public function getFileContents($path) {
*/
public function getFiles($path = '') {
$keys = $this->gaufrette->listKeys($this->getGaufrettePath($path));
$chroot = $this->chroot;

$filesystem = $this;
return array_map(function($path) use ($filesystem) {
return new File($filesystem, $path);
$paths = array_map(function($file_path) use ($chroot) {
return substr($file_path, strlen($chroot));
}, $keys['keys']);

return array_map(array($this, 'getFile'), $paths);
}

/**
Expand All @@ -122,7 +124,10 @@ public function getFiles($path = '') {
* @return string
*/
private function getGaufrettePath($path) {
return $this->normalize("$this->chroot/$path");
$chroot = $this->normalize($this->chroot);
$path = $this->normalize($path);

return $this->normalize("$chroot/$path");
}

/**
Expand All @@ -132,24 +137,11 @@ private function getGaufrettePath($path) {
*
* @return string
*/
private function getFullPath($path = '') {
public function getFullPath($path = '') {
$gaufrettePath = $this->normalize($this->getGaufrettePath($path));
return "$this->localPath/$gaufrettePath";
}


/**
* Do a PHP include of the file and return the result.
* This only really works with local filesystems amirite?
*
* @param string $path Filesystem-relative path for the file to include.
*
* @return mixed
*/
public function includeFile($path) {
return include $this->getFullPath($path);
}

/**
* Whether there is a file or directory at the given path.
*
Expand Down
Loading

0 comments on commit 6f1eaa7

Please sign in to comment.