Skip to content

Commit 688781c

Browse files
committed
In working state again
1 parent 77d815e commit 688781c

File tree

20 files changed

+309
-106
lines changed

20 files changed

+309
-106
lines changed

example/MyApp/Foundation/Models/Base.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ model:
22
name: Base
33
methods:
44
get.translated.attributes:
5+
comment: Get the translated attributes
56
returnType: array
67
content:
78
php-core: return $this->translatedAttributes();

module-guestbook.yml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,8 @@ resources:
5656

5757
Migrations.create_some_fucking_shit:
5858
migration:
59-
columns:
60-
parent_id:
61-
type: string
62-
name:
63-
type: string
64-
email:
65-
type: string
66-
ip:
67-
type: string
68-
body:
69-
type: string
70-
created_at:
71-
type: string
72-
updated_at:
73-
type: string
59+
imports:
60+
Gosidesign.Guestbook.Models.Message:
61+
- columns
7462
compilers:
7563
- php-laravel
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php namespace Layla\Cody\Blueprints;
2+
3+
class Package {
4+
5+
public $resources = array();
6+
7+
public function __construct($vendor, $name)
8+
{
9+
$this->vendor = $vendor;
10+
$this->name = $name;
11+
}
12+
13+
public function addResource(Resource $resource)
14+
{
15+
$this->resources[$resource->getName()] = $resource;
16+
}
17+
18+
public function getResources()
19+
{
20+
return $this->resources;
21+
}
22+
23+
public function getVendor()
24+
{
25+
return $this->vendor;
26+
}
27+
28+
public function getName()
29+
{
30+
return $this->name;
31+
}
32+
33+
public function getIdentifier()
34+
{
35+
return $this->vendor.'.'.$this->name;
36+
}
37+
38+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php namespace Layla\Cody\Blueprints;
2+
3+
class Resource {
4+
5+
public function __construct($package, $type, $name, $configuration, $compilers)
6+
{
7+
$this->package = $package;
8+
$this->type = $type;
9+
$this->name = $name;
10+
$this->configuration = $configuration;
11+
$this->compilers = $compilers;
12+
}
13+
14+
public function getPackage()
15+
{
16+
return $this->package;
17+
}
18+
19+
public function getType()
20+
{
21+
return $this->type;
22+
}
23+
24+
public function getName()
25+
{
26+
return $this->name;
27+
}
28+
29+
public function getConfiguration()
30+
{
31+
return $this->configuration;
32+
}
33+
34+
public function getCompilers()
35+
{
36+
return $this->compilers;
37+
}
38+
39+
public function getIdentifier()
40+
{
41+
return $this->package->getIdentifier().'.'.$this->name;
42+
}
43+
44+
public function get($key, $default = null)
45+
{
46+
return isset($this->configuration[$key]) ? $this->configuration[$key] : $default;
47+
}
48+
49+
public function set($key, $value)
50+
{
51+
$this->configuration[$key] = $value;
52+
53+
return $this;
54+
}
55+
56+
public function add($type, $key, $value)
57+
{
58+
$this->configuration[$type][$key] = $value;
59+
}
60+
61+
}
62+

src/Layla/Cody/Cody.php

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,6 @@ public function setInput($input)
1919
$this->input = $input;
2020
}
2121

22-
public function compileResource($type, $package, $name, $configuration, $compilers)
23-
{
24-
$files = array();
25-
foreach($compilers as $compiler)
26-
{
27-
$compiler = $this->app->make('compiler.'.$compiler, array($package, $name, $configuration));
28-
29-
list($path, $content) = $compiler->compile($type);
30-
31-
$files[$path] = $content;
32-
}
33-
34-
return $files;
35-
}
36-
3722
public function compileInput($input, $format = null)
3823
{
3924
if( ! is_null($format))
@@ -43,37 +28,23 @@ public function compileInput($input, $format = null)
4328
$input = $parser->parse($input);
4429
}
4530

46-
if( ! isset($input['package']))
47-
{
48-
throw new Exception("Syntax error: 'package' key not present in input. Given input is: ".json_encode($input, JSON_PRETTY_PRINT));
49-
}
50-
51-
if( ! isset($input['resources']))
52-
{
53-
throw new Exception("Syntax error: 'resources' key not present in input. Given input is: ".json_encode($input, JSON_PRETTY_PRINT));
54-
}
31+
$packages = Objectifier::objectify($input);
5532

56-
$package = $input['package'];
57-
$resources = $input['resources'];
58-
59-
$files = array();
60-
foreach($resources as $name => $resource)
33+
$results = array();
34+
foreach($packages as $package)
6135
{
62-
if( ! isset($resource['compilers']))
36+
foreach($package->getResources() as $resource)
6337
{
64-
throw new Exception("Syntax error: 'compilers' key not present in resource configuration. Given resource configuration is: ".json_encode($resource, JSON_PRETTY_PRINT));
65-
}
66-
67-
$compilers = $resource['compilers'];
68-
unset($resource['compilers']);
69-
70-
$type = key($resource);
71-
$configuration = $resource[$type];
38+
foreach($resource->getCompilers() as $identifier)
39+
{
40+
$compiler = $this->app->make('compiler.'.$identifier, array($resource));
7241

73-
$files = array_merge($files, $this->compileResource($type, $package, $name, $configuration, $compilers));
42+
$results[$compiler->getDestination()] = $compiler->compile();
43+
}
44+
}
7445
}
7546

76-
return $files;
47+
return $results;
7748
}
7849

7950
public function json()

src/Layla/Cody/CodyServiceProvider.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ protected function registerCompilers()
6060

6161
$app->bind('compiler.php-laravel', function($app, $arguments)
6262
{
63-
list($package, $name, $configuration) = $arguments;
63+
list($resource) = $arguments;
6464

65-
return new LaravelCompiler($app, $package, $name, $configuration);
65+
return new LaravelCompiler($app, $resource);
6666
});
6767

6868
$app->bind('compiler.js-ember', function($app, $arguments)
6969
{
70-
list($package, $name, $configuration) = $arguments;
70+
list($resource) = $arguments;
7171

72-
return new EmberCompiler($app, $app, $name, $configuration);
72+
return new EmberCompiler($app, $resource);
7373
});
7474

7575
$app->bind('compiler.python-django', function($app, $arguments)
7676
{
77-
list($package, $name, $configuration) = $arguments;
77+
list($resource) = $arguments;
7878

79-
return new DjangoCompiler($app, $app, $name, $configuration);
79+
return new DjangoCompiler($app, $resource);
8080
});
8181
}
8282

src/Layla/Cody/Compilers/Compiler.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
class Compiler {
44

5-
public function __construct($app, $package, $name, $configuration)
5+
public function __construct($app, $resource)
66
{
77
$this->app = $app;
8-
$this->package = $package;
9-
$this->name = $name;
10-
$this->configuration = $configuration;
8+
$this->resource = $resource;
119

1210
$this->setup();
1311
}
@@ -95,7 +93,7 @@ protected function indent($text, $amount = 1)
9593

9694
public function addProperty($name, $configuration)
9795
{
98-
$this->configuration['properties'][$name] = $configuration;
96+
$this->resource->add('properties', $name, $configuration);
9997

10098
return $this;
10199
}
@@ -104,7 +102,7 @@ public function addMethod($name, $configuration, $body)
104102
{
105103
$configuration['content'][$this->compiler] = $body;
106104

107-
$this->configuration['methods'][$name] = $configuration;
105+
$this->resource->add('methods', $name, $configuration);
108106

109107
return $this;
110108
}
@@ -113,7 +111,7 @@ public function get($key, $default = null, $on = null)
113111
{
114112
if(is_null($on))
115113
{
116-
$on = $this->configuration;
114+
$on = $this->resource->getConfiguration();
117115
}
118116

119117
return array_key_exists($key, $on) ? $on[$key] : $default;

src/Layla/Cody/Compilers/Php/Core/ClassCompiler.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ class ClassCompiler extends PhpCompiler {
1414
*/
1515
public function getUses()
1616
{
17+
$package = $this->resource->getPackage();
18+
1719
$uses = $this->get('uses', array());
1820

1921
$namespaceCompiler = $this->getNamespaceCompilerFor($this->get('base'));
2022

2123
// Add baseclass to use statements if necesarry
22-
if( ! is_null($this->get('base')) && ! $namespaceCompiler->isWithinNamespaceOf($this->package.'.'.$this->name))
24+
if( ! is_null($this->get('base')) && ! $namespaceCompiler->isWithinNamespaceOf($package->getVendor().'.'.$package->getName()))
2325
{
2426
$uses[] = $namespaceCompiler->getName();
2527
}
@@ -59,7 +61,7 @@ protected function compileComment()
5961
*/
6062
public function compile()
6163
{
62-
$nameCompiler = $this->getNamespaceCompilerFor($this->package.'.'.$this->name);
64+
$nameCompiler = $this->getNamespaceCompilerFor($this->resource->getIdentifier());
6365
$baseCompiler = $this->getNamespaceCompilerFor($this->get('base'));
6466

6567
// Start the php file
@@ -134,22 +136,7 @@ public function compile()
134136
// Close the class
135137
$content .= "\n}\n\n";
136138

137-
$filename = $this->getFilename();
138-
139-
return array(
140-
$filename,
141-
$content
142-
);
143-
}
144-
145-
public function getFilename()
146-
{
147-
$packageParts = explode('.', $this->package);
148-
list($vendor, $name) = $packageParts;
149-
150-
$nameParts = explode('.', $this->name);
151-
152-
return 'vendor/'.strtolower($vendor.'/'.$name).'/src/'.implode('/', $packageParts).'/'.implode('/', $nameParts).'.php';
139+
return $content;
153140
}
154141

155142
}

src/Layla/Cody/Compilers/Php/Core/MethodCompiler.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,14 @@ public function compile()
6565
return $comment."\n".'public function '.$this->getName()."()\n{\n".$this->indent($this->get('content'))."\n}";
6666
}
6767

68+
public function get($key, $default = null, $on = null)
69+
{
70+
if(is_null($on))
71+
{
72+
$on = $this->configuration;
73+
}
74+
75+
return array_key_exists($key, $on) ? $on[$key] : $default;
76+
}
77+
6878
}

src/Layla/Cody/Compilers/Php/Core/ParameterCompiler.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,14 @@ public function getLine()
2121
return array('@param', $this->get('type'), '$'.$this->name, $this->get('comment'));
2222
}
2323

24+
public function get($key, $default = null, $on = null)
25+
{
26+
if(is_null($on))
27+
{
28+
$on = $this->configuration;
29+
}
30+
31+
return array_key_exists($key, $on) ? $on[$key] : $default;
32+
}
33+
2434
}

0 commit comments

Comments
 (0)