Skip to content

Commit cb79d01

Browse files
authored
Merge pull request #100 from wagnert/master
Make minor customizations for appserver.io 1.1.15
2 parents c96dd32 + 9ddcfd5 commit cb79d01

File tree

8 files changed

+195
-173
lines changed

8 files changed

+195
-173
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
"jms/serializer": "~1.0",
1111
"doctrine/orm": "2.5.*",
1212
"appserver-io/routlt": "2.4.*",
13-
"appserver-io/console": "7.0.*"
13+
"appserver-io/console": "10.0.*",
14+
"appserver-io/provisioning": "1.0.*"
1415
},
1516
"require-dev": {
17+
"appserver-io/phar": "~1.0",
1618
"appserver-io/build" : "~2.0"
1719
},
1820
"config": {

src/META-INF/classes/AppserverIo/Apps/Example/Provisioning/PrepareDatabaseStep.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
* @author Tim Wagner <[email protected]>
1515
* @copyright 2015 TechDivision GmbH <[email protected]>
1616
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17-
* @link https://github.com/appserver-io/appserver
17+
* @link https://github.com/appserver-io-apps/example
1818
* @link http://www.appserver.io
1919
*/
2020

2121
namespace AppserverIo\Apps\Example\Provisioning;
2222

23+
use AppserverIo\Provisioning\Steps\AbstractStep;
2324
use AppserverIo\Psr\EnterpriseBeans\Annotations as EPB;
24-
use AppserverIo\Appserver\Provisioning\Steps\AbstractStep;
2525

2626
/**
2727
* An step implementation that creates a database, login credentials and dummy
@@ -30,22 +30,32 @@
3030
* @author Tim Wagner <[email protected]>
3131
* @copyright 2015 TechDivision GmbH <[email protected]>
3232
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33-
* @link https://github.com/appserver-io/appserver
33+
* @link https://github.com/appserver-io-apps/example
3434
* @link http://www.appserver.io
3535
*
3636
* @EPB\Inject(shared=false)
3737
*/
3838
class PrepareDatabaseStep extends AbstractStep
3939
{
40-
40+
4141
/**
4242
* The user processor instance (a SFB instance).
4343
*
44-
* @var \AppserverIo\Apps\Example\Services\SchemaProcessor
44+
* @var \AppserverIo\Apps\Example\Services\SchemaProcessorInterface
4545
* @EPB\EnterpriseBean(name="SchemaProcessor")
4646
*/
4747
protected $schemaProcessor;
4848

49+
/**
50+
* Returns the schema processor instance.
51+
*
52+
* @return \AppserverIo\Apps\Example\Services\SchemaProcessorInterface
53+
*/
54+
protected function getSchemaProcessor()
55+
{
56+
return $this->schemaProcessor;
57+
}
58+
4959
/**
5060
* Executes the functionality for this step, in this case the execution of
5161
* the PHP script defined in the step configuration.
@@ -58,15 +68,15 @@ public function execute()
5868
{
5969

6070
// log a message that provisioning starts
61-
\info('Now start to prepare database using SchemaProcessor!');
71+
\info(sprintf('Now start to prepare database using "%s"', get_class($this->getSchemaProcessor())));
6272

6373
// create schema, default products + login credentials
64-
$this->schemaProcessor->createDatabase();
65-
$this->schemaProcessor->createSchema();
66-
$this->schemaProcessor->createDefaultProducts();
67-
$this->schemaProcessor->createDefaultCredentials();
74+
$this->getSchemaProcessor()->createDatabase();
75+
$this->getSchemaProcessor()->createSchema();
76+
$this->getSchemaProcessor()->createDefaultProducts();
77+
$this->getSchemaProcessor()->createDefaultCredentials();
6878

6979
// log a message that provisioning has been successfull
70-
\info('Successfully prepared database using SchemaProcessor!');
80+
\info(sprintf('Successfully prepared database using "%s"', get_class($this->getSchemaProcessor())));
7181
}
7282
}

src/META-INF/classes/AppserverIo/Apps/Example/Services/AbstractPersistenceProcessor.php

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,8 @@ abstract class AbstractPersistenceProcessor extends AbstractProcessor
4747
*
4848
* @return \Doctrine\ORM\EntityManagerInterface The initialized Doctrine entity manager
4949
*/
50-
public function getEntityManager()
50+
protected function getEntityManager()
5151
{
5252
return $this->entityManager;
5353
}
54-
55-
/**
56-
* Close the entity manager's connection before destroying the bean.
57-
*
58-
* @return void
59-
* @EPB\PreDestroy
60-
*/
61-
public function preDestroy()
62-
{
63-
if ($entityManager = $this->getEntityManager()) {
64-
if ($connection = $entityManager->getConnection()) {
65-
$connection->close();
66-
}
67-
}
68-
}
69-
70-
/**
71-
* Close the entity manager's connection before re-attaching it to the container.
72-
*
73-
* @return void
74-
* @EPB\PreAttach
75-
*/
76-
public function preAttach()
77-
{
78-
if ($entityManager = $this->getEntityManager()) {
79-
if ($connection = $entityManager->getConnection()) {
80-
$connection->close();
81-
}
82-
}
83-
}
8454
}

src/META-INF/classes/AppserverIo/Apps/Example/Services/AbstractProcessor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function preAttach()
106106
*
107107
* @return \AppserverIo\Psr\Application\ApplicationInterface The application instance
108108
*/
109-
public function getApplication()
109+
protected function getApplication()
110110
{
111111
return $this->application;
112112
}
@@ -116,7 +116,7 @@ public function getApplication()
116116
*
117117
* @return \AppserverIo\Appserver\Core\InitialContext The initial context instance
118118
*/
119-
public function getInitialContext()
119+
protected function getInitialContext()
120120
{
121121
return $this->getApplication()->getInitialContext();
122122
}
@@ -126,7 +126,7 @@ public function getInitialContext()
126126
*
127127
* @return \AppserverIo\Configuration\Interfaces\NodeInterface The system configuration
128128
*/
129-
public function getSystemConfiguration()
129+
protected function getSystemConfiguration()
130130
{
131131
return $this->getInitialContext()->getSystemConfiguration();
132132
}
@@ -136,7 +136,7 @@ public function getSystemConfiguration()
136136
*
137137
* @return array The array with the datasources
138138
*/
139-
public function getDatasources()
139+
protected function getDatasources()
140140
{
141141
return $this->getSystemConfiguration()->getDatasources();
142142
}

src/META-INF/classes/AppserverIo/Apps/Example/Services/SchemaProcessor.php

Lines changed: 52 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@
2020

2121
namespace AppserverIo\Apps\Example\Services;
2222

23-
use Doctrine\DBAL\DriverManager;
24-
use Doctrine\ORM\Tools\SchemaTool;
25-
use Doctrine\DBAL\Schema\SqliteSchemaManager;
2623
use Doctrine\Common\Collections\ArrayCollection;
2724
use AppserverIo\Apps\Example\Entities\Impl\Product;
2825
use AppserverIo\Psr\EnterpriseBeans\Annotations as EPB;
26+
use AppserverIo\Console\Server\Services\SchemaProcessorTrait;
2927

3028
/**
3129
* A singleton session bean implementation that handles the
@@ -43,27 +41,43 @@ class SchemaProcessor extends AbstractPersistenceProcessor implements SchemaProc
4341
{
4442

4543
/**
46-
* The name of the configuration key that contains the database name.
44+
* Trait that provides schema managing functionality.
4745
*
48-
* @var string
46+
* @var AppserverIo\Console\Server\Services\SchemaProcessorTrait
4947
*/
50-
const PARAM_DBNAME = 'dbname';
51-
48+
use SchemaProcessorTrait;
49+
50+
/**
51+
* The application instance.
52+
*
53+
* @var \AppserverIo\Psr\Application\ApplicationInterface
54+
* @EPB\Resource(type="ApplicationInterface")
55+
*/
56+
protected $application;
57+
5258
/**
53-
* The DIC provider instance.
59+
* The DI provider instance.
5460
*
55-
* @var \AppserverIo\Psr\Di\ProviderInterface $provider
61+
* @var \AppserverIo\Psr\Di\ProviderInterface
5662
* @EPB\Resource(type="ProviderInterface")
5763
*/
58-
protected $providerInterface;
59-
64+
protected $provider;
65+
66+
/**
67+
* The timer service context instance.
68+
*
69+
* @var \AppserverIo\Psr\EnterpriseBeans\TimerServiceContextInterface
70+
* @EPB\Resource(type="TimerServiceContextInterface")
71+
*/
72+
protected $timerServiceContext;
73+
6074
/**
61-
* The system logger implementation.
75+
* The object manager instance.
6276
*
63-
* @var \AppserverIo\Logger\Logger
64-
* @EPB\Resource(type="SystemLogger")
77+
* @var \AppserverIo\Psr\Di\ObjectManagerInterface
78+
* @EPB\Resource(type="ObjectManagerInterface")
6579
*/
66-
protected $systemLogger;
80+
protected $objectManager;
6781

6882
/**
6983
* A list with default credentials for login testing.
@@ -83,95 +97,45 @@ class SchemaProcessor extends AbstractPersistenceProcessor implements SchemaProc
8397
array('appserver_09', 'appserver.i0', array('Customer')),
8498
array('guest', 'appserver.i0', array('Guest'))
8599
);
86-
100+
87101
/**
88-
* Example method that should be invoked after constructor.
102+
* Returns the application instance.
89103
*
90-
* @return void
91-
* @EPB\PostConstruct
104+
* @return \AppserverIo\Psr\Application\ApplicationInterface The initialized Doctrine entity manager
92105
*/
93-
public function initialize()
106+
protected function getApplication()
94107
{
95-
\info(
96-
sprintf('%s has successfully been invoked by @PostConstruct annotation', __METHOD__)
97-
);
108+
return $this->application;
98109
}
99-
110+
100111
/**
101-
* Return's the system logger instance.
112+
* Returns the DI provider instance.
102113
*
103-
* @return \AppserverIo\Logger\Logger The sytsem logger instance
114+
* @return \AppserverIo\Psr\Di\ProviderInterface The DI provider instance
104115
*/
105-
public function getSystemLogger()
116+
protected function getProvider()
106117
{
107-
return $this->systemLogger;
118+
return $this->provider;
108119
}
109-
120+
110121
/**
111-
* Create's the database itself.
122+
* Returns the object manager instance.
112123
*
113-
* This quite seems to be a bit strange, because with all databases
114-
* other than SQLite, we need to remove the database name from the
115-
* connection parameters BEFORE connecting to the database, as
116-
* connection to a not existing database fails.
117-
*
118-
* @return void
124+
* @return \AppserverIo\Psr\Di\ObjectManagerInterface The object manager instance
119125
*/
120-
public function createDatabase()
126+
protected function getObjectManager()
121127
{
122-
123-
try {
124-
// clone the connection and load the database name
125-
$connection = clone $this->getEntityManager()->getConnection();
126-
$dbname = $connection->getDatabase();
127-
128-
// remove the the database name
129-
$params = $connection->getParams();
130-
if (isset($params[SchemaProcessor::PARAM_DBNAME])) {
131-
unset($params[SchemaProcessor::PARAM_DBNAME]);
132-
}
133-
134-
// create a new connection WITHOUT the database name
135-
$cn = DriverManager::getConnection($params);
136-
$sm = $cn->getDriver()->getSchemaManager($cn);
137-
138-
// SQLite doesn't support database creation by a method
139-
if ($sm instanceof SqliteSchemaManager) {
140-
return;
141-
}
142-
143-
// query whether or not the database already exists
144-
if (!in_array($dbname, $sm->listDatabases())) {
145-
$sm->createDatabase($dbname);
146-
}
147-
} catch (\Exception $e) {
148-
\error($e->__toString());
149-
}
128+
return $this->objectManager;
150129
}
151-
130+
152131
/**
153-
* Deletes the database schema and creates it new.
154-
*
155-
* Attention: All data will be lost if this method has been invoked.
132+
* Returns the timer service context instance.
156133
*
157-
* @return void
134+
* @return \AppserverIo\Psr\EnterpriseBeans\TimerServiceContextInterface The timer service context instance
158135
*/
159-
public function createSchema()
136+
protected function getTimerServiceContext()
160137
{
161-
162-
try {
163-
// load the entity manager and the schema tool
164-
$entityManager = $this->getEntityManager();
165-
$schemaTool = new SchemaTool($entityManager);
166-
167-
// load the class definitions
168-
$classes = $entityManager->getMetadataFactory()->getAllMetadata();
169-
170-
// create or update the schema
171-
$schemaTool->updateSchema($classes);
172-
} catch (\Exception $e) {
173-
\error($e->__toString());
174-
}
138+
return $this->timerServiceContext;
175139
}
176140

177141
/**
@@ -196,7 +160,7 @@ public function createDefaultProducts()
196160
}
197161

198162
// set user data and save it
199-
$product = $this->providerInterface->newInstance($className);
163+
$product = $this->getProvider()->newInstance($className);
200164
$product->setName("Product-$i");
201165
$product->setStatus(Product::STATUS_ACTIVE);
202166
$product->setUrlKey("product-$i");
@@ -237,7 +201,7 @@ public function createDefaultCredentials()
237201
}
238202

239203
// set user data and save it
240-
$user = $this->providerInterface->newInstance($className);
204+
$user = $this->getProvider()->newInstance($className);
241205
$user->setEmail(sprintf('%[email protected]', $username));
242206
$user->setUsername($username);
243207
$user->setUserLocale('en_US');
@@ -253,7 +217,7 @@ public function createDefaultCredentials()
253217

254218
// create the user's roles
255219
foreach ($roleNames as $roleName) {
256-
$role = $this->providerInterface->newInstance('\AppserverIo\Apps\Example\Entities\Impl\Role');
220+
$role = $this->getProvider()->newInstance('\AppserverIo\Apps\Example\Entities\Impl\Role');
257221
$role->setUser($user);
258222
$role->setName($roleName);
259223
$roles->add($role);

0 commit comments

Comments
 (0)