Skip to content

Commit 247efb6

Browse files
committed
Profiles work
TODO: Fix all the tests
1 parent 9330dab commit 247efb6

File tree

16 files changed

+409
-160
lines changed

16 files changed

+409
-160
lines changed

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ $ sudo cp phpcrsh.phar /usr/bin/local/phpcrsh
3030

3131
To connect to a doctrine-dbal PHPCR repository:
3232

33-
$ phpcr --db-name=foobar --db-username=user --db-password=foobar
33+
$ phpcr --transport=doctrine-dbal --db-name=foobar --db-username=user --db-password=foobar
3434

3535
Full definition:
3636

3737
````bash
3838
Usage:
39-
phpcr_shell [-h|--help] [-v|--verbose] [-V|--version] [--ansi] [--no-ansi] [-t|--transport="..."] [-pu|--phpcr-username="..."] [-pp|--phpcr-password[="..."]] [-pw|--phpcr-workspace[="..."]] [-du|--db-username="..."] [-dn|--db-name="..."] [-dp|--db-password[="..."]] [-dh|--db-host="..."] [-dd|--db-driver="..."] [-dP|--db-path="..."] [--no-interaction] [--unsupported] [-url|--repo-url="..."] [--command="..."]
39+
phpcrsh [-h|--help] [-v|--verbose] [-V|--version] [--ansi] [--no-ansi] [-t|--transport="..."] [-pu|--phpcr-username="..."] [-pp|--phpcr-password[="..."]] [-pw|--phpcr-workspace[="..."]] [-du|--db-username="..."] [-dn|--db-name="..."] [-dp|--db-password[="..."]] [-dh|--db-host="..."] [-dd|--db-driver="..."] [-dP|--db-path="..."] [--no-interaction] [--unsupported] [-url|--repo-url="..."] [--command="..."]
4040

4141
Options:
4242
--help (-h) Display this help message.
@@ -60,7 +60,32 @@ Options:
6060
--command Run the given command
6161
````
6262

63-
Todo:
63+
## Using profiles
64+
65+
Profiles enable you to save and reuse connection settings. Profiles can be
66+
created or used by using the `--profile` option.
67+
68+
To create or update a profile, use it in conjunction with `--transport`, i.e.:
69+
70+
````bash
71+
$ phpcrsh --profile=mydb --transport=doctrine-dbal --db-user=foobar --db-name=mydb
72+
Create new profile "mydb"?
73+
````
74+
75+
To use the profile:
76+
77+
````bash
78+
$ phpcrsh --profile=mydb
79+
````
80+
81+
Or use the short syntax:
82+
83+
````bash
84+
$ phpcrsh --pmydb
85+
````
86+
87+
88+
## Todo
6489

6590
- Better querying support
6691
- Better autocompletion

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"jackalope/jackalope-jackrabbit": "dev-master",
88
"jackalope/jackalope": "dev-master",
99
"phpcr/phpcr": "dev-master",
10-
"phpcr/phpcr-utils": "dev-master"
10+
"phpcr/phpcr-utils": "dev-master",
11+
"finder/finder": "~2.0"
1112
},
1213
"require-dev": {
1314
"mockery/mockery": "0.9",
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Config;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use PHPCR\Shell\Console\Helper\ConfigHelper;
8+
use PHPCR\Shell\Config\Profile;
9+
use Symfony\Component\Filesystem\Filesystem;
10+
11+
class ProfileLoaderSpec extends ObjectBehavior
12+
{
13+
function let(
14+
ConfigHelper $configHelper,
15+
Filesystem $filesystem
16+
)
17+
{
18+
$configHelper->getConfigDir()->willReturn(__DIR__);
19+
$this->beConstructedWith($configHelper, $filesystem);
20+
}
21+
22+
function it_is_initializable()
23+
{
24+
$this->shouldHaveType('PHPCR\Shell\Config\ProfileLoader');
25+
}
26+
27+
function it_should_list_profile_names()
28+
{
29+
$this->getProfileNames()->shouldReturn(array(
30+
'one', 'two'
31+
));
32+
}
33+
34+
function it_should_load_data_into_a_given_profile(
35+
Profile $profile,
36+
Filesystem $filesystem
37+
)
38+
{
39+
$profile->getName()->willReturn('one');
40+
$profile->set('transport', array(
41+
'name' => 'foobar',
42+
'bar_foo' => 'barfoo',
43+
'foo_bar' => 'foobar',
44+
))->shouldBeCalled();
45+
$profile->set('phpcr', array(
46+
'username' => 'username',
47+
'password' => 'password',
48+
'workspace' => 'default',
49+
))->shouldBeCalled();
50+
51+
$this->loadProfile($profile);
52+
}
53+
54+
function it_should_save_a_given_profile(
55+
Profile $profile,
56+
Filesystem $filesystem
57+
)
58+
{
59+
$profile->getName()->willReturn('newprofile');
60+
$profile->toArray()->willReturn(array(
61+
'transport' => array(
62+
'name' => 'test_transport',
63+
'option1' => 'value1',
64+
),
65+
'phpcr' => array(
66+
'username' => 'daniel',
67+
'password' => 'leech',
68+
),
69+
));
70+
$filesystem->dumpFile(Argument::type('string'), <<<EOT
71+
transport:
72+
name: test_transport
73+
option1: value1
74+
phpcr:
75+
username: daniel
76+
password: leech
77+
78+
EOT
79+
, '0600')->shouldBeCalled();
80+
$this->saveProfile($profile);
81+
}
82+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
transport:
2+
name: foobar
3+
bar_foo: barfoo
4+
foo_bar: foobar
5+
phpcr:
6+
username: username
7+
password: password
8+
workspace: default
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
transport: daadoo
2+
dar_boo: darboo
3+
boo_dar: boodar
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Config\Exception;
4+
5+
class FileExistsException extends \Exception
6+
{
7+
}

src/PHPCR/Shell/Config/Profile.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ public function __construct($name = null)
2121
$this->name = $name;
2222
}
2323

24+
/**
25+
* Return the array data for this profile
26+
*
27+
* @return array
28+
*/
29+
public function toArray()
30+
{
31+
return $this->profile;
32+
}
33+
2434
protected function validateDomain($domain)
2535
{
2636
if (!array_key_exists($domain, $this->profile)) {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Config;
4+
5+
use Symfony\Component\Filesystem\Filesystem;
6+
use PHPCR\Shell\Console\Helper\ConfigHelper;
7+
use Symfony\Component\Finder\Finder;
8+
use Symfony\Component\Yaml\Yaml;
9+
use PHPCR\Shell\Config\Exception\FileExistsException;
10+
11+
class ProfileLoader
12+
{
13+
const DIR_PROFILE = 'profiles';
14+
15+
protected $config;
16+
protected $filesystem;
17+
18+
public function __construct(ConfigHelper $config, Filesystem $filesystem = null)
19+
{
20+
$this->config = $config;
21+
$this->filesystem = $filesystem ? : new Filesystem;
22+
}
23+
24+
protected function getProfileDir()
25+
{
26+
$dir = sprintf('%s/%s', $this->config->getConfigDir(), self::DIR_PROFILE);;
27+
28+
return $dir;
29+
}
30+
31+
public function getProfilePath($name)
32+
{
33+
$dir = sprintf('%s/%s/%s.yml', $this->config->getConfigDir(), self::DIR_PROFILE, $name);;
34+
35+
return $dir;
36+
}
37+
38+
public function getProfileNames()
39+
{
40+
$dir = $this->getProfileDir();
41+
42+
if (false === $this->filesystem->exists($dir)) {
43+
return array();
44+
}
45+
46+
$files = Finder::create()->files()->name('*.yml')->in($dir);
47+
48+
$profiles = array();
49+
foreach ($files as $file) {
50+
$profiles[] = substr($file->getBasename(), 0, -4);
51+
}
52+
53+
sort($profiles);
54+
55+
return $profiles;
56+
}
57+
58+
public function loadProfile(Profile $profile)
59+
{
60+
$path = $this->getProfilePath($profile->getName());
61+
62+
if (!file_exists($path)) {
63+
throw new \InvalidArgumentException(sprintf('Profile "%s" does not exist, expected to find it in "%s"',
64+
$profile->getName(), $path
65+
));
66+
}
67+
68+
$contents = file_get_contents($path);
69+
$data = Yaml::parse($contents);
70+
71+
if (isset($data['transport'])) {
72+
$profile->set('transport', $data['transport']);
73+
}
74+
75+
if (isset($data['phpcr'])) {
76+
$profile->set('phpcr', $data['phpcr']);
77+
}
78+
}
79+
80+
public function saveProfile(Profile $profile, $overwrite = false)
81+
{
82+
$profileDir = $this->getProfileDir();
83+
$path = $this->getProfilePath($profile->getName());
84+
85+
if (false === $overwrite && file_exists($path)) {
86+
throw new FileExistsException(sprintf(
87+
'Profile already exists at "%s"', $path
88+
));
89+
}
90+
91+
$yaml = Yaml::dump($profile->toArray());
92+
93+
$this->filesystem->dumpFile($path, $yaml, 0600);
94+
}
95+
}

src/PHPCR/Shell/Console/Application/ShellApplication.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PHPCR\Shell\Console\Command\Phpcr\PhpcrShellCommand;
3030
use PHPCR\Shell\Config\Profile;
3131
use PHPCR\Shell\Transport\TransportRegistry;
32+
use PHPCR\Shell\Config\ProfileLoader;
3233

3334
/**
3435
* Main application for PHPCRSH
@@ -61,6 +62,7 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
6162
$this->dispatcher = new EventDispatcher();
6263
$this->transportRegistry = new TransportRegistry();
6364
$this->registerTransports();
65+
$this->registerHelpers();
6466
$this->registerEventListeners();
6567
}
6668

@@ -105,7 +107,6 @@ public function init()
105107
return;
106108
}
107109

108-
$this->registerHelpers();
109110
$this->registerCommands();
110111

111112
$event = new ApplicationInitEvent($this);
@@ -224,6 +225,16 @@ private function registerCommands()
224225
private function registerEventListeners()
225226
{
226227
$this->dispatcher->addSubscriber(new Subscriber\ProfileFromSessionInputSubscriber());
228+
$this->dispatcher->addSubscriber(new Subscriber\ProfileWriterSubscriber(
229+
new ProfileLoader(
230+
$this->getHelperSet()->get('config')
231+
)
232+
));
233+
$this->dispatcher->addSubscriber(new Subscriber\ProfileLoaderSubscriber(
234+
new ProfileLoader(
235+
$this->getHelperSet()->get('config')
236+
)
237+
));
227238

228239
$this->dispatcher->addSubscriber(new Subscriber\ConfigInitSubscriber());
229240
$this->dispatcher->addSubscriber(new Subscriber\ExceptionSubscriber());

src/PHPCR/Shell/Console/Command/ShellCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function configure()
4848
new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output.'),
4949
new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output.'),
5050

51-
new InputOption('--transport', '-t', InputOption::VALUE_REQUIRED, 'Transport to use.', 'doctrine-dbal'),
51+
new InputOption('--transport', '-t', InputOption::VALUE_REQUIRED, 'Transport to use.'),
5252
new InputOption('--phpcr-username', '-pu', InputOption::VALUE_REQUIRED, 'PHPCR Username.', 'admin'),
5353
new InputOption('--phpcr-password', '-pp', InputOption::VALUE_OPTIONAL, 'PHPCR Password.', 'admin'),
5454
new InputOption('--phpcr-workspace','-pw', InputOption::VALUE_OPTIONAL, 'PHPCR Workspace.', 'default'),
@@ -61,6 +61,7 @@ public function configure()
6161
new InputOption('--no-interaction', null, InputOption::VALUE_NONE, 'Turn off interaction (for testing purposes)'),
6262
new InputOption('--repo-url', '-url', InputOption::VALUE_REQUIRED, 'URL of repository (e.g. for jackrabbit).', 'http://localhost:8080/server/'),
6363

64+
new InputOption('--profile', '-p', InputOption::VALUE_OPTIONAL, 'Speicfy a profile name, use wit <info>--transport</info> to update or create'),
6465
new InputOption('--unsupported', null, InputOption::VALUE_NONE, 'Show all commands, including commands not supported by the repository'),
6566
new InputOption('--command', null, InputOption::VALUE_REQUIRED, 'Run the given command'),
6667
));

src/PHPCR/Shell/Console/Helper/ProfileHelper.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)