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 Elgg#6844

Conflicts:
	engine/classes/Elgg/ViewsService.php
	engine/tests/phpunit/Elgg/Di/ServiceProviderTest.php
	engine/tests/phpunit/bootstrap.php
  • Loading branch information
ewinslow committed May 29, 2015
1 parent 6245f36 commit e86ea42
Show file tree
Hide file tree
Showing 23 changed files with 1,358 additions and 835 deletions.
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -28,6 +28,9 @@
"composer validate"
]
},
"scripts": {
"test": "vendor/bin/phpunit"
},
"suggest": {
"ext-mbstring": "*"
},
Expand Down
7 changes: 4 additions & 3 deletions engine/classes/Elgg/Di/ServiceProvider.php
Expand Up @@ -56,13 +56,13 @@
* @property-read \Elgg\SystemMessagesService $systemMessages
* @property-read \Elgg\I18n\Translator $translator
* @property-read \Elgg\Database\UsersTable $usersTable
* @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 @@ -258,7 +258,8 @@ public function __construct(\Elgg\Config $config) {
$this->setClassName('usersTable', \Elgg\Database\UsersTable::class);

$this->setFactory('views', function(ServiceProvider $c) {
return new \Elgg\ViewsService($c->hooks, $c->logger);
global $CONFIG;
return new \Elgg\Views\Registry($CONFIG, $c->events, $c->hooks, $c->input, $c->logger);
});

$this->setClassName('widgets', \Elgg\WidgetsService::class);
Expand Down
20 changes: 20 additions & 0 deletions engine/classes/Elgg/Filesystem/Directory.php
@@ -1,6 +1,8 @@
<?php
namespace Elgg\Filesystem;

use Elgg\Structs\Collection;

/**
* A simple directory abstraction.
*
Expand Down Expand Up @@ -48,6 +50,15 @@ public function getFile($path);
*/
public function getFiles($path = '');

/**
* Get the absolute path to the given directory-relative path.
*
* @param string $path A file/directory path within this directory.
*
* @return string
*/
public function getFullPath($path = '');

/**
* Do a PHP include of the file and return the result.
*
Expand All @@ -59,6 +70,15 @@ public function getFiles($path = '');
*/
public function includeFile($path);

/**
* Whether this directory has an existing subdirectory at the given path.
*
* @param string $path The relative path within this directory
*
* @return boolean
*/
public function isDirectory($path);

/**
* Whether this directory has an existing file at the given location.
*
Expand Down
64 changes: 58 additions & 6 deletions engine/classes/Elgg/Filesystem/File.php
Expand Up @@ -28,17 +28,21 @@ public function __construct(Directory $directory, $path) {
}

/**
* @return boolean Whether this file exists.
* Whether this file exists.
*
* @return boolean
*/
public function exists() {
return $this->directory->isFile($this->path);
}

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

/**
Expand All @@ -51,12 +55,41 @@ public function getContents() {
}

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

/**
* Get the file's extension.
*
* @return string
*/
public function getExtension() {
return pathinfo($this->path, PATHINFO_EXTENSION);
}

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

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

/**
* Do a PHP include of the file and return the result.
*
Expand All @@ -68,8 +101,27 @@ public function includeFile() {
return $this->directory->includeFile($this->path);
}

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

/**
* Set the content of this file, overwriting old content if necessary.
*
* @return void
*/
public function putContents($content) {
$this->directory->putContents($this->path, $content);
}

/** @inheritDoc */
public function __toString() {
return $this->path;
return $this->getFullPath();
}
}
36 changes: 16 additions & 20 deletions engine/classes/Elgg/Filesystem/GaufretteDirectory.php
Expand Up @@ -42,14 +42,8 @@ public function chroot($path) {
return new self($this->gaufrette, $this->localPath, $this->getGaufrettePath($path));
}

/**
* Whether this filesystem has an existing directory at the given path.
*
* @param string $path The path to the directory, relative to this filesystem.
*
* @return boolean
*/
private function isDirectory($path) {
/** @inheritDoc */
public function isDirectory($path) {
$adapter = $this->gaufrette->getAdapter();
return $adapter->isDirectory($this->getGaufrettePath($path));
}
Expand Down Expand Up @@ -81,35 +75,33 @@ public function getFile($path) {
/** @inheritDoc */
public function getFiles($path = '') {
$keys = $this->gaufrette->listKeys($this->getGaufrettePath($path));
$chrootLength = strlen($this->chroot);

$files = new ArrayCollection($keys['keys']);

return $files->map(function($path) {
return new File($this, $path);
return $files->map(function($path) use ($chrootLength) {
return $this->getFile(substr($path, $chrootLength));
});
}

/**
* Get the absolute path to the given directory-relative path.
*
* @param string $path A file/directory path within this directory.
*
* @return string
*/
private function getFullPath($path = '') {
/** @inheritDoc */
public function getFullPath($path = '') {
$gaufrettePath = $this->normalize($this->getGaufrettePath($path));
return "$this->localPath/$gaufrettePath";
}

/**
* Get a path suitable for passing to the underlying gaufrette filesystem.
*
* @param string $path The path relative to this directory.
* @param string $path A file/directory path within this directory.
*
* @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");
}

/** @inheritDoc */
Expand All @@ -133,6 +125,10 @@ public function putContents($path, $content) {
$this->gaufrette->write($this->getGaufrettePath($path), $content, true);
}

public function __toString() {
return $this->getFullPath();
}

/**
* Shorthand for generating a new local filesystem.
*
Expand Down
13 changes: 13 additions & 0 deletions engine/classes/Elgg/Views/Exception/UnreadableDirectory.php
@@ -0,0 +1,13 @@
<?php
namespace Elgg\Views\Exception;

class UnreadableDirectory extends \Exception {
public $path;

/**
* @param string $path
*/
public function __construct($path) {
$this->path = $path;
}
}

0 comments on commit e86ea42

Please sign in to comment.