Skip to content

Commit f94e6ab

Browse files
author
Marc J. Schmidt
committed
Added logging, added grunffile with traceur/scss transpiler and js minifier. Added download package functionality. Added config:convert output tab.
1 parent ccfafbc commit f94e6ab

21 files changed

+697
-1512
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
/Resources/public/libs
2+
/Resources/public/build
3+
/node_modules
24
/Model/Base
3-
/Model/Map
5+
/Model/Map
6+
/.sass-cache
7+
/vendor

Controller/MainController.php

+89
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,95 @@ public function fiddleAsJsonAction($fiddle, Request $request)
5757
return $result;
5858
}
5959

60+
/**
61+
* @Route("/prepare-download/{fiddle}", requirements={"fiddle" = "[a-zA-Z0-9]{7}"})
62+
* @Method("POST")
63+
*/
64+
public function downloadFiddleAction($fiddle, Request $request)
65+
{
66+
$fiddle = FiddleQuery::create()->findPk($fiddle);
67+
if (!$fiddle) {
68+
throw $this->createNotFoundException('The fiddle does not exist. [json]');
69+
}
70+
71+
/** @var \PropelSandbox\Executor\Executor $executor */
72+
$executor = $this->container->get('propelsandbox.executor');
73+
if (!$executor->isUpdate2Date($fiddle)) {
74+
$executor->execute($fiddle);
75+
}
76+
77+
$directory = $executor->getDirectory($fiddle);
78+
79+
$webDir = $this->get('kernel')->getRootDir() . '/../web';
80+
$zipName = 'fiddle-' . $fiddle->getId() . ".zip";
81+
$path = $webDir . '/downloads/' . $zipName;
82+
83+
if (!is_dir(dirname($path))) {
84+
mkdir(dirname($path), 0770, true);
85+
}
86+
87+
if (file_exists($path)) {
88+
unlink($path);
89+
}
90+
91+
$zip = new \ZipArchive();
92+
$zip->open($path, \ZipArchive::CREATE);
93+
94+
$this->addFolderToZip($directory . '/home/sandbox/generated-classes/', $zip, 'generated-classes/');
95+
$this->addFolderToZip($directory . '/home/sandbox/generated-sql/', $zip, 'generated-sql/');
96+
foreach (['propel_log.txt', 'propel.yml', 'schema.xml', 'script.php'] as $file) {
97+
$zip->addFile($directory . '/home/sandbox/' . $file, $file);
98+
}
99+
//
100+
// $vendors = ['propel/propel', 'composer', 'monolog', 'nikic', 'psr', 'symfony'];
101+
// foreach ($vendors as $vendor) {
102+
// addFolderToZip($directory . '/vendor/'. $vendor . '/', $zip, 'vendor/' . $vendor . '/');
103+
// }
104+
// $zip->addFile($directory . '/vendor/autoload.php', 'vendor/autoload.php');
105+
106+
$zip->addFile(__DIR__ . '/../Resources/meta/package-composer.json', 'composer.json');
107+
108+
$zip->close();
109+
110+
$result = [
111+
'name' => $zipName,
112+
'path' => '/downloads/' . $zipName,
113+
'size' => filesize($path)
114+
];
115+
116+
return $result;
117+
}
118+
119+
/**
120+
* @param string $dir with leading slash
121+
* @param \ZipArchive $zipArchive
122+
* @param string $zipDir with leading slash or empty
123+
*/
124+
protected function addFolderToZip($dir, \ZipArchive $zipArchive, $zipDir = '')
125+
{
126+
if (is_dir($dir)) {
127+
if ($dh = opendir($dir)) {
128+
//Add the directory
129+
if (!empty($zipDir)) {
130+
$zipArchive->addEmptyDir($zipDir);
131+
}
132+
// Loop through all the files
133+
while (($file = readdir($dh)) !== false) {
134+
//If it's a folder, run the function again!
135+
if (!is_file($dir . $file)) {
136+
// Skip parent and root directories
137+
if (($file !== ".") && ($file !== "..")) {
138+
$this->addFolderToZip($dir . $file . "/", $zipArchive, $zipDir . $file . "/");
139+
}
140+
} else {
141+
// Add the files
142+
$zipArchive->addFile($dir . $file, $zipDir . $file);
143+
}
144+
}
145+
}
146+
}
147+
}
148+
60149
/**
61150
* @Route("/my-fiddles")
62151
* @Method("GET")

Executor/Executor.php

+51-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ class Executor
1818
*/
1919
protected $db;
2020

21+
/**
22+
* Disables all checks and fires all commands again and again.
23+
*
24+
* @var bool
25+
*/
26+
protected $debug = false;
27+
28+
/**
29+
* Defines whether prepareRuntimeConfig should call config:convert or not
30+
*
31+
* @var bool
32+
*/
33+
protected $needConfigConvertCall = false;
34+
2135
/**
2236
* @param $redis
2337
*/
@@ -44,13 +58,19 @@ public function execute(Fiddle $fiddle)
4458
}
4559

4660
$this->prepareChroot($fiddle);
61+
$directory = $this->getDirectory($fiddle);
62+
$logFile = $directory . '/home/sandbox/propel_log.txt';
63+
file_put_contents($logFile, '');
4764

65+
$this->prepareDatabase($fiddle);
4866
$this->buildDatabaseSchema($fiddle);
4967
$this->prepareRuntimeConfig($fiddle);
5068

5169
$scriptOutput = $this->executeInJail($fiddle);
5270
$fiddle->setScriptOutput($scriptOutput);
5371
$this->extractDbInformation($fiddle);
72+
73+
$fiddle->setLogOutput(file_get_contents($logFile));
5474
}
5575

5676
protected function prepareChroot(Fiddle $fiddle)
@@ -69,13 +89,11 @@ protected function prepareChroot(Fiddle $fiddle)
6989
\$autoloader->add('', __DIR__ . '/generated-classes/');
7090
require './generated-conf/config.php';
7191
72-
7392
$php
7493
EOF
7594
);
7695
file_put_contents($directory . '/home/sandbox/schema.xml', $fiddle->getSchema());
7796

78-
$this->prepareDatabase($fiddle);
7997
}
8098

8199
/**
@@ -90,7 +108,7 @@ protected function prepareDatabase(Fiddle $fiddle)
90108
$fiddleEscaped = $this->db->quote($fiddle->getId(), \PDO::PARAM_STR);
91109
$fiddleId = $this->db->quoteIdentifier($fiddle->getId());
92110

93-
if (file_exists($directory . '/home/sandbox/propel.yml')) {
111+
if (!$this->debug && file_exists($directory . '/home/sandbox/propel.yml')) {
94112
return false;
95113
}
96114

@@ -120,6 +138,8 @@ protected function prepareDatabase(Fiddle $fiddle)
120138
)
121139
);
122140

141+
$this->needConfigConvertCall = true;
142+
123143
$this->db->executeQuery(
124144
sprintf(
125145
"GRANT USAGE, ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT, UPDATE ON %s.* TO %s@'localhost'",
@@ -128,7 +148,6 @@ protected function prepareDatabase(Fiddle $fiddle)
128148
)
129149
);
130150

131-
132151
$propelConfig = <<<EOF
133152
propel:
134153
database:
@@ -141,6 +160,11 @@ classname: Propel\Runtime\Connection\DebugPDO
141160
password: $password
142161
attributes:
143162
runtime:
163+
log:
164+
defaultLogger:
165+
type: stream
166+
path: ./propel_log.txt
167+
level: 100
144168
defaultConnection: default
145169
connections:
146170
- default
@@ -159,12 +183,12 @@ protected function prepareRuntimeConfig(Fiddle $fiddle)
159183
{
160184
$directory = $this->getDirectory($fiddle);
161185

162-
if (file_exists($directory . '/home/sandbox/generated-conf/')) {
186+
if (!$this->needConfigConvertCall && !$this->debug && file_exists($directory . '/home/sandbox/generated-conf/config.php')) {
163187
return;
164188
}
165189

166-
$this->executeInJail($fiddle, '/usr/bin/php /vendor/propel/propel/bin/propel config:convert');
167-
190+
$output = $this->executeInJail($fiddle, '/bin/build-config.sh');
191+
$fiddle->setConfigBuildOutput($output);
168192
}
169193

170194
/**
@@ -176,18 +200,18 @@ protected function buildDatabaseSchema(Fiddle $fiddle)
176200
$currentHash = $this->getSchemaHash($fiddle);
177201
$directory = $this->getDirectory($fiddle);
178202

179-
if ($lastBuiltHash !== $currentHash) {
203+
if ($this->debug || $lastBuiltHash !== $currentHash) {
180204
$buildOutput = $this->executeInJail($fiddle, '/bin/sql-build.sh');
181205
$fiddle->setSqlBuildOutput($buildOutput);
182206

183207
$buildOutput = $this->executeInJail($fiddle, '/bin/model-build.sh');
184208
$fiddle->setModelBuildOutput($buildOutput);
185209
}
186210

187-
if (file_exists($directory . '/home/sandbox/generated-sql/')) {
211+
if ($this->debug || file_exists($directory . '/home/sandbox/generated-sql/')) {
188212
$insertOutput = $this->executeInJail(
189213
$fiddle,
190-
'/usr/bin/php /vendor/propel/propel/bin/propel sql:insert -vv'
214+
'/bin/sql-insert.sh'
191215
);
192216
$fiddle->setSqlInsertOutput($insertOutput);
193217
} else {
@@ -206,11 +230,12 @@ protected function extractDbInformation(Fiddle $fiddle)
206230
foreach ($tables as $table) {
207231
$table = current($table);
208232
$tableIdentifier = $identifier . '.' . $this->db->quoteIdentifier($table);
233+
$columns = $this->db->fetchAll(sprintf('SHOW COLUMNS FROM %s', $tableIdentifier));
209234
$items = $this->db->fetchAll(sprintf('SELECT * FROM %s LIMIT 1000', $tableIdentifier));
210235
$result[] = [
211236
'name' => $table,
212237
'count' => count($items),
213-
'columns' => $items ? array_keys($items[0]) : [],
238+
'columns' => $columns,
214239
'items' => $items ?: false
215240
];
216241
}
@@ -237,11 +262,25 @@ protected function getSchemaHash(Fiddle $fiddle)
237262
* @param Fiddle $fiddle
238263
* @return string
239264
*/
240-
protected function getDirectory(Fiddle $fiddle)
265+
public function getDirectory(Fiddle $fiddle)
241266
{
242267
return '/tmp/propelsandbox/chroots/' . $fiddle->getId();
243268
}
244269

270+
/**
271+
* @param Fiddle $fiddle
272+
* @return boolean
273+
*/
274+
public function isUpdate2Date(Fiddle $fiddle)
275+
{
276+
$directory = $this->getDirectory($fiddle);
277+
if (!file_exists($directory . '/home/sandbox/propel.yml')) {
278+
return false;
279+
}
280+
281+
return true;
282+
}
283+
245284
/**
246285
* @param Fiddle $fiddle
247286
* @param string $command needs absolute path to the binary

Gruntfile.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module.exports = function(grunt) {
2+
grunt.initConfig({
3+
shell: {
4+
make_traceur_app: {
5+
command: 'cd Resources/public; ../../node_modules/traceur/traceur --out build/app.js app.js --experimental --source-maps'
6+
}
7+
},
8+
9+
uglify: {
10+
build: {
11+
options: {
12+
compress: false,
13+
mangle: false,
14+
sourceMap: true,
15+
sourceMapIncludeSources: true
16+
},
17+
files: {
18+
'Resources/public/build/complete-app.js': [
19+
'node_modules/traceur/bin/traceur-runtime.js',
20+
'Resources/public/libs/angular/angular.js',
21+
'Resources/public/libs/codemirror/lib/codemirror.js',
22+
'Resources/public/libs/codemirror/mode/xml/xml.js',
23+
'Resources/public/libs/codemirror/mode/htmlmixed/htmlmixed.js',
24+
'Resources/public/libs/codemirror/mode/clike/clike.js',
25+
'Resources/public/libs/codemirror/mode/php/php.js',
26+
'Resources/public/libs/angular-ui-codemirror/ui-codemirror.min.js',
27+
'Resources/public/libs/ladda/js/spin.js',
28+
'Resources/public/libs/ladda/dist/ladda.min.js',
29+
'Resources/public/libs/angular-route/angular-route.js',
30+
'Resources/public/libs/angular-bootstrap/ui-bootstrap.min.js',
31+
'Resources/public/libs/angular-bootstrap/ui-bootstrap-tpls.min.js',
32+
'Resources/public/build/app.js'
33+
]
34+
}
35+
}
36+
},
37+
38+
sass: {
39+
dist: {
40+
options: {
41+
style: 'expanded'
42+
},
43+
files: {
44+
'Resources/public/build/app.css': 'Resources/public/css/app.scss'
45+
}
46+
}
47+
},
48+
49+
watch: {
50+
scripts: {
51+
files: [
52+
'Resources/public/*.js',
53+
'Resources/public/controller/*.js',
54+
'Resources/public/filters/*.js'
55+
],
56+
tasks: ['shell', 'uglify'],
57+
options: {
58+
interrupt: true,
59+
debounceDelay: 50
60+
}
61+
},
62+
styles: {
63+
files: 'Resources/public/css/*',
64+
tasks: ['sass'],
65+
options: {
66+
interrupt: true,
67+
debounceDelay: 50
68+
}
69+
}
70+
}
71+
});
72+
73+
grunt.loadNpmTasks('grunt-contrib-watch');
74+
grunt.loadNpmTasks('grunt-shell');
75+
grunt.loadNpmTasks('grunt-contrib-uglify');
76+
grunt.loadNpmTasks('grunt-contrib-sass');
77+
78+
grunt.registerTask('default', ['shell', 'uglify', 'sass']);
79+
};

Model/Fiddle.php

+10
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,15 @@ public function setDatabaseInformation($v)
109109
return parent::setDatabaseInformation($this->arrayToString($v));
110110
}
111111

112+
public function getConfigBuildOutput()
113+
{
114+
return $this->stringToArray(parent::getConfigBuildOutput());
115+
}
116+
117+
public function setConfigBuildOutput($v)
118+
{
119+
return parent::setConfigBuildOutput($this->arrayToString($v));
120+
}
121+
112122

113123
}

Resources/config/schema.xml

+3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
<column name="schema" type="LONGVARCHAR" />
99
<column name="php" type="LONGVARCHAR" />
1010

11+
<column name="log_output" type="LONGVARCHAR" />
12+
1113
<column name="script_output" type="LONGVARCHAR" />
1214
<column name="sql_build_output" type="LONGVARCHAR" />
1315
<column name="sql_insert_output" type="LONGVARCHAR" />
1416
<column name="model_build_output" type="LONGVARCHAR" />
1517
<column name="schema_migration_output" type="LONGVARCHAR" />
18+
<column name="config_build_output" type="LONGVARCHAR" />
1619

1720
<column name="database_information" type="LONGVARCHAR" />
1821

Resources/meta/build-config.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
echo "execute ./vendor/propel/propel/bin/propel config:convert -vv"
4+
php /vendor/propel/propel/bin/propel config:convert -vv
5+
6+
tree ./generated-conf/
7+
8+
echo cat ./generated-conf/config.php:
9+
cat ./generated-conf/config.php

0 commit comments

Comments
 (0)