Skip to content

Commit

Permalink
again mostly rewrote everything
Browse files Browse the repository at this point in the history
  • Loading branch information
josch committed Jul 10, 2024
1 parent 07cec2f commit 38719c9
Show file tree
Hide file tree
Showing 11 changed files with 518 additions and 364 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

> README STILL ON THE WAY TO COMPLETENESS <
## Implementation examples / tests:

Perhaps those examples render the complete README unnecessary.

https://github.com/serjoscha87/php-request-mapper/tree/tests

## Purpose

Ever though that it can not be that hard to reflect pretty-url requets to files on your server's file-system? Well - trying it you will quickly face edge cases that will convince you of the opposite.
Expand Down Expand Up @@ -147,6 +153,12 @@ $myCustomRequestMapper = new RequestMapper('/my-emulation-url', new RequestMappe
));
```

### Mapping Priority

TODO

... higher values mean higher priority ...

### Defaults and fallbacks

Those default properties can be passed to the **RequestMapperConfig** constructor:
Expand Down
47 changes: 36 additions & 11 deletions src/Default404Page.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

/**
* @noinspection PhpUnused
*/

namespace serjoscha87\phpRequestMapper;

/*
Expand All @@ -8,32 +12,53 @@

class Default404Page extends PageBase implements IPage {

public static string $fileName = '404'; // without extension / path
public static string $fileNameRegular = '404'; // without extension / path
public static string $fileNameDetail = '404-detail'; // "

/**
* @throws \Exception
*/
public function __construct(RequestMapper $request_mapper) {
$this->request_mapper/*< inherited from PageBase */ = $request_mapper;
public function __construct(RequestMapper $requestMapper) {
/**
* $this->requestMapper @see PageBase
* $this->filePath @see PageBase
*/
$this->requestMapper = $requestMapper;

if(!file_exists($this->getFilePath()))
throw new \Exception('404 file not found: ' . $this->getFilePath());
//if(!file_exists($this->getFilePath()))
//throw new \Exception('404 file not found: ' . $this->getFilePath());
}

public function __toString() : string {
return self::$fileName;
return self::$fileNameRegular;
}

public function getName() : string {
return self::$fileName;
return self::$fileNameRegular;
}

/**
* @return string the path to the 404 file
*/
public function getFilePath () : string {

// respect a possible filepath the implementer might have set through a callback
if($this->filePath !== null)
return $this->filePath;

$rm = $this->getRequestMapper();
return sprintf('%s/%s%s', $rm->getPageBasePath(), self::$fileName, $rm->getPageFileExtension());

$build404FilePath = fn(string $fileName) => sprintf('%s/%s%s', $rm->getPageBasePath(), $fileName, $rm->getPageFileExtension());

$userRegular404File = false;
if($rm->isDetailPageRequest()) {
$pageRootDetail404File = $build404FilePath(self::$fileNameDetail);
if(is_file($pageRootDetail404File))
return $pageRootDetail404File;
else
$userRegular404File = true;
}

if(!$rm->isDetailPageRequest() || $userRegular404File)
return $build404FilePath(self::$fileNameRegular);

}

}
15 changes: 12 additions & 3 deletions src/DetailPage.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
<?php

/**
* @noinspection PhpUnused
*/

namespace serjoscha87\phpRequestMapper;

class DetailPage extends Page implements IPage {
/**
* @method getFilePath()
* inherited @see Page
*/
class DetailPage/*->Page->PageBase*/ extends Page /* @see PageBase (transitive inheritance)*/ implements IPage {

protected string $query = '';

public function __construct(RequestMapper $request_mapper, $filePath, string $query = '') {
public function __construct(RequestMapper $requestMapper, string $filePath, string $query = '') /** @see Page::__construct */ {
$this->query = $query;
parent::__construct($request_mapper, $filePath);
parent::__construct($requestMapper, $filePath);
}

/**
* @return ?string the name of the parent that contains the actual detail page file. Prefixed with "detail-"
* @throws \Exception
*/
public function getName() : string|null {
return $this->filePath ? sprintf('detail-%s', basename($this->getBasePath())) : null;
Expand Down
1 change: 1 addition & 0 deletions src/IConcreteMapping.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace serjoscha87\phpRequestMapper;

interface IConcreteMapping {}
6 changes: 4 additions & 2 deletions src/IPage.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

/**
* @noinspection PhpUnused
*/

namespace serjoscha87\phpRequestMapper;

interface IPage {

public function __toString() : string;
public function getName() : string|null;

//public function getUri() : string|null;

public function getFilePath () : string|null;
public function getBasePath () : string|null;

Expand Down
Loading

0 comments on commit 38719c9

Please sign in to comment.