diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index 43707c4..948b140 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -6,6 +6,10 @@ class Home extends BaseController { public function index() { - return $this->response->redirect('/sample'); + return view('sample/sample', [ + 'title' => 'ci4Sample', + 'location' => 'home', + 'menu' => '', + ]); } } diff --git a/app/Controllers/Refactoring/Basic/Exam1.php b/app/Controllers/Refactoring/Basic/Exam1.php new file mode 100644 index 0000000..15a6dd3 --- /dev/null +++ b/app/Controllers/Refactoring/Basic/Exam1.php @@ -0,0 +1,53 @@ +addDays(30); + + echo "고객명 : {$invoice['customer']}\n"; + echo "채무액 : {$outstanding}\n"; + echo "마감일 : {$invoice['dueDate']}\n"; + } + + public function index() + { + $invoice = [ + 'customer' => 'customer', + 'orders' => [ + ['amount' => 100], + ['amount' => 110], + ['amount' => 120], + ['amount' => 130], + ['amount' => 140], + ], + 'dueDate' => '', + ]; + + echo '
'; + $this->printOwing($invoice); + echo ''; + } + +} \ No newline at end of file diff --git a/app/Controllers/Refactoring/Basic/Exam2.php b/app/Controllers/Refactoring/Basic/Exam2.php new file mode 100644 index 0000000..6e92da7 --- /dev/null +++ b/app/Controllers/Refactoring/Basic/Exam2.php @@ -0,0 +1,65 @@ +moreThanFiveLateDeliveries($aDriver) ? 2 : 1); + } + + protected function moreThanFiveLateDeliveries($aDriver) + { + return $aDriver['numberOfLateDeliveries'] > 5; + } + + protected function rating2($aDriver) + { + return ($this->lessThanFiveLateDeliveries($aDriver) ? 2 : 1); + } + + protected function lessThanFiveLateDeliveries($drv) + { + return $drv['numberOfLateDeliveries'] < 5; + } + + protected function reportLines($aCustomer) + { + $lines = []; + $lines = $this->getherCustomerData($lines, $aCustomer); + return $lines; + } + + protected function getherCustomerData($out, $aCustomer) + { + $out['name'] = $aCustomer['name']; + $out['location'] = $aCustomer['location']; + + return $out; + } + + public function index() + { + $aDriver = [ + 'numberOfLateDeliveries' => 3 + ]; + + $aCustomer = [ + 'name' => 'Name', + 'location' => 'Location', + ]; + + echo '
';
+ echo "rating1 = {$this->rating1($aDriver)}\n";
+ echo "rating2 = {$this->rating2($aDriver)}\n";
+ echo "reportLines = " . print_r($this->reportLines($aCustomer), true);
+ echo '';
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Refactoring/Basic/Exam3.php b/app/Controllers/Refactoring/Basic/Exam3.php
new file mode 100644
index 0000000..45f57f2
--- /dev/null
+++ b/app/Controllers/Refactoring/Basic/Exam3.php
@@ -0,0 +1,34 @@
+ 5,
+ 'itemPrice' => 1000
+ ];
+
+ echo '';
+ echo "price = {$this->price($order)}";
+ echo '';
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Refactoring/Basic/Exam4.php b/app/Controllers/Refactoring/Basic/Exam4.php
new file mode 100644
index 0000000..86cca75
--- /dev/null
+++ b/app/Controllers/Refactoring/Basic/Exam4.php
@@ -0,0 +1,26 @@
+ 'mr', 'lastName' => 'he'];
+
+ public function index()
+ {
+ $spaceship['owner'] = $this->defaultOwner;
+ $this->defaultOwner = ['firstName' => 'mrs', 'lastName' => 'her'];
+
+ echo ''; + echo 'spaceship = ' . print_r($spaceship, true) . "\n"; + echo 'defaultOwner = ' . print_r($this->defaultOwner, true); + echo ''; + } +} \ No newline at end of file diff --git a/app/Controllers/Refactoring/Basic/Exam5.php b/app/Controllers/Refactoring/Basic/Exam5.php new file mode 100644 index 0000000..f2810db --- /dev/null +++ b/app/Controllers/Refactoring/Basic/Exam5.php @@ -0,0 +1,39 @@ + $max); + }); + + } + + public function index() + { + $station = [ + 'name' => 'ZB!', + 'readings' => [ + ['temp' => 47, 'time' => '2016-11-10 09:10'], + ['temp' => 53, 'time' => '2016-11-10 09:20'], + ['temp' => 58, 'time' => '2016-11-10 09:30'], + ['temp' => 53, 'time' => '2016-11-10 09:40'], + ['temp' => 51, 'time' => '2016-11-10 09:50'], + ], + ]; + + echo '
'; + print_r($this->readingsOutsideRange($station, 50, 55)); + echo ''; + + } +} \ No newline at end of file diff --git a/app/Controllers/Refactoring/Basic/Exam6.php b/app/Controllers/Refactoring/Basic/Exam6.php new file mode 100644 index 0000000..e63f608 --- /dev/null +++ b/app/Controllers/Refactoring/Basic/Exam6.php @@ -0,0 +1,73 @@ + 'ivan', + 'quantity' => 10, + 'month' => 5, + 'year' => 2017, + ]; + + return $reading; +} + +function taxThreshold($year) +{ + return $year > 2015 ? 0 : 0.5; +} + +function baseRate($month, $year) +{ + return $year / $month; +} + +function calculateBaseCharge($aReading) +{ + return baseRate($aReading['month'], $aReading['year']) * $aReading['quantity']; +} + +/** + * Description of Exam6 + * + * @author hoksi + */ +class Exam6 extends BaseController +{ + protected function client1() + { + $aReading = acquireReading(); + $baseCharge = baseRate($aReading['month'], $aReading['year']) * $aReading['quantity']; + + return $baseCharge; + } + + protected function client2() + { + $aReading = acquireReading(); + $base = (baseRate($aReading['month'], $aReading['year']) * $aReading['quantity']); + $taxableCharge = max(0, $base - taxThreshold($aReading['year'])); + + return $taxableCharge; + } + + protected function client3() + { + $aReading = acquireReading(); + $baseCharge = calculateBaseCharge($aReading); + + return $baseCharge; + } + + public function index() + { + echo '
';
+ echo "client1 : {$this->client1()}\n";
+ echo "client2 : {$this->client2()}\n";
+ echo "client3 : {$this->client3()}\n";
+ echo '';
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Refactoring/Basic/Exam7.php b/app/Controllers/Refactoring/Basic/Exam7.php
new file mode 100644
index 0000000..c2a78bb
--- /dev/null
+++ b/app/Controllers/Refactoring/Basic/Exam7.php
@@ -0,0 +1,73 @@
+ 'ivan',
+ 'quantity' => 10,
+ 'month' => 5,
+ 'year' => 2017,
+ ];
+
+ return $reading;
+}
+
+function taxThreshold($year)
+{
+ return $year > 2015 ? 0 : 0.5;
+}
+
+function baseRate($month, $year)
+{
+ return $year / $month;
+}
+
+function calculateBaseCharge($aReading)
+{
+ return baseRate($aReading['month'], $aReading['year']) * $aReading['quantity'];
+}
+
+/**
+ * Description of Exam7
+ *
+ * @author hoksi
+ */
+class Exam7 extends BaseController
+{
+ protected function client1()
+ {
+ $aReading = acquireReading();
+ $baseCharge = baseRate($aReading['month'], $aReading['year']) * $aReading['quantity'];
+
+ return $baseCharge;
+ }
+
+ protected function client2()
+ {
+ $aReading = acquireReading();
+ $base = (baseRate($aReading['month'], $aReading['year']) * $aReading['quantity']);
+ $taxableCharge = max(0, $base - taxThreshold($aReading['year']));
+
+ return $taxableCharge;
+ }
+
+ protected function client3()
+ {
+ $aReading = acquireReading();
+ $baseCharge = calculateBaseCharge($aReading);
+
+ return $baseCharge;
+ }
+
+ public function index()
+ {
+ echo '';
+ echo "client1 : {$this->client1()}\n";
+ echo "client2 : {$this->client2()}\n";
+ echo "client3 : {$this->client3()}\n";
+ echo '';
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Refactoring/Basic/Exam8.php b/app/Controllers/Refactoring/Basic/Exam8.php
new file mode 100644
index 0000000..dd46978
--- /dev/null
+++ b/app/Controllers/Refactoring/Basic/Exam8.php
@@ -0,0 +1,33 @@
+ $shippingMethod['discountThreshold']) ? $shippingMethod['discountedFee'] : $shippingMethod['feePerCase'];
+ $shippingCost = $quantity * $shippingPerCase;
+ $price = $basePrice - $discount + $shippingCost;
+
+ return $price;
+}
+
+/**
+ * Description of Exam8
+ *
+ * @author hoksi
+ */
+class Exam8 extends BaseController
+{
+ public function index()
+ {
+ $product = ['basePrice' => 10000, 'discountThreshold' => 8000, 'discountRate' => 0.1];
+ $shippingMethod = ['discountThreshold' => 8000, 'discountedFee' => 500, 'feePerCase' => 3500];
+
+ echo ''; + echo "priceOrder : " . priceOrder($product, 5, $shippingMethod) . "\n"; + echo ''; + } +} \ No newline at end of file diff --git a/app/Controllers/Refactoring/Basic/Home.php b/app/Controllers/Refactoring/Basic/Home.php new file mode 100644 index 0000000..6504dd9 --- /dev/null +++ b/app/Controllers/Refactoring/Basic/Home.php @@ -0,0 +1,23 @@ + '기본적인 리팩터링', + 'location' => 'refactoring', + 'menu' => 'basic', + ]; + + public function index() + { + return view('refactoring/basic/home', $this->menuInfo); + } +} \ No newline at end of file diff --git a/app/Controllers/Refactoring/Basic/Refactoring/Exam1.php b/app/Controllers/Refactoring/Basic/Refactoring/Exam1.php new file mode 100644 index 0000000..6661daa --- /dev/null +++ b/app/Controllers/Refactoring/Basic/Refactoring/Exam1.php @@ -0,0 +1,73 @@ +printBanner(); + $outstanding = $this->calculateOutstanding($invoice); + $invoice = $this->recordDueData($invoice); + $this->printDetail($invoice, $outstanding); + } + + protected function printBanner() + { + echo "*****************\n"; + echo "*** 고객 채무 ***\n"; + echo "*****************\n"; + } + + protected function printDetail($invoice, $outstanding) + { + echo "고객명 : {$invoice['customer']}\n"; + echo "채무액 : {$outstanding}\n"; + echo "마감일 : {$invoice['dueDate']}\n"; + } + + protected function recordDueData($invoice) + { + $time = Time::now(); + $invoice['dueDate'] = $time->addDays(30); + + return $invoice; + } + + protected function calculateOutstanding($invoice) + { + $outstanding = 0; + foreach($invoice['orders'] as $o) { + $outstanding += $o['amount']; + } + + return $outstanding; + } + + public function index() + { + $invoice = [ + 'customer' => 'customer', + 'orders' => [ + ['amount' => 100], + ['amount' => 110], + ['amount' => 120], + ['amount' => 130], + ['amount' => 140], + ], + 'dueDate' => '', + ]; + + echo '
'; + $this->printOwing($invoice); + echo ''; + } + +} \ No newline at end of file diff --git a/app/Controllers/Refactoring/Basic/Refactoring/Exam2.php b/app/Controllers/Refactoring/Basic/Refactoring/Exam2.php new file mode 100644 index 0000000..93be0d5 --- /dev/null +++ b/app/Controllers/Refactoring/Basic/Refactoring/Exam2.php @@ -0,0 +1,50 @@ + 5 ? 2 : 1); + } + + protected function rating2($aDriver) + { + // 인라인시 $drv를 $aDriver로 리펙토링 + return ($aDriver['numberOfLateDeliveries'] < 5 ? 2 : 1); + } + + protected function reportLines($aCustomer) + { + $lines = []; + // 인라인시 $out을 $lines로 리펙토링 + $lines['name'] = $aCustomer['name']; + $lines['location'] = $aCustomer['location']; + return $lines; + } + + public function index() + { + $aDriver = [ + 'numberOfLateDeliveries' => 3 + ]; + + $aCustomer = [ + 'name' => 'Name', + 'location' => 'Location', + ]; + + echo '
';
+ echo "rating1 = {$this->rating1($aDriver)}\n";
+ echo "rating2 = {$this->rating2($aDriver)}\n";
+ echo "reportLines = " . print_r($this->reportLines($aCustomer), true);
+ echo '';
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Refactoring/Basic/Refactoring/Exam3.php b/app/Controllers/Refactoring/Basic/Refactoring/Exam3.php
new file mode 100644
index 0000000..49a009d
--- /dev/null
+++ b/app/Controllers/Refactoring/Basic/Refactoring/Exam3.php
@@ -0,0 +1,34 @@
+ 5,
+ 'itemPrice' => 1000
+ ];
+
+ echo '';
+ echo "price = {$this->price($order)}";
+ echo '';
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Refactoring/Basic/Refactoring/Exam4.php b/app/Controllers/Refactoring/Basic/Refactoring/Exam4.php
new file mode 100644
index 0000000..89593e1
--- /dev/null
+++ b/app/Controllers/Refactoring/Basic/Refactoring/Exam4.php
@@ -0,0 +1,37 @@
+ 'mr', 'lastName' => 'he'];
+
+ public function getDefaultOwner()
+ {
+ return $this->defaultOwner;
+ }
+
+ public function setDefaultOwner($arg)
+ {
+ $this->defaultOwner = $arg;
+ return $this;
+ }
+
+ public function index()
+ {
+ $spaceship['owner'] = $this->getDefaultOwner();
+ $this->setDefaultOwner(['firstName' => 'mrs', 'lastName' => 'her']);
+
+ echo ''; + echo 'spaceship = ' . print_r($spaceship, true) . "\n"; + echo 'defaultOwner = ' . print_r($this->getDefaultOwner(), true); + echo ''; + } +} \ No newline at end of file diff --git a/app/Controllers/Refactoring/Basic/Refactoring/Exam5.php b/app/Controllers/Refactoring/Basic/Refactoring/Exam5.php new file mode 100644 index 0000000..a9e1d7a --- /dev/null +++ b/app/Controllers/Refactoring/Basic/Refactoring/Exam5.php @@ -0,0 +1,60 @@ +getMin() || $r['temp'] > $range->getMax()); + }); + + } + + public function index() + { + $range = new NumberRange(50, 55); + + $station = [ + 'name' => 'ZB!', + 'readings' => [ + ['temp' => 47, 'time' => '2016-11-10 09:10'], + ['temp' => 53, 'time' => '2016-11-10 09:20'], + ['temp' => 58, 'time' => '2016-11-10 09:30'], + ['temp' => 53, 'time' => '2016-11-10 09:40'], + ['temp' => 51, 'time' => '2016-11-10 09:50'], + ], + ]; + + echo '
'; + print_r($this->readingsOutsideRange($station, $range)); + echo ''; + + } +} + +class NumberRange { + protected $data; + + public function __construct($min, $max) + { + $this->data = ['min' => $min, 'max' => $max]; + } + + public function getMin() + { + return $this->data['min']; + } + + public function getMax() + { + return $this->data['max']; + } +} \ No newline at end of file diff --git a/app/Controllers/Refactoring/Basic/Refactoring/Exam6.php b/app/Controllers/Refactoring/Basic/Refactoring/Exam6.php new file mode 100644 index 0000000..a40fa46 --- /dev/null +++ b/app/Controllers/Refactoring/Basic/Refactoring/Exam6.php @@ -0,0 +1,116 @@ + 'ivan', + 'quantity' => 10, + 'month' => 5, + 'year' => 2017, + ]; + + return $reading; +} + +function taxThreshold($year) +{ + return $year > 2015 ? 0 : 0.5; +} + +/** + * Description of Exam6 + * + * @author hoksi + */ +class Exam6 extends BaseController +{ + protected function client1() + { + $rawReading = acquireReading(); + $aReading = new Reading($rawReading); + $baseCharge = $aReading->getBaseCharge(); + + return $baseCharge; + } + + protected function client2() + { + $rawReading = acquireReading(); + $aReading = new Reading($rawReading); + $taxableCharge = $aReading->taxableChargeFn(); + + return $taxableCharge; + } + + protected function client3() + { + $rawReading = acquireReading(); + $aReading = new Reading($rawReading); + $baseCharge = $aReading->getBaseCharge(); + + return $baseCharge; + } + + public function index() + { + echo '
';
+ echo "client1 : {$this->client1()}\n";
+ echo "client2 : {$this->client2()}\n";
+ echo "client3 : {$this->client3()}\n";
+ echo '';
+ }
+}
+
+class Reading {
+ protected $customer;
+ protected $quantity;
+ protected $month;
+ protected $year;
+
+ public function __construct($data)
+ {
+ $this->customer = $data['customer'];
+ $this->quantity = $data['quantity'];
+ $this->month = $data['month'];
+ $this->year = $data['year'];
+ }
+
+ public function getCustomer()
+ {
+ return $this->customer;
+ }
+
+ public function getQuantity()
+ {
+ return $this->quantity;
+ }
+
+ public function getMonth()
+ {
+ return $this->month;
+ }
+
+ public function getYear()
+ {
+ return $this->year;
+ }
+
+ public function baseRate($month, $year)
+ {
+ return $year / $month;
+ }
+
+ public function getBaseCharge()
+ {
+ return $this->baseRate($this->month, $this->year) * $this->quantity;
+ }
+
+ public function taxableChargeFn()
+ {
+ return max(0, $this->getBaseCharge() - taxThreshold($this->getYear()));
+ }
+
+}
\ No newline at end of file
diff --git a/app/Controllers/Refactoring/Basic/Refactoring/Exam7.php b/app/Controllers/Refactoring/Basic/Refactoring/Exam7.php
new file mode 100644
index 0000000..77750e3
--- /dev/null
+++ b/app/Controllers/Refactoring/Basic/Refactoring/Exam7.php
@@ -0,0 +1,83 @@
+ 'ivan',
+ 'quantity' => 10,
+ 'month' => 5,
+ 'year' => 2017,
+ ];
+
+ return $reading;
+}
+
+function taxThreshold($year)
+{
+ return $year > 2015 ? 0 : 0.5;
+}
+
+function baseRate($month, $year)
+{
+ return $year / $month;
+}
+
+function calculateBaseCharge($aReading)
+{
+ return baseRate($aReading['month'], $aReading['year']) * $aReading['quantity'];
+}
+
+function enrichReading($original)
+{
+ $result = $original;
+ $result['baseChage'] = calculateBaseCharge($result);
+ $result['taxableCharge'] = max(0, $result['baseChage'] - taxThreshold($result['year']));
+ return $result;
+}
+
+/**
+ * Description of Exam7
+ *
+ * @author hoksi
+ */
+class Exam7 extends BaseController
+{
+ protected function client1()
+ {
+ $rawReading = acquireReading();
+ $aReading = enrichReading($rawReading);
+ $baseCharge = $aReading['baseChage'];
+
+ return $baseCharge;
+ }
+
+ protected function client2()
+ {
+ $rawReading = acquireReading();
+ $aReading = enrichReading($rawReading);
+ $taxableCharge =$aReading['taxableCharge'];
+
+ return $taxableCharge;
+ }
+
+ protected function client3()
+ {
+ $rawReading = acquireReading();
+ $aReading = enrichReading($rawReading);
+ $baseCharge = $aReading['baseChage'];
+
+ return $baseCharge;
+ }
+
+ public function index()
+ {
+ echo '';
+ echo "client1 : {$this->client1()}\n";
+ echo "client2 : {$this->client2()}\n";
+ echo "client3 : {$this->client3()}\n";
+ echo '';
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Refactoring/Basic/Refactoring/Exam8.php b/app/Controllers/Refactoring/Basic/Refactoring/Exam8.php
new file mode 100644
index 0000000..eb4d26b
--- /dev/null
+++ b/app/Controllers/Refactoring/Basic/Refactoring/Exam8.php
@@ -0,0 +1,43 @@
+ $basePrice, 'quantity' => $quantity, 'discount' => $discount];
+}
+
+function applyShipping($priceData, $shippingMethod)
+{
+ $shippingPerCase = ($priceData['basePrice'] > $shippingMethod['discountThreshold']) ? $shippingMethod['discountedFee'] : $shippingMethod['feePerCase'];
+ $shippingCost = $priceData['quantity'] * $shippingPerCase;
+ return $priceData['basePrice'] - $priceData['discount'] + $shippingCost;
+}
+
+/**
+ * Description of Exam8
+ *
+ * @author hoksi
+ */
+class Exam8 extends BaseController
+{
+ public function index()
+ {
+ $product = ['basePrice' => 10000, 'discountThreshold' => 8000, 'discountRate' => 0.1];
+ $shippingMethod = ['discountThreshold' => 8000, 'discountedFee' => 500, 'feePerCase' => 3500];
+
+ echo ''; + echo "priceOrder : " . priceOrder($product, 5, $shippingMethod) . "\n"; + echo ''; + } +} \ No newline at end of file diff --git a/app/Controllers/Refactoring/Capsulation/Exam1.php b/app/Controllers/Refactoring/Capsulation/Exam1.php new file mode 100644 index 0000000..e44dd09 --- /dev/null +++ b/app/Controllers/Refactoring/Capsulation/Exam1.php @@ -0,0 +1,24 @@ + '홍길동', 'country' => 'KR']; + $result = "[h1]{$organization['name']}[/h1]"; + $organization['name'] = '김삿갓'; + + echo '
';
+ echo "result : {$result}\n";
+ echo "organization : " . print_r($organization, true) . "\n";
+ echo '';
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Refactoring/Capsulation/Home.php b/app/Controllers/Refactoring/Capsulation/Home.php
new file mode 100644
index 0000000..320329e
--- /dev/null
+++ b/app/Controllers/Refactoring/Capsulation/Home.php
@@ -0,0 +1,23 @@
+ '캡슐화',
+ 'location' => 'refactoring',
+ 'menu' => 'capsulation',
+ ];
+
+ public function index()
+ {
+ return view('refactoring/capsulation/home', $this->menuInfo);
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Refactoring/Capsulation/Refactoring/Exam1.php b/app/Controllers/Refactoring/Capsulation/Refactoring/Exam1.php
new file mode 100644
index 0000000..7a61b1e
--- /dev/null
+++ b/app/Controllers/Refactoring/Capsulation/Refactoring/Exam1.php
@@ -0,0 +1,29 @@
+ '홍길동', 'country' => 'KR'];
+}
+
+/**
+ * Description of Exam1
+ *
+ * @author hoksi
+ */
+class Exam1 extends BaseController
+{
+ public function index()
+ {
+ $organization = getRawDataOfOrganization();
+ $result = "[h1]{$organization['name']}[/h1]";
+ $organization['name'] = '김삿갓';
+
+ echo '';
+ echo "result : {$result}\n";
+ echo "organization : " . print_r($organization, true) . "\n";
+ echo '';
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Response/Parser/Exam4.php b/app/Controllers/Response/Parser/Exam4.php
new file mode 100644
index 0000000..725fbcd
--- /dev/null
+++ b/app/Controllers/Response/Parser/Exam4.php
@@ -0,0 +1,31 @@
+ 'My Blog Title',
+ 'blog_heading' => 'My Blog Heading',
+ 'blog_entries' => [
+ ['title' => 'Title 1', 'body' => 'Body 1'],
+ ['title' => 'Title 2', 'body' => 'Body 2'],
+ ['title' => 'Title 3', 'body' => 'Body 3'],
+ ['title' => 'Title 4', 'body' => 'Body 4'],
+ ['title' => 'Title 5', 'body' => 'Body 5'],
+ ],
+ ];
+
+ return parser('response/parser/exam4', $data);
+ }
+}
\ No newline at end of file
diff --git a/app/Controllers/Sample/Home.php b/app/Controllers/Sample/Home.php
index f4d4414..f2b3b90 100644
--- a/app/Controllers/Sample/Home.php
+++ b/app/Controllers/Sample/Home.php
@@ -14,11 +14,7 @@ class Home extends BaseController
public function index()
{
- return view('sample/sample', [
- 'title' => 'ci4Sample',
- 'location' => 'home',
- 'menu' => '',
- ]);
+ return $this->response->redirect('/');
}
public function view(...$params)
diff --git a/app/Helpers/parser_helper.php b/app/Helpers/parser_helper.php
new file mode 100644
index 0000000..b2be09b
--- /dev/null
+++ b/app/Helpers/parser_helper.php
@@ -0,0 +1,15 @@
+setData($data)
+ ->renderString(\Config\Services::renderer()->setData($data, 'raw')->render($name, $options, $saveData), $options, $saveData);
+}
diff --git a/app/Views/refactoring/basic/home.php b/app/Views/refactoring/basic/home.php
new file mode 100644
index 0000000..f4c0cca
--- /dev/null
+++ b/app/Views/refactoring/basic/home.php
@@ -0,0 +1,415 @@
+= $this->extend('sample/layout') ?>
+
+= $this->section('content') ?>
+
+ 코드 조각을 찾아 무슨일을 하는지 파악한 다음, 독립된 함수로 추출하고 목적에 맞는 이름을 붙인다.
+ 코드가 무슨 일을 하는지 파악하는 데 한참이 걸리다면 그 부분을 함수로 추출한 뒤 `무슨 일`에 걸맞는 이름을 짓는다.
+
+ `함수 추출하기`에서는 코드가 명료해지고 이해하기 쉽도록 코드의 일부를 목적이 분명히 들어나는 이름의 함수로 사용하기를 권장한다.
+ 하지만 때로는 함수 본문이 이름만큼 명확하거나 이름만큼 깔끔하게 리펙터링될 때도 있다. 이럴때는 함수를 과감히 제거하고 인라인한다.
+
+ 표현식이 너무 복잡하여 이해하기 어려울 때 지역 변수를 활용하여 표현식을 쪼개 관리하기 쉽게 만듭니다.
+ 추가한 변수는 복잡한 로직을 쉽게 만들기도 하지만 디버깅에도 도움이 됩니다.
+
+ 변수 이름이 표현식과 다를 바 없다면 그 변수는 인라인하는 것이 좋습니다. +
+ +function moreThan100($anOrder)
+{
+ $basePrice = $anOrder['basePrice'];
+ return ($basePrice > 100);
+}
+ |
+ => | +
+function moreThan100($anOrder)
+{
+ return ($anOrder['basePrice'] > 100);
+
+}
+ |
+
+ 함수 이름이 좋으면 함수 호출문만 보고도 무슨 일을 하는지 파악할 수 있습니다.
+ 잘못된 함수 이름을 발견하고 그보다 더 이해하기 쉬운 이름이 떠오르면 즉시 바꿉니다.
+
+function circum($radius)
+{
+ return 2 * pi() * $radius;
+}
+ |
+ => | +
+function circumference($radius)
+{
+ return 2 * pi() * $radius;
+}
+ |
+
+ 유효 범위가 넓은 전역 변수는 함수로 캡슐화 하십시오.
+ 전역 변수 접근을 함수로 캡슐화하여 독점하면 데이터의 변경 및 사용하는 코드를 감시할 수 있으며, 데이터 변경 전 검증이나 변경 후 추가 로직을 쉽게 넣을 수 있습니다.
+
+ 함수 이름과 마찬가지로 변수 이름도 많은 것을 설명해줍니다.
+ 개발을 진행하다 잘못 지어진 변수 이름을 발견한다면 그 목적에 맞는 이름으로 변수명을 바꿔줍니다.
+
+ 함수의 데이터 항목이 여러개인 경우 데이터 구조 하나로 모아줍니다.
+ 데이터 구조로 묶으면 데이터 사이의 관계가 명확해지고 매개변수 수가 줄어듭니다.
+
+ 공통의 데이터를 중심으로 긴밀하게 엮여 작동하는 함수들을 클래스 하나로 묶어줍니다.
+ 클래스로 묶으면 이 함수들이 공유하는 공통 환경을 더 명확하게 표현할 수 있고, 각 함수에 전달되는 인수를 줄여서 객체 안세서의
+ 함수 호출을 간결하게 만들 수 있습니다.
+
+ 여러 함수에서 도출 로직이 반복되어 사용되다면 변환 함수로 묶어준다.
+ 변환 함수는 원본 데이터를 입력받아서 피룡한 정보를 모두 도출한 뒤, 각각을 출력 데이터의 필드에 넣어 반환한다.
+ 이렇게 해두면 도출 과정을 검토할 일이 생겼을 대 변환 함수만 살펴보면 된다.
+
+ 서로 다른 두 대상을 한꺼번에 다루는 코드를 발견하면 각각을 별개 모듈로 나누는 방법을 모색한다.
+ 모듈이 잘 분리되어 있다면 다른 모듈의 상세 내용은 기억하지 못해도 원하는 대로 수정을 끝마칠 수도 있다.
+
+ 데이터 레코드와 같은 가변 데이타를 저장할 때 객체를 사용합니다.
+ 객체를 사용하면 저장된 값에 대해 무엇을 어떻게 저장했는지 숨길수 있으며(캐슐화), 필드의 이름이 바뀌어도 동일한 메서드를 통해 제공 받을수 있습니다.
+
{body}
+ {/blog_entries} + + \ No newline at end of file diff --git a/app/Views/response/parser/home.php b/app/Views/response/parser/home.php index 6c7a0ad..7f3932d 100644 --- a/app/Views/response/parser/home.php +++ b/app/Views/response/parser/home.php @@ -8,17 +8,20 @@뷰 파서는 뷰 파일에 포함된 유사 변수에 대한 간단한 텍스트 대체를 수행합니다. 간단한 변수 또는 변수 태그 쌍을 구문 분석할 수 있습니다.
-유사 변수 또는 제어 구문은 중괄호({})로 묶습니다
-이러한 유사변수는 실제 PHP 변수가 아니며 템플릿(뷰 파일)에서 PHP 코드를 제거할 수 있는 일반 텍스트 표현입니다.
+
+ 뷰 파서는 뷰 파일에 포함된 유사 변수에 대한 간단한 텍스트 대체를 수행합니다. 간단한 변수 또는 변수 태그 쌍을 구문 분석할 수 있습니다.
+ 유사 변수 또는 제어 구문은 중괄호({})로 묶습니다
+ 이러한 유사변수는 실제 PHP 변수가 아니며 템플릿(뷰 파일)에서 PHP 코드를 제거할 수 있는 일반 텍스트 표현입니다.
+
`Parser` 클래스는 어플리케이션의 뷰 경로에 저장된 “PHP/HTML 스크립트”를 처리합니다. 이 스크립트는 PHP 코드를 포함할 수 없습니다.
-각 뷰의 매개 변수(의사 변수라고 함)는 사용자가 제공한 값의 유형에 따라 대체를 유발합니다. 유사 변수는 PHP 변수로 추출되지 않습니다. 그 대신 값은 유사 변수 구문을 통해 액세스되며 이름은 중괄호 안에서 참조됩니다.
-`Parser` 클래스는 내부적으로 연관 배열을 사용하여 `render()`를 호출할 때까지 의사 변수 설정을 누적합니다. 이는 의사 변수 이름이 고유해야 하거나, 나중에 설정된 매개 변수가 같은 이름의 이전 매개 변수보다 우선함을 의미합니다.
-또한 스크립트 내부의 다른 컨텍스트에 대한 이스케이프 매개 변수 값에 영향을 줍니다. 이스케이프된 각 값에 고유한 매개 변수 이름을 지정해야 합니다.
+
+ `Parser` 클래스는 어플리케이션의 뷰 경로에 저장된 “PHP/HTML 스크립트”를 처리합니다. 이 스크립트는 PHP 코드를 포함할 수 없습니다.
+ 각 뷰의 매개 변수(의사 변수라고 함)는 사용자가 제공한 값의 유형에 따라 대체를 유발합니다. 유사 변수는 PHP 변수로 추출되지 않습니다. 그 대신 값은 유사 변수 구문을 통해 액세스되며 이름은 중괄호 안에서 참조됩니다.
+ `Parser` 클래스는 내부적으로 연관 배열을 사용하여 `render()`를 호출할 때까지 의사 변수 설정을 누적합니다. 이는 의사 변수 이름이 고유해야 하거나, 나중에 설정된 매개 변수가 같은 이름의 이전 매개 변수보다 우선함을 의미합니다.
+ 또한 스크립트 내부의 다른 컨텍스트에 대한 이스케이프 매개 변수 값에 영향을 줍니다. 이스케이프된 각 값에 고유한 매개 변수 이름을 지정해야 합니다.
+
`render()` 메소드를 사용하여 다음과 같이 간단한 템플릿을 구문 분석(또는 렌더링)할 수 있습니다.
-뷰 파라미터는 템플릿에서 교체할 데이터의 연관 배열로 setData()에 전달됩니다. 다음 예제에서 템플릿은 두 개의 변수 `{blog_title}`과 `{blog_heading}`를 포함합니다.
-`render()`의 첫 번째 매개 변수는 뷰 파일의 이름입니다. 여기서 `blog_template`가 뷰 파일의 이름입니다.
+
+ `render()` 메소드를 사용하여 다음과 같이 간단한 템플릿을 구문 분석(또는 렌더링)할 수 있습니다.
+ 뷰 파라미터는 템플릿에서 교체할 데이터의 연관 배열로 setData()에 전달됩니다. 다음 예제에서 템플릿은 두 개의 변수 `{blog_title}`과 `{blog_heading}`를 포함합니다.
+ `render()`의 첫 번째 매개 변수는 뷰 파일의 이름입니다. 여기서 `blog_template`가 뷰 파일의 이름입니다.
+
대체 유형은 `simple, looping, nested` 세 가지가 지원됩니다. 유사 변수가 추가된 것과 동일한 순서로 대체가 수행됩니다.
-파서가 수행하는 단순 치환은 아래 예에서와 같이 해당 데이터 매개 변수에 스칼라 또는 문자열 값이 있는 의사 변수의 일대일 대체입니다.
+
+ 대체 유형은 `simple`, `looping`, `nested` 세 가지가 지원되며 유사 변수는 추가된 순서로 대체가 수행됩니다.
+ 파서가 수행하는 단순 치환은 아래 예와 같이 해당 데이터 매개 변수에 의사 변수(스칼라 또는 문자열 값이 있는)의 일대일 대체입니다.
+
의사 변수의 값이 배열인 경우 루프 대체가 발생합니다.
-다음 코드에서 한 쌍의 변수 `{blog_entries} data… {/blog_entries}`를 볼 수 있습니다.
-이와 같은 경우 이들 쌍들 사이의 전체 데이터 청크는 파라미터 배열의 “blog_entries” 요소의 행 수에 대응하여 여러 번 반복 됩니다.
-변수 쌍 구문 분석은 단일 변수를 구문 분석하기 위해 위에 표시된 동일한 코드를 사용하지만 데이터에 다차원 배열을 추가합니다.
-반복하려는 배열이 배열 대신 객체라면 파서는 먼저 객체에서 `asArray` 메소드를 찾습니다.
-`asArray` 메소드가 존재한다면 해당 메소드를 호출하여 얻은 결과 배열을 위에서 설명한대로 반복합니다.
-`asArray` 메소드가 없으면 객체가 배열로 캐스트(cast)되고 해당 퍼블릭 속성이 파서에 제공됩니다.
+
+ 의사 변수의 값이 배열인 경우 루프 대체가 발생합니다.
+ 다음 코드에서 한 쌍의 변수 `{blog_entries} data… {/blog_entries}`를 볼 수 있습니다.
+ 이와 같은 경우 이들 쌍들 사이의 전체 데이터 청크는 파라미터 배열의 “blog_entries” 요소의 행 수에 대응하여 여러 번 반복 됩니다.
+ 변수 쌍 구문 분석은 단일 변수를 구문 분석하기 위해 위에 표시된 동일한 코드를 사용하지만 데이터에 다차원 배열을 추가합니다.
+ 반복하려는 배열이 배열 대신 객체라면 파서는 먼저 객체에서 `asArray` 메소드를 찾습니다.
+ `asArray` 메소드가 존재한다면 해당 메소드를 호출하여 얻은 결과 배열을 위에서 설명한대로 반복합니다.
+ `asArray` 메소드가 없으면 객체가 배열로 캐스트(cast)되고 해당 퍼블릭 속성이 파서에 제공됩니다.
+
+ 간단한 사용자 헬퍼를 만들어 뷰와 파서를 연동하여 함께 사용할 수 있습니다. +
+ + = tabler_iframe_tabs([ + [ + 'id' => 'Exam4Show', + 'title' => '/response/parser/exam4', + 'href' => '/response/parser/exam4', + ], + [ + 'id' => 'Exam4View', + 'title' => 'exam4.php', + 'href' => '/sample/home/view/Views/response/parser/exam4', + ], + [ + 'id' => 'Exam4', + 'title' => 'Exam4.php', + 'href' => '/sample/home/view/Controllers/Response/Parser/Exam4', + ], + [ + 'id' => 'parserHelper', + 'title' => 'parser_helper.php', + 'href' => '/sample/home/view/Helpers/parser_helper', + ], + ]) ?> = tabler_card_end() ?> - = tabler_iframe_tabs([ - [ - 'id' => 'LoopView', - 'title' => 'blog_template.php', - 'href' => '/sample/home/view/Views/response/parser/blog_template', - ], - [ - 'id' => 'Loop', - 'title' => 'Loop.php', - 'href' => '/sample/home/view/Controllers/Response/Parser/Loop', - ], - [ - 'id' => 'LoopShow', - 'title' => '/response/parser/loop', - 'href' => '/response/parser/loop', - ], - ]) ?>