Skip to content

Commit

Permalink
Merge pull request #252 from koriym/example
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
koriym committed Aug 21, 2017
2 parents 85445b5 + 26c8b98 commit 43a7e9c
Show file tree
Hide file tree
Showing 27 changed files with 174 additions and 99 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -5,3 +5,5 @@ build
.php_cs.cache
tests/Fake/fake-app/var/log/*
tests/Fake/fake-app/var/tmp/*
example/var/tmp/*
example/var/log/*
2 changes: 1 addition & 1 deletion .php_cs
Expand Up @@ -125,6 +125,6 @@ return \PhpCsFixer\Config::create()
->setFinder(
PhpCsFixer\Finder::create()
->exclude('tests/Fake')
->exclude('var')
->exclude('example/')
->in(__DIR__)
)->setLineEnding("\n");
8 changes: 0 additions & 8 deletions docs/demo-app/bootstrap/api.php

This file was deleted.

38 changes: 0 additions & 38 deletions docs/demo-app/bootstrap/bootstrap.php

This file was deleted.

8 changes: 0 additions & 8 deletions docs/demo-app/bootstrap/web.php

This file was deleted.

10 changes: 0 additions & 10 deletions docs/demo-app/var/conf/aura.route.php

This file was deleted.

Empty file removed docs/demo-app/var/log/.placefolder
Empty file.
Empty file removed docs/demo-app/var/tmp/.placefolder
Empty file.
14 changes: 0 additions & 14 deletions docs/demo-app/var/www/.htaccess

This file was deleted.

Binary file removed docs/demo-app/var/www/favicon.ico
Binary file not shown.
6 changes: 0 additions & 6 deletions docs/demo-app/var/www/index.php

This file was deleted.

31 changes: 31 additions & 0 deletions example/README.md
@@ -0,0 +1,31 @@
# Demo

This is a minimal BEAR.Sunday application with BEAR.Package.

## Install and Test
```
composer install
./vendor/bin/phpunit
```

## Run


console access

```
php public/index get /
```

web access

```
php -S 127.0.0.1:8080 -t public
```

batch command

```
php bin/run.php
```

18 changes: 18 additions & 0 deletions example/bin/run.php
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of the BEAR.Package package.
*
* @license http://opensource.org/licenses/MIT MIT
*/
use BEAR\Package\Bootstrap;

require dirname(__DIR__) . '/load.php';

$page = (new Bootstrap)
->getApp('MyVendor\MyApp', 'hal-app')
->resource
->get
->uri('page://self/api/user')(['id' => 1]);

echo $page->code . PHP_EOL;
echo (string) $page;
17 changes: 17 additions & 0 deletions example/composer.json
@@ -0,0 +1,17 @@
{
"name": "my-vendor/hello-package",
"require": {
"bear/sunday": "^1.2"
},
"autoload": {
"psr-4": {
"MyVendor\\HelloWorld\\": "src"
}
},
"require-dev": {
"phpunit/phpunit": "^6.3"
},
"scripts": {
"serve": "php -S 127.0.0.1:8080 -t public"
}
}
12 changes: 12 additions & 0 deletions example/load.php
@@ -0,0 +1,12 @@
<?php
/**
* This file is part of the BEAR.Package package.
*
* @license http://opensource.org/licenses/MIT MIT
*/
use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = (require dirname(__DIR__) . '/vendor/autoload.php');
/* @var $loader \Composer\Autoload\ClassLoader */
$loader->addPsr4('MyVendor\MyApp' . '\\', __DIR__ . '/src');
AnnotationRegistry::registerLoader('class_exists');
22 changes: 22 additions & 0 deletions example/public/index.php
@@ -0,0 +1,22 @@
<?php
use BEAR\Package\Bootstrap;
use BEAR\Sunday\Extension\Application\AbstractApp;

require dirname(__DIR__) . '/load.php';

$context = PHP_SAPI === 'cli' ? 'cli-hal-app' : 'hal-app';

$app = (new Bootstrap)->getApp('MyVendor\MyApp', $context);
/* @var $app AbstractApp */
$request = $app->router->match($GLOBALS, $_SERVER);
try {
$page = $app
->resource
->{$request->method}
->uri($request->path)($request->query)
->transfer($app->responder, $_SERVER);
exit(0);
} catch (\Exception $e) {
$app->error->handle($e, $request)->transfer();
exit(1);
}
File renamed without changes.
File renamed without changes.
Expand Up @@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace MyVendor\MyApp\Resource\App;
namespace MyVendor\MyApp\Resource\Page\Api;

use BEAR\Resource\ResourceObject;
use BEAR\Sunday\Inject\ResourceInject;
Expand Down
Expand Up @@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace MyVendor\MyApp\Resource\App;
namespace MyVendor\MyApp\Resource\Page\Api;

use BEAR\RepositoryModule\Annotation\Cacheable;
use BEAR\Resource\Annotation\Embed;
Expand All @@ -17,14 +17,16 @@
class User extends ResourceObject
{
/**
* @Link(rel="profile", href="/profile{?id}")
* @Embed(rel="website", src="/website{?id}")
* @Embed(rel="contact", src="/contact{?id}")
* @Link(rel="profile", href="/api/profile{?id}")
* @Embed(rel="website", src="/api/website{?id}")
* @Embed(rel="contact", src="/api/contact{?id}")
*/
public function onGet($id)
{
$this['id'] = $id;
$this['name'] = 'Akihito Koriyama';
$this->body += [
'id' => $id,
'name' => 'Koriym'
];

return $this;
}
Expand Down
Expand Up @@ -4,16 +4,17 @@
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace MyVendor\MyApp\Resource\App;
namespace MyVendor\MyApp\Resource\Page\Api;

use BEAR\Resource\ResourceObject;

class Website extends ResourceObject
{
public function onGet($id)
{
$this['url'] = "http:://example.org/{$id}";
$this['id'] = $id;
$this->body = [
'url' => "http:://example.org/{$id}"
];

return $this;
}
Expand Down
Expand Up @@ -10,9 +10,9 @@

class Index extends ResourceObject
{
public function onGet($name = 'BEAR.Sunday')
public function onGet($name = 'BEAR.Sunday') : ResourceObject
{
$this['greeting'] = 'Hello ' . $name;
$this->body = ['greeting' => 'Hello ' . $name];

return $this;
}
Expand Down
Expand Up @@ -12,7 +12,7 @@
class User extends ResourceObject
{
/**
* @Embed(rel="user1", src="app://self/user{?id}")
* @Embed(rel="user", src="/api/user{?id}")
*/
public function onGet($id)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Provide/Representation/HalRenderer.php
Expand Up @@ -120,7 +120,7 @@ private function valuateElements(ResourceObject &$ro)
*/
private function isDifferentSchema(ResourceObject $parentRo, ResourceObject $childRo) : bool
{
return $parentRo->uri->host . $parentRo->uri->host !== $childRo->uri->scheme . $childRo->uri->host;
return $parentRo->uri->scheme . $parentRo->uri->host !== $childRo->uri->scheme . $childRo->uri->host;
}

private function getHal(AbstractUri $uri, array $body, array $annotations) : Hal
Expand Down
21 changes: 21 additions & 0 deletions tests/Fake/fake-app/src/Resource/App/Emb.php
@@ -0,0 +1,21 @@
<?php
/**
* This file is part of the BEAR.Package package.
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace FakeVendor\HelloWorld\Resource\App;

use BEAR\Resource\Annotation\Embed;
use BEAR\Resource\ResourceObject;

class Emb extends ResourceObject
{
/**
* @Embed(rel="user", src="/user{?id}")
*/
public function onGet($id)
{
return $this;
}
}
33 changes: 33 additions & 0 deletions tests/Provide/Representation/HalRendererTest.php
Expand Up @@ -69,6 +69,39 @@ public function testRenderPost()
}

public function testRenderEmbed()
{
$ro = $this->resource->get->uri('app://self/emb?id=1')->eager->request();
$result = (string) $ro;
$expect = '{
"_embedded": {
"user": {
"id": "1",
"friend_id": "f1",
"org_id": "o1",
"_links": {
"self": {
"href": "/user?id=1"
},
"friend": {
"href": "/friend?id=f1"
},
"org": {
"href": "/org?id=o1"
}
}
}
},
"_links": {
"self": {
"href": "/emb?id=1"
}
}
}
';
$this->assertSame($expect, $result);
}

public function testNoEmbededLinksWhenSchemaIsDifferent()
{
$ro = $this->resource->get->uri('page://self/emb')->eager->request();
$result = (string) $ro;
Expand Down

0 comments on commit 43a7e9c

Please sign in to comment.