Skip to content

Commit 779ff27

Browse files
committed
added PHP Session
1 parent 2b390ba commit 779ff27

File tree

7 files changed

+96
-21
lines changed

7 files changed

+96
-21
lines changed

cjs/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,31 @@
1717
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1818
*/
1919

20-
module.exports = class Session extends Map {
20+
module.exports = class Session {
2121
constructor(modules) {
22-
super([['_', modules._]]);
22+
this.map = new Map([['_', modules._]]);
2323
this.flushed = new Set;
2424
this.modules = modules;
2525
}
2626
add(module) {
27-
if (!this.has(module)) {
28-
const {module: m, code, dependencies} = this.modules[module];
29-
for (let i = 0, {length} = dependencies; i < length; i++)
30-
this.add(dependencies[i]);
31-
this.set(module, {module: m, code});
27+
if (!this.map.has(module)) {
28+
const info = this.modules[module];
29+
info.dependencies.forEach(this.add, this);
30+
this.map.set(module, info);
3231
}
3332
return this;
3433
}
3534
flush() {
3635
const output = [];
37-
this.forEach(({module, code}, name) => {
36+
this.map.forEach(({module, code}, name) => {
3837
if (!this.flushed.has(name)) {
3938
this.flushed.add(name);
4039
output.push(module);
4140
}
4241
if (0 < code.length)
4342
output.push(code);
4443
});
45-
this.clear();
44+
this.map.clear();
4645
return output.join('\n');
4746
}
4847
}

esm/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,31 @@
1616
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1717
*/
1818

19-
export default class Session extends Map {
19+
export default class Session {
2020
constructor(modules) {
21-
super([['_', modules._]]);
21+
this.map = new Map([['_', modules._]]);
2222
this.flushed = new Set;
2323
this.modules = modules;
2424
}
2525
add(module) {
26-
if (!this.has(module)) {
27-
const {module: m, code, dependencies} = this.modules[module];
28-
for (let i = 0, {length} = dependencies; i < length; i++)
29-
this.add(dependencies[i]);
30-
this.set(module, {module: m, code});
26+
if (!this.map.has(module)) {
27+
const info = this.modules[module];
28+
info.dependencies.forEach(this.add, this);
29+
this.map.set(module, info);
3130
}
3231
return this;
3332
}
3433
flush() {
3534
const output = [];
36-
this.forEach(({module, code}, name) => {
35+
this.map.forEach(({module, code}, name) => {
3736
if (!this.flushed.has(name)) {
3837
this.flushed.add(name);
3938
output.push(module);
4039
}
4140
if (0 < code.length)
4241
output.push(code);
4342
});
44-
this.clear();
43+
this.map.clear();
4544
return output.join('\n');
4645
}
4746
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
"description": "JS in JSON Session",
55
"main": "./cjs/index.js",
66
"scripts": {
7-
"build": "npm run cjs",
8-
"cjs": "ascjs --no-default esm cjs"
7+
"build": "npm run cjs && npm test",
8+
"cjs": "ascjs --no-default esm cjs",
9+
"test": "node test/test.js && echo '' && php php/test.php"
910
},
1011
"keywords": [
1112
"JS",

php/session.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php namespace JSinJSON;
2+
3+
/*!
4+
* ISC License
5+
*
6+
* Copyright (c) 2021, Andrea Giammarchi, @WebReflection
7+
*
8+
* Permission to use, copy, modify, and/or distribute this software for any
9+
* purpose with or without fee is hereby granted, provided that the above
10+
* copyright notice and this permission notice appear in all copies.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
15+
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
17+
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18+
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19+
*/
20+
21+
class Session {
22+
public function __construct($modules) {
23+
$this->map = new \stdClass;
24+
$this->map->_ = $modules->_;
25+
$this->flushed = array();
26+
$this->modules = $modules;
27+
}
28+
public function add($module) {
29+
if (!property_exists($this->map, $module)) {
30+
$info = $this->modules->$module;
31+
foreach ($info->dependencies as $dependency)
32+
$this->add($dependency);
33+
$this->map->$module = $info;
34+
}
35+
}
36+
public function flush() {
37+
$output = array();
38+
foreach ($this->map as $name => $info) {
39+
if (array_search($name, $this->flushed, true) === false) {
40+
$this->flushed[] = $name;
41+
$output[] = $info->module;
42+
}
43+
if (0 < strlen($info->code))
44+
$output[] = $info->code;
45+
}
46+
$this->map = new \stdClass;
47+
return implode("\n", $output);
48+
}
49+
}
50+
51+
?>

php/test.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
include(__DIR__.'/session.php');
4+
5+
$session = new JSinJSON\Session(
6+
json_decode('{"_":{"code":"","module":"true;"},"Test":{"code":"console.log(1)","module":"console.log(Math.random())","dependencies":[]}}')
7+
);
8+
9+
$session->add('Test');
10+
echo $session->flush();
11+
echo "\n";
12+
$session->add('Test');
13+
echo $session->flush();
14+
echo "\n";
15+
16+
?>

test/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const Session = require('../cjs');
2+
3+
const session = new Session(
4+
JSON.parse('{"_":{"code":"","module":"true;"},"Test":{"code":"console.log(1)","module":"console.log(Math.random())","dependencies":[]}}')
5+
);
6+
7+
session.add('Test');
8+
console.log(session.flush());
9+
session.add('Test');
10+
console.log(session.flush());

0 commit comments

Comments
 (0)