Skip to content

Missing problem file #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 8, 2020
Merged
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
11 changes: 9 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,20 @@ public function run(): int
)
);
return 1;
} catch (RuntimeException $e) {
} catch (\Throwable $e) {
$message = $e->getMessage();
$basePath = canonicalise_path($container->get('basePath'));

if (strpos($message, $basePath) !== null) {
$message = str_replace($basePath, '', $message);
}

$container
->get(OutputInterface::class)
->printError(
sprintf(
'%s',
$e->getMessage()
$message
)
);
return 1;
Expand Down
11 changes: 10 additions & 1 deletion src/Command/PrintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PhpSchool\PhpWorkshop\Command;

use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
use PhpSchool\PhpWorkshop\Exception\ProblemFileDoesNotExistException;
use PhpSchool\PhpWorkshop\ExerciseRepository;
use PhpSchool\PhpWorkshop\MarkdownRenderer;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
Expand Down Expand Up @@ -68,7 +70,14 @@ public function __invoke(): void
$currentExercise = $this->userState->getCurrentExercise();
$exercise = $this->exerciseRepository->findByName($currentExercise);

$markDown = (string) file_get_contents($exercise->getProblem());
$problemFile = $exercise->getProblem();

if (!is_readable($problemFile)) {
throw ProblemFileDoesNotExistException::fromFile($problemFile);
}

$markDown = (string) file_get_contents($problemFile);

$doc = $this->markdownRenderer->render($markDown);
$doc = str_replace('{appname}', $this->appName, $doc);
$this->output->write($doc);
Expand Down
16 changes: 16 additions & 0 deletions src/Exception/ProblemFileDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\Exception;

class ProblemFileDoesNotExistException extends \RuntimeException
{
public static function fromFile(string $file): self
{
return new self(sprintf(
'Exercise problem file: "%s" does not exist or is not readable',
canonicalise_path($file)
));
}
}
10 changes: 10 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\Exception;

class RuntimeException extends \RuntimeException
{

}
8 changes: 7 additions & 1 deletion src/ExerciseRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Colors\Color;
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\PhpWorkshop\Exception\ProblemFileDoesNotExistException;
use PhpSchool\PhpWorkshop\Output\OutputInterface;

/**
Expand Down Expand Up @@ -100,7 +101,12 @@ public function __invoke(CliMenu $menu): void
$output .= $this->color->__invoke(" " . $exercise->getName())->yellow()->bold() . "\n";
$output .= $this->color->__invoke(sprintf(" Exercise %d of %d\n\n", $exerciseIndex, $numExercises))->yellow();

$content = (string) file_get_contents($exercise->getProblem());
$problemFile = $exercise->getProblem();
if (!is_readable($problemFile)) {
throw ProblemFileDoesNotExistException::fromFile($problemFile);
}

$content = (string) file_get_contents($problemFile);
$doc = $this->markdownRenderer->render($content);
$doc = str_replace('{appname}', $this->appName, $doc);
$output .= $doc;
Expand Down
33 changes: 33 additions & 0 deletions src/Utils/StringUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\Utils;

use PhpSchool\PhpWorkshop\Exception\RuntimeException;

class StringUtils
{
public static function canonicalisePath(string $filename): string
{
$path = [];
foreach (explode('/', $filename) as $part) {
// ignore parts that have no value
if (strlen($part) === 0 || $part === '.') {
continue;
}

if ($part !== '..') {
$path[] = $part;
} elseif (count($path) > 0) {
array_pop($path);
} else {
throw new RuntimeException('Climbing above the root is not permitted.');
}
}

return $filename[0] === '/'
? '/' . implode('/', $path)
: implode('/', $path);
}
}
14 changes: 14 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use PhpSchool\PhpWorkshop\Utils\StringUtils;

if (!function_exists('mb_str_pad')) {

/**
Expand Down Expand Up @@ -31,3 +33,15 @@ function camel_case_to_kebab_case(string $string): string
}, $string);
}
}

if (!function_exists('canonicalise_path')) {

/**
* @param string $path
* @return string
*/
function canonicalise_path(string $path): string
{
return StringUtils::canonicalisePath($path);
}
}
32 changes: 32 additions & 0 deletions test/Util/StringUtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshopTest\Util;

use PhpSchool\PhpWorkshop\Utils\StringUtils;
use PHPUnit\Framework\TestCase;
use PhpSchool\PhpWorkshop\Exception\RuntimeException;

class StringUtilsTest extends TestCase
{
public function testCanonicalisePath(): void
{
$this->assertEquals('/path/to/file', StringUtils::canonicalisePath('/path/to/file'));
$this->assertEquals('/path/to/file', StringUtils::canonicalisePath('/path/././to/file'));
$this->assertEquals('/path/to/file', StringUtils::canonicalisePath('/path///to/file'));
$this->assertEquals('/path/to/file', StringUtils::canonicalisePath('/path/to/file'));
$this->assertEquals('/', StringUtils::canonicalisePath('/path/to/../../'));
$this->assertEquals('/path', StringUtils::canonicalisePath('/path/to/some/../../'));
$this->assertEquals('/some', StringUtils::canonicalisePath('/path/../some/'));
$this->assertEquals('some', StringUtils::canonicalisePath('path/../some/'));
}

public function testExceptionIsThrownIfTryingToTraverseUpPastRoom(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Climbing above the root is not permitted.');

StringUtils::canonicalisePath('/path/to/../../../');
}
}